Need to encrypt some text with a password or private key in Python? AES-256 is a solid symmetric cipher that is commonly used to encrypt data for oneself. In other words, the same person who is encrypting the data is typically decrypting it as well (think password manager).
Before we dive in, if you’re here because you’re interested in learning cryptography in a more comprehensive and structured way, I recently released a free hands-on coding course, Practical Cryptography, where you can do so.
For this tutorial, we’ll be using Python 3, so make sure you install pycryptodome, which will give us access to an implementation of AES-256:
AES-256 typically requires that the data to be encrypted is supplied in 16-byte blocks, and you may have seen that on other sites or tutorials. AES-256 in GCM mode, however, doesn’t require any special padding to be done by us manually.
Now we create a simple encrypt(plain_text, password) function. This function uses the password to encrypt the plain text. Therefore, anyone with access to the encrypted text and the password will be able to decrypt it.
We use AES in a mode of operation in order to encrypt. The solutions above suggest using CBC, which is one example. Another is called CTR, and it’s somewhat easier to use: # AES supports multiple key sizes: 16 (AES128), 24 (AES192), or 32 (AES256). # pair (iv, ciphtertext). 'iv' stands for initialization vector. Edit 2015.12.14: thanks to Stephen for pointing out that the block size for AES is always 16, and the key size can be 16, 24, or 32. See FIPS-197 for more details. If you plan to use this script, you'll need to have PyCrypto installed on your computer. You may also want to check out all available functions/classes of the module Crypto.Cipher.AES, or try the search function. Project: file-encryptor Author: StorjOld File: convergence.py License: MIT License. Def itertransform(filename, key): 'Generate encrypted file with given key. This generator function reads the file.
salt.nonce.tag.cipher_text
You probably want to see it all work in an example script. Look no further!
Thanks for reading, now take a course!
Interested in a high-paying job in tech? Land interviews and pass them with flying colors after taking my hands-on coding courses.
Follow and hit me up on Twitter @q_vault if you have any questions or comments. If I’ve made a mistake in the article be sure to let me know so I can get it corrected!
Subscribe to my newsletter for more coding articles delivered straight to your inbox.
AWS Boto3 is the Python SDK for AWS. Boto3 can be used to directly interact with AWS resources from Python scripts. In this tutorial, we will look at how we can use the Boto3 library to perform various operations on AWS KMS.
Table of contents
pip install boto3
cryptopgraphy
: We will be using the cryptography package to encrypt and decrypt data.A Customer Master Key (CMK) is used to encrypt data. However, the maximum size of data that can be encrypted using the master key is 4KB. CMKs are used to generate, encrypt, and decrypt data keys that can be used outside of AWS KMS to encrypt data.
AWS KMS supports two types of CMKs:
The following function creates a new Customer Master Key:
The output of the above function should be something like:
CMKs are created, managed and stored within AWS KMS. The following snippet shows how to retrieve an existing CMK based on the description it was created with.
Output
A data key is a unique symmetric data key that is used to encrypt data outside of AWS KMS. AWS returns both an encrypted and a plaintextversion of the data key.
AWS recommends the following pattern to use the data key to encrypt data outside of AWS KMS:
The function below generates a data key and returns the encrypted as well as plaintext copy of the key.
Data can be encrypted client-side using the generated data key along with the cryptography package in Python. It is recommended to store the encrypted data key along with your encrypted data since that will be used to decrypt the data in the future.
Next, let’s create a file called test_file
with the following content:
After running the encrypt_file
function on our input file, the contents of the encrypted file should look something like:
The decrypt
function can be used to decrypt an encrypted data key. The decrypted data key can then be used to decrypt any data on the client side.
Output of running this function on the encrypted file: