Ex.
7 HYBRID CRYPTOGRAPHY
Date: 18.02.25
By:
S Nittin Balajee
23011103042
Aim:
Implement a Hybrid (private+public) Encryption and Decryption Mechanism for Data
Protection in Transit.
Code:
import time
from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes
from Crypto.Util.Padding import pad, unpad
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import padding
from cryptography.hazmat.primitives import hashes
import base64
# 1. Generate RSA keys (Public and Private)
def generate_rsa_keys():
private_key = rsa.generate_private_key(
public_exponent=65537,
key_size=2048,
)
private_pem = private_key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.TraditionalOpenSSL,
encryption_algorithm=serialization.NoEncryption()
)
public_key = private_key.public_key()
public_pem = public_key.public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo
)
return private_pem, public_pem
# 2. Encrypt Data using AES
def encrypt_data_with_aes(data, aes_key):
print("Encrypting data with AES...")
cipher = AES.new(aes_key, AES.MODE_CBC)
ct_bytes = cipher.encrypt(pad(data.encode(), AES.block_size))
return cipher.iv + ct_bytes # Return IV + Ciphertext
# 3. Encrypt AES key using RSA
def encrypt_aes_key_with_rsa(aes_key, public_key_pem):
print("Encrypting AES key with RSA...")
public_key = serialization.load_pem_public_key(public_key_pem)
encrypted_aes_key = public_key.encrypt(
aes_key,
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(),
label=None
)
)
return encrypted_aes_key
# 4. Decrypt AES Key using RSA
def decrypt_aes_key_with_rsa(encrypted_aes_key, private_key_pem):
print("Decrypting AES key with RSA...")
private_key = serialization.load_pem_private_key(private_key_pem, password=None)
decrypted_aes_key = private_key.decrypt(
encrypted_aes_key,
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(),
label=None
)
)
return decrypted_aes_key
# 5. Decrypt Data using AES
def decrypt_data_with_aes(encrypted_data, aes_key):
print("Decrypting data with AES...")
iv = encrypted_data[:AES.block_size]
ciphertext = encrypted_data[AES.block_size:]
cipher = AES.new(aes_key, AES.MODE_CBC, iv)
decrypted_data = unpad(cipher.decrypt(ciphertext), AES.block_size)
return decrypted_data.decode()
# Example Usage
# Generate RSA keys (Public and Private)
private_key_pem, public_key_pem = generate_rsa_keys()
# Generate a random AES key
aes_key = get_random_bytes(32) # 256-bit AES key
# Encrypt some data using AES
data = "Test message."
start_time = time.time()
encrypted_data = encrypt_data_with_aes(data, aes_key)
print(f"Encryption took {time.time() - start_time} seconds")
# Encrypt the AES key using the RSA public key
start_time = time.time()
encrypted_aes_key = encrypt_aes_key_with_rsa(aes_key, public_key_pem)
print(f"AES key encryption took {time.time() - start_time} seconds")
# Decrypt the AES key using RSA private key
start_time = time.time()
decrypted_aes_key = decrypt_aes_key_with_rsa(encrypted_aes_key, private_key_pem)
print(f"AES key decryption took {time.time() - start_time} seconds")
# Decrypt the data using the decrypted AES key
start_time = time.time()
decrypted_data = decrypt_data_with_aes(encrypted_data, decrypted_aes_key)
print(f"Decryption took {time.time() - start_time} seconds")
# Output the results
print("Original Data:", data)
print("Decrypted Data:", decrypted_data)
Output:
Result:
Implement a Hybrid (private+public) Encryption and Decryption Mechanism for Data
Protection in Transit is successful.