Practical 11

Symmetric Key Distribution using RSA (Hybrid Encryption)

🎯 Objective

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.

🔐 Secure Key Distribution

Use RSA to safely share AES keys over insecure channels

⚡ Efficient Encryption

Leverage AES for fast bulk data encryption

🛡️ Best of Both Worlds

Combine RSA security with AES speed

🎯 Purpose of Hybrid Encryption

Combine the strengths of both symmetric and asymmetric cryptography:

Address the limitations of using either system alone:

📚 Theory: What is Hybrid Encryption?

In modern cryptography, hybrid encryption combines:

This approach is used in real-world protocols like TLS, Signal, PGP, etc.

Why Hybrid?

Feature Symmetric (AES) Asymmetric (RSA)
Speed ✅ Very fast ❌ Slow
Secure Key Exchange ❌ Needs secure channel ✅ Secure key transfer
Encrypts Large Data ✅ Efficient ❌ Inefficient
💡 Solution: Use RSA to encrypt the AES key, and AES to encrypt the data → secure and fast!

💻 Implementation Code

Alice wants to send a message to Bob securely

Python Implementation
# 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())

📤 Output

Encrypted symmetric key (RSA): 11726e17946bbe1fb4aa2624c86cb08edb55f438dbc0fe588a19b3b323f73f173d0ba2b8eed3f0ff8cb6bf30a1f096b766eb499933a703ce14221dc06b7b139539f43404b8b8025f2921a2135956044c75ba7e7b532d2003ebeac3c7fbdd6c2a2c83aceba866693e761bda24133ba18c5efe9c65c4c04e1c633721db8639d8340b1953ba81026d3bf2ca5d85c229fd5a9826a77705e498350cfb51f853eb64e6de966bc589f910806f3819d952481270a39ae008cae7e4175be995c9ec653dfdde8008ff4e19c22a63eae1b8640e8b841410010f61959f4f43d6d662dca8381d7161716ea767357054a22a725b7afd7ff3952bf31aa2b78cd07ae0eb124c2dea Decrypted text (AES): GECBHARUCH KEY MGMT DEMO

🛡️ Security Aspects

🔒 Confidentiality

Only the intended recipient with the RSA private key can decrypt the AES key and message

✅ Integrity

Padding and encryption ensure tampering results in decryption failure

🔑 Key Separation

Data encryption and key transmission use separate cryptographic techniques

🚫 Brute Force Protection

Hybrid model prevents brute-force attacks on large data by offloading encryption to AES

🌍 Real-World Relevance

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

📋 Summary

Hybrid Encryption

Combines RSA (for key) + AES (for data)

AES

Fast, symmetric cipher for bulk data

RSA

Secure asymmetric key exchange

PKCS1_OAEP

Secure padding for RSA encryption

pad/unpad

Required for block cipher like AES in CBC mode

🔍 Key Observations

  • ✅ The symmetric key was successfully encrypted using the RSA public key and decrypted using the RSA private key
  • ✅ AES encryption and decryption using the original and decrypted keys yielded the original plaintext
  • ✅ Even though the actual message is encrypted with AES, its security relies on how well the AES key is protected by RSA

🎓 Learning Outcomes

After this practical, students will understand: