I am writing a script to encrypt files in Python using RSA. I have successfully encrypted the original file and saved it to another file. When decrypting the encrypted file, the code is running, but when I am opening the image it is showing that the file is not a .jpg
type file. The values n, d and e are stored in nfile
, dfile
, and efile
, respectively.
Here's the encryption and decryption part of the code:
if myMode == 'encrypt':
n = open('nfile.pem', 'r')
e = open('efile.pem', 'r')
n1 = int(n.read())
e1 = int(e.read())
translated = str(pow(content1, e1, n1))
print(translated)
elif myMode == 'decrypt':
fileobj2 = open('encrypted.jpg', 'rb')
content2 = fileobj2.read
content3 = int.from_bytes(b'content2', byteorder='big')
fileobj2.close()
n = open('nfile.pem', 'r')
d = open('dfile.pem', 'r')
n1 = int(n.read())
d1 = int(d.read())
translated = str(pow(content3, d1, n1))
translated1 = str.encode(translated)
# Write out the translated message to the output file.
outputFileObj = open('decrypted1.jpg', 'wb')
outputFileObj.write(translated1)
outputFileObj.close()
Here, encrypted.jpg
is the file that is generated after encryption.
The attached image is the error that I am facing
Looking forward to suggestions.