Package: pyAesCrypt
About pyAesCryptpyAesCrypt is a Python 3 file-encryption module and script that uses AES256-CBC to encrypt/decrypt files and binary streams.pyAesCrypt is compatible with theAES Cryptfile format(version 2).It is Free Software, released under theApache License, Version 2.0.pyAesCrypt is brought to you by Marco Bellaccini - marco.bellaccini(at!)gmail.com.IMPORTANT SECURITY NOTE: version 2 of the AES Crypt file format does not authenticate the “file size modulo 16” byte. This implies that an attacker
with write access to the encrypted file may alter the corresponding plaintext file size by up to 15 bytes.NOTE: there is no low-level memory management in Python, hence it is not possible to wipe memory areas were sensitive information was stored.Module usage exampleHere is an example showing encryption and decryption of a file:importpyAesCryptpassword="please-use-a-long-and-random-password"# encryptpyAesCrypt.encryptFile("data.txt","data.txt.aes",password)# decryptpyAesCrypt.decryptFile("data.txt.aes","dataout.txt",password)This is the most straightforward way to use pyAesCrypt, and should be preferred.If you need to specify a custom buffer size (default is 64KB), you can pass it as an optional argument:importpyAesCrypt# custom encryption/decryption buffer size (default is 64KB)bufferSize=128*1024password="please-use-a-long-and-random-password"# encryptpyAesCrypt.encryptFile("data.txt","data.txt.aes",password,bufferSize)# decryptpyAesCrypt.decryptFile("data.txt.aes","dataout.txt",password,bufferSize)In case you need it, you can work with binary streams too:importpyAesCryptfromosimportstat,remove# encryption/decryption buffer size - 64K# with stream-oriented functions, setting buffer size is mandatorybufferSize=64*1024password="please-use-a-long-and-random-password"# encryptwithopen("data.txt","rb")asfIn:withopen("data.txt.aes","wb")asfOut:pyAesCrypt.encryptStream(fIn,fOut,password,bufferSize)# decryptwithopen("data.txt.aes","rb")asfIn:try:withopen("dataout.txt","wb")asfOut:# decrypt file streampyAesCrypt.decryptStream(fIn,fOut,password,bufferSize)exceptValueError:# remove output file on errorremove("dataout.txt")you can also perform in-memory encryption/decryption (using BytesIO):importpyAesCryptimportiobufferSize=64*1024password="please-use-a-long-and-random-password"# binary data to be encryptedpbdata=b"This is binary plaintext\x00\x01"# input plaintext binary streamfIn=io.BytesIO(pbdata)# initialize ciphertext binary streamfCiph=io.BytesIO()# initialize decrypted binary streamfDec=io.BytesIO()# encrypt streampyAesCrypt.encryptStream(fIn,fCiph,password,bufferSize)# print encrypted dataprint("This is the ciphertext:\n"+str(fCiph.getvalue()))# go back to the start of the ciphertext streamfCiph.seek(0)# decrypt streampyAesCrypt.decryptStream(fCiph,fDec,password,bufferSize)# print decrypted dataprint("Decrypted data:\n"+str(fDec.getvalue()))Script usage examplesEncrypt file test.txt in test.txt.aes:pyAesCrypt -e test.txtDecrypt file test.txt.aes in test.txt:pyAesCrypt -d test.txt.aesEncrypt file test.txt in test2.txt.aes:pyAesCrypt -e test.txt -o test2.txt.aesDecrypt file test.txt.aes in test2.txt:pyAesCrypt -d test.txt.aes -o test2.txtFAQsIs pyAesCrypt malware?NO!Of course it isn’t!Nevertheless, being a module, it can be used by any other software, including malware.In fact, it has been reported that it is used as crypto library by some ransomware.