To implement hybrid encryption where a symmetric key (AES) is securely distributed using asymmetric encryption (RSA), and then used to encrypt and decrypt data efficiently.
Use RSA to safely share AES keys over insecure channels
Leverage AES for fast bulk data encryption
Combine RSA security with AES speed
Combine the strengths of both symmetric and asymmetric cryptography:
Address the limitations of using either system alone:
In modern cryptography, hybrid encryption combines:
This approach is used in real-world protocols like TLS, Signal, PGP, etc.
| Feature | Symmetric (AES) | Asymmetric (RSA) |
|---|---|---|
| Speed | ✅ Very fast | ❌ Slow |
| Secure Key Exchange | ❌ Needs secure channel | ✅ Secure key transfer |
| Encrypts Large Data | ✅ Efficient | ❌ Inefficient |
Alice wants to send a message to Bob securely
# Alice want to send the message to Bob
from Crypto.Cipher import AES, PKCS1_OAEP
from Crypto.PublicKey import RSA
from Crypto.Random import get_random_bytes
from Crypto.Util.Padding import pad, unpad
# 1. Alice generates AES (session) key
symmetric_key = get_random_bytes(16)
# Alice want to share this session key to Bob
# 2. Bob generates RSA (asymmetric) key pair
rsa_key = RSA.generate(2048)
rsa_pub = rsa_key.publickey() # shared in public - Alice have
# 3. Alice encrypts session key using BOB RSA public key
rsa_cipher = PKCS1_OAEP.new(rsa_pub)
encrypted_symmetric_key = rsa_cipher.encrypt(symmetric_key)
# 4. Alice encrypts the message using AES CBC
message = b'GECBHARUCH KEY MGMT DEMO'
iv = get_random_bytes(16) # for AES CBC
aes_cipher = AES.new(symmetric_key, AES.MODE_CBC, iv)
ciphertext = aes_cipher.encrypt(pad(message, 16))
# 5. Alice sends ciphertext and encrypted_symmetric_key to BOB
# 6. Bob decrypts encrypted_symmetric_key using BOB RSA private key
rsa_decipher = PKCS1_OAEP.new(rsa_key)
decrypted_key = rsa_decipher.decrypt(encrypted_symmetric_key)
# 7. Bob decrypt ciphertext using AES CBC with decrypted_key
aes_decipher = AES.new(decrypted_key, AES.MODE_CBC, iv)
decrypted_data = unpad(aes_decipher.decrypt(ciphertext), 16)
# Output
print("Encrypted symmetric key (RSA):", encrypted_symmetric_key.hex())
print("Decrypted text (AES):", decrypted_data.decode())
Only the intended recipient with the RSA private key can decrypt the AES key and message
Padding and encryption ensure tampering results in decryption failure
Data encryption and key transmission use separate cryptographic techniques
Hybrid model prevents brute-force attacks on large data by offloading encryption to AES
This hybrid method is used in:
| Application | Role of Hybrid Encryption |
|---|---|
| TLS/SSL | RSA to send AES session key |
| Email (PGP) | RSA to encrypt AES-encrypted message |
| Secure Messaging | Signal protocol uses hybrid encryption |
| Cloud Storage | Encrypt AES key with RSA for each recipient |
Combines RSA (for key) + AES (for data)
Fast, symmetric cipher for bulk data
Secure asymmetric key exchange
Secure padding for RSA encryption
Required for block cipher like AES in CBC mode
After this practical, students will understand: