def encrypt(self, key, filename): chunksize = 128 * 1024 outFile = os.path.join(os.path.dirname(filename), "(Secured)" + os.path.basename(filename)) filesize = str(os.path.getsize(filename)).zfill(16) IV = Random.new().read(AES.block_size) print(IV, len(IV)) encryptor = AES.new(key, AES.MODE_CBC, IV) with open(filename, "rb") as infile: with open(outFile, "wb") as outfile: outfile.write(filesize.encode('utf-8')) 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)) return outFile