Encrypting/Decrypting Files and Strings
Tuesday, August 4, 2026 at 4:33 PM | 1 min read
Last modified on Tuesday, August 4, 2026 at 4:33 PM
Photo by Rachel Claire on pexels.com
Overview
A comparison of two approaches to encrypting and decrypting data: a modularized Python application using the Fernet symmetric encryption method, and the GnuPG (gpg) command-line tool. The project covers both file encryption and string encryption, with each task split into its own single-purpose file.
Task Modularization
The Python approach splits encryption and decryption into individual files by responsibility:
- create_key.py — generates a 128-bit AES encryption key
- get_key.py — loads the generated key for use in the other scripts
- encrypt_file.py / decrypt_file.py — encrypt and decrypt a file
- encrypt_string.py / decrypt_string.py — encrypt and decrypt a string
The string-based functions required a bit more care: the encrypted message generated in encrypt_string.py has to be imported into decrypt_string.py so the correct token gets passed to message_decrypt, avoiding an InvalidToken error from the cryptography package.
Command Line Alternative
The same encrypt/decrypt tasks are also carried out using GnuPG, comparing the tradeoffs of a programmatic approach against a quicker, less structured command-line workflow using AES256 symmetric encryption.
Related Resources
- Encrypting and Decrypting Files and Strings: companion post covering both approaches in full detail
