Encrypting and decrypting with the AES algorithm

AES encryption needs a strong key. The stronger the key, the stronger your encryption. Our AES Key needs to be either 16, 24, or 32 bytes long and our Initialization Vector needs to be 16 Bytes long. That will be generated using the random and string modules.

To use an encryption algorithm such as AES, we can import it from the Crypto.Cipher.AES package. As the PyCrypto block-level encryption API is very low level, it only accepts 16-, 24-, or 32-bytes-long keys for AES-128, AES-196, and AES-256, respectively. The longer the key, the stronger the encryption.

Also, for AES encryption using pycrypto, you need to ensure that the data is a multiple of 16 bytes in length. Pad the buffer if it is ...

Get Mastering Python for Networking and Security now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.