我在加密路径 accountfile 处的文件内容时遇到很多麻烦。加密确实有效,因为解密成功输出了保存到文件的版本的 accountfile 的路径。代码运行成功,没有错误,但加密后保存的加密文件最终为空。如何让ptext的内容加密成功?
def encrypt_account(path, filename, accountfile):
c_file = PurePath(path).parent / (PurePath(filename).parent.name + "c.txt")
file3 = open(c_file, 'rb')
byteskey = file3.read(32)
file3.close()
ckey = bytes(byteskey)
cipher = AES.new(ckey, AES.MODE_CBC)
ptext = open(str(accountfile)).read()# this is the account file
ciphertext = cipher.encrypt(pad(bytes(ptext, "utf-8"), AES.block_size))
with open(str(path.parent / accountfile.stem) + ".enc", 'wb')as c_file:
c_file.write(cipher.iv)
c_file.write(ciphertext)
#c_file.close()
#os.remove(accountfile)
手掌心
相关分类