def encryptFile(in_filename, out_filename=None, chunksize=64*1024):
if not out_filename:
out_filename = in_filename + '.enc'
key = Random.get_random_bytes(32)
iv = Random.get_random_bytes(16)
encryptor = AES.new(key, AES.MODE_CBC, iv)
#filesize = os.path.getsize(in_filename)
with open(in_filename, 'rb') as infile:
with open(out_filename, 'wb') as outfile:
#outfile.write(struct.pack('<Q', filesize))
outfile.write(key)
outfile.write(iv)
while True:
chunk = infile.read(chunksize)
if len(chunk) == 0:
break
elif len(chunk) % 16 != 0:
chunk += b' ' * (16 - len(chunk) % 16)
outfile.write(encryptor.encrypt(chunk))
os.remove(in_filename)
评论列表
文章目录