Hello there, how’s it going? (Download script here)
Its amazing how powerful this programming language is, really. This is mostly what I wanted to show you guys this time and one of the main reasons I keep coming back time after time to Python.
Using about 20 lines of code, we’re able to encrypt many files in seconds.
Anyway, this post will basically function as a introduction into encryption in Python 3. I had many posts about using encryption in my previous blog with PyCrypto – and I’m still a big fan. However, with Python 2.X in the midst of sunsetting, I figure its time to start looking at newer libraries.
With that said, pyAesCrypt seems to work rather seamlessly and provides a few different options for functionality – file encryption, stream encryption and in-memory encryption (sounds spicy ay?).
Encryption Using pyAesCrypt
Before we get into the recursive file encryption script, let’s take a look at their brief, yet effective example of basic file encryption:
import pyAesCrypt # encryption/decryption buffer size - 64K bufferSize = 64 * 1024 password = "foopassword" # encrypt pyAesCrypt.encryptFile("data.txt", "data.txt.aes", password, bufferSize) # decrypt pyAesCrypt.decryptFile("data.txt.aes", "dataout.txt", password, bufferSize)
As you can see its very simple and straight to the point.
All it requires is the file to be encrypted, the new file, a password and the buffer size – all of which are self-explanatory.
With that out of the way, let’s dive into the recursive encrypt script:
import pyAesCrypt import glob import os # Encryption/decryption buffer size - 64K bufferSize = 64 * 1024 # Get current directory curDir = os.getcwd() # Prompt user for password to encrypt files password1 = input('\n> Enter password to encrypt: ') print('\n> Beginning recursive encryption...\n\n') # Main loop to encrypt all files recursively for x in glob.glob('directory\\**\*', recursive=True): fullpath = os.path.join(curDir, x) fullnewf = os.path.join(curDir, x + '.aes') # Encrypt if os.path.isfile(fullpath): print('>>> Original: \t' + fullpath + '') print('>>> Encrypted: \t' + fullnewf + '\n') pyAesCrypt.encryptFile(fullpath, fullnewf, password1, bufferSize) # Remove file after encryption os.remove(fullpath)
We’ll simply prompt the user for a password and get right to business.
Using glob, we’ll point to the directory that we’re going inside off recursively. Remember to set the recursive parameter to True.
It’s important to use the full path in these cases since running the script from a different directory or other silly mistakes could cause one to encrypt their entire hard drive or something – remember, recursion!
Anyway we’ll make a check here using os.path.isfile to make sure we are dealing with an actual file and not a directory – that would return an error.
Finally we’ll encrypt the file and remove the original afterwords.
import pyAesCrypt import glob import os # Encryption/decryption buffer size - 64K bufferSize = 64 * 1024 # Get current directory curDir = os.getcwd() # Prompt user for password to decrypt files password1 = input('\n> Enter password to decrypt: ') print('\n> Beginning recursive decryption...\n\n') # Main loop to decrypt all files recursively for x in glob.glob('directory\\**\*', recursive=True): fullpath = os.path.join(curDir, x) fullnewf = os.path.join(curDir, os.path.splitext(x)) # Decrypt if os.path.isfile(fullpath): print('>>> Encrypted: \t' + fullpath + '') try: pyAesCrypt.decryptFile(fullpath, fullnewf, password1, bufferSize) print('>>> Decrypted: \t' + fullnewf + '\n') os.remove(fullpath) # Remove encrypted file after decrypt except ValueError: print('>>> Error - Wrong password!\n')
There really isn’t much different in the decryption script, except for the main pyAesCrypt decrypt method of course.
The main thing is that you change the ‘directory‘ on the script to whatever directory you want to run the script in recursively.
Another thing to note is that you can comment out the os.remove(fullpath) in each script in case you don’t want to delete the original file.
Hope you guys enjoyed this one, very simple, but for sure I will get into some more functionality provided by pyAesCrypt.
‘Till then, see you guys next time!