I would like to know how I can convert an integer into a binary number in a sequence of length = 32 bytes and least-significant-bit first encoding, in Python 3.
I need to do this conversion for an exercise in my cryptography class. I have tried the python function int.to_bytes()
but it seems not working...
The integer I would like to convert is x = 999.
This is my code if it can help:
def reading_a_pdf_file (filename):
rfile = open(filename, 'rb')
contains = rfile.read();
rfile.close();
return contains
# ...
def writing_in_a_pdf_file (filename, txt):
wfile = open(filename, 'wb')
wfile.write(txt)
wfile.close()
# ...
import nacl.secret
import nacl.utils
x= 999
key = x.to_bytes(32, byteorder='little')
# creating the box of encryption/decryption
box = nacl.secret.SecretBox(key)
# reading the encrypted file
encrypted = reading_a_pdf_file('L12-14.enc.pdf')
# we decrypt the contain of the file
decrypted = box.decrypt(encrypted)
# finally we save into a new pdf file
writing_in_a_pdf_file('L12-14.pdf', decrypted)
print("The file have been successfully decrypted in 'L12-14.pdf'")
At the end of the program, I am supposed to get the file L12-14.pdf, but I get the error: "Decryption failed. Ciphertext failed verification" which means that my key is not the good one.
As I know the integer is right, I suppose I am making a mistake when I convert it.
Can you help me ?