I am having much trouble encrypting the contents of the file at path accountfile. The encryption does indeed work, as the decryption successfully output the path of accountfile of a version where the path was being saved to the file. The code runs successfully without errors, but the encrypted file that is saved ends up being blank when encrypted. How do I get the contents of ptext to be successfully encrypted?
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)