Advanced Encryption Standard Implementation in Python
To implement AES encryption and decryption in Python using pycryptodome and understand the basics of AES key expansion and structure.
To implement AES (Advanced Encryption Standard) encryption and decryption using ECB mode. This practical also helps learners understand the importance of key expansion and secure block cipher design used in modern cryptography.
| Feature | Value |
|---|---|
| Block Size | 128 bits (16 bytes) |
| Key Sizes | 128, 192, 256 bits |
| Type | Symmetric Cipher |
| Structure | SubstitutionβPermutation Network |
| Rounds (128-bit) | 10 |
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
# 16-byte key (128-bit AES)
key = b'ThisIsASecretKey'
plaintext = b'GECBHARUCH2025'
# AES cipher in ECB mode
cipher = AES.new(key, AES.MODE_ECB)
# Encrypt
ciphertext = cipher.encrypt(pad(plaintext, 16))
print("Encrypted (hex):", ciphertext.hex())
# Decrypt
decrypted = unpad(cipher.decrypt(ciphertext), 16)
print("Decrypted text:", decrypted.decode())
Encrypted (hex): af79118251b6f9dd37268ff7418d7a40 Decrypted text: GECBHARUCH2025
AES generates round keys from the initial key using a process called key expansion.
For AES-128, performs 10 encryption rounds
Uses original key + 10 generated keys
RotWord, SubWord, Rcon, XOR operations
This process ensures each encryption round uses a unique and secure key, adding complexity and improving resistance to attacks.
ECB is not secure for sensitive data, because identical plaintext blocks yield identical ciphertext blocks.
More secure modes: CBC, CFB, CTR (recommended for real use cases).
| Feature | Practical 2: DES | Practical 3: AES |
|---|---|---|
| Standard | Outdated | Current global standard |
| Key Size | 56 bits (effective) | 128 bits (can be 192/256) |
| Block Size | 64 bits | 128 bits |
| Rounds | 16 fixed | 10 for 128-bit key |
| Security | Weak (brute-forceable) | Strong (not practically breakable) |
| Speed | Slower, older hardware | Faster and more efficient on modern CPUs |
| Applications | Legacy systems | Web, banking, cloud, mobile, government |
AES is the current global standard for symmetric-key encryption, and is widely used in:
Financial systems and secure PIN encryption
SSL/TLS encryption for websites
WPA2/WPA3 uses AES encryption
Data protection in cloud, USB drives, smartphones
Military communications (approved by NIST and NSA)
After completing this practical, students will:
AES supports multiple key sizes, improving flexibility and security.
Block size is fixed at 128 bits, ensuring consistency.
AES performs a series of substitutions, permutations, and key additions β not just simple shifting or bit-flipping.
Use of Crypto.Cipher.AES abstracts away complex operations, but understanding the internals (e.g., S-boxes, rounds, key schedule) is vital for deeper knowledge.
This demonstrates AES, the most widely used and trusted symmetric encryption algorithm in the world. Students learn to implement AES in ECB mode, use padding, and handle key management. Compared to DES, AES provides greater security, efficiency, and adaptability, making it essential for modern secure systems. While ECB mode is used here for simplicity, students should be made aware of more secure modes (like CBC, CTR) for production use.