kenneth.lara
kenneth.lara 5d ago โ€ข 20 views

How to Encrypt Files with AES in Python: A Beginner's Guide

Hey everyone! ๐Ÿ‘‹ Ever wondered how to keep your files super secure with Python? It sounds complicated, but it's totally doable! I'm working on a project where I need to encrypt sensitive data, and I found AES encryption. Let's learn how to do it together โ€“ it's easier than you think! ๐Ÿค“
๐Ÿ’ป Computer Science & Technology
๐Ÿช„

๐Ÿš€ Can't Find Your Exact Topic?

Let our AI Worksheet Generator create custom study notes, online quizzes, and printable PDFs in seconds. 100% Free!

โœจ Generate Custom Content

1 Answers

โœ… Best Answer
User Avatar
michael.white Dec 30, 2025

๐Ÿ“š Introduction to AES Encryption

AES (Advanced Encryption Standard) is a symmetric block cipher widely used for securing sensitive data. Symmetric means the same key is used for both encryption and decryption. AES is known for its speed, efficiency, and strong security, making it a standard for various applications from securing Wi-Fi networks to protecting financial transactions. It replaced the older DES (Data Encryption Standard).

๐Ÿ“œ History and Background

In 1997, the U.S. National Institute of Standards and Technology (NIST) initiated a process to find a replacement for DES. Fifteen candidates were presented, and in 2001, Rijndael algorithm (developed by Joan Daemen and Vincent Rijmen) was selected as the AES. AES has three key sizes: 128-bit, 192-bit, and 256-bit, each offering a different level of security. Larger key sizes provide stronger encryption but also require more computational resources.

๐Ÿ”‘ Key Principles of AES

  • ๐Ÿงฑ Block Cipher: AES operates on fixed-size blocks of data. Typically, these blocks are 128 bits in size.
  • ๐Ÿ”’ Symmetric Key: The same key is used for both encryption and decryption, so it's crucial to keep the key secret.
  • ๐Ÿ”„ Rounds: AES involves multiple rounds of substitution, permutation, and mixing to transform the plaintext into ciphertext. The number of rounds depends on the key size.
  • ๐Ÿ”ข Key Expansion: The encryption key is expanded into a larger key schedule used during the encryption and decryption rounds.

๐Ÿ’ป Real-World Examples in Python

Let's dive into practical examples of using AES in Python. We'll use the pycryptodome library, a powerful cryptographic toolkit.

Installing PyCryptodome

First, install the necessary library:

pip install pycryptodome

Encryption Example


from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes
from Crypto.Util.Padding import pad, unpad
import base64

def encrypt(plain_text, password):
    # Generate a random salt
    salt = get_random_bytes(AES.block_size)

    # Derive a key from the password and salt
    private_key = hashlib.scrypt(
        password.encode(), salt=salt, n=214, r=8, p=1, dklen=32
    )

    cipher_config = AES.new(private_key, AES.MODE_GCM)

    # Encrypt the plaintext
    cipher_text, tag = cipher_config.encrypt_and_digest(bytes(plain_text, 'utf-8'))
    
    return {
        'salt': base64.b64encode(salt).decode('utf-8'),
        'cipher_text': base64.b64encode(cipher_text).decode('utf-8'),
        'tag': base64.b64encode(tag).decode('utf-8'),
        'nonce': base64.b64encode(cipher_config.nonce).decode('utf-8')
    }

Decryption Example


import hashlib

def decrypt(enc_dict, password):
    salt = base64.b64decode(enc_dict['salt'])
    cipher_text = base64.b64decode(enc_dict['cipher_text'])
    tag = base64.b64decode(enc_dict['tag'])
    nonce = base64.b64decode(enc_dict['nonce'])

    private_key = hashlib.scrypt(
        password.encode(), salt=salt, n=214, r=8, p=1, dklen=32
    )

    cipher = AES.new(private_key, AES.MODE_GCM, nonce=nonce)

    decrypted = cipher.decrypt_and_verify(cipher_text, tag)

    return decrypted.decode('utf-8')

Complete Example


import hashlib
import base64
from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes


def encrypt(plain_text, password):
    # Generate a random salt
    salt = get_random_bytes(AES.block_size)

    # Derive a key from the password and salt
    private_key = hashlib.scrypt(
        password.encode(), salt=salt, n=214, r=8, p=1, dklen=32
    )

    cipher_config = AES.new(private_key, AES.MODE_GCM)

    # Encrypt the plaintext
    cipher_text, tag = cipher_config.encrypt_and_digest(bytes(plain_text, 'utf-8'))
    
    return {
        'salt': base64.b64encode(salt).decode('utf-8'),
        'cipher_text': base64.b64encode(cipher_text).decode('utf-8'),
        'tag': base64.b64encode(tag).decode('utf-8'),
        'nonce': base64.b64encode(cipher_config.nonce).decode('utf-8')
    }

def decrypt(enc_dict, password):
    salt = base64.b64decode(enc_dict['salt'])
    cipher_text = base64.b64decode(enc_dict['cipher_text'])
    tag = base64.b64decode(enc_dict['tag'])
    nonce = base64.b64decode(enc_dict['nonce'])

    private_key = hashlib.scrypt(
        password.encode(), salt=salt, n=214, r=8, p=1, dklen=32
    )

    cipher = AES.new(private_key, AES.MODE_GCM, nonce=nonce)

    decrypted = cipher.decrypt_and_verify(cipher_text, tag)

    return decrypted.decode('utf-8')


password = "P@$$wOrd"
message = "Secret message"

encrypted = encrypt(message, password)
decrypted = decrypt(encrypted, password)

print(f"Encrypted message: {encrypted}")
print(f"Decrypted message: {decrypted}")

๐Ÿ›ก๏ธ Security Considerations

  • ๐Ÿ”‘ Key Management: Securely store and manage your encryption keys. Avoid hardcoding keys directly into your code.
  • ๐Ÿง‚ Use Salt: Always use a unique salt for each encryption operation to prevent dictionary attacks.
  • ๐Ÿ“ฆ Initialization Vectors (IVs) or Nonces: Use unique IVs or nonces to ensure that the same plaintext encrypted multiple times results in different ciphertexts.
  • โš ๏ธ Beware of Padding: Ensure that padding is handled correctly to avoid vulnerabilities.

๐Ÿ“ Conclusion

AES encryption is a powerful tool for securing data in Python. By understanding the principles and using libraries like pycryptodome, you can implement robust encryption solutions. Remember to prioritize key management and follow security best practices to ensure the confidentiality and integrity of your data.

Join the discussion

Please log in to post your answer.

Log In

Earn 2 Points for answering. If your answer is selected as the best, you'll get +20 Points! ๐Ÿš€