In this section, we will learn about the pyAesCrypt module of Python. pyAesCrypt is a file encryption module that uses AES256-CBC to encrypt/decrypt files and binary streams.
Install pyAesCrypt as follows:
pip3 install pyAesCrypt
Create a script called file_encrypt.py and write the following code in it:
import pyAesCrypt
from os import stat, remove
# encryption/decryption buffer size - 64K
bufferSize = 64 * 1024
password = "#Training"
with open("sample.txt", "rb") as fIn:
with open("sample.txt.aes", "wb") as fOut:
pyAesCrypt.encryptStream(fIn, fOut, password, bufferSize)
# get encrypted file size
encFileSize = stat("sample.txt.aes").st_size
Run the script as follows:
student@ubuntu:~/work$ python3 file_encrypt.py
Output :
Please check your current working directory. You will find the sample.txt...