【发布时间】:2019-05-04 11:17:35
【问题描述】:
我正在编写一个脚本来使用 RSA 在 Python 中加密文件。我已成功加密原始文件并将其保存到另一个文件中。解密加密文件时,代码正在运行,但是当我打开图像时,它显示该文件不是.jpg 类型的文件。值 n、d 和 e 分别存储在 nfile、dfile 和 efile 中。
下面是加解密部分的代码:
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()
这里encrypted.jpg是加密后生成的文件。
附图是我面临的错误
期待建议。
【问题讨论】:
标签: python python-3.x encryption file-handling public-key-encryption