Cyber Security Lab Assignment 4
Cyber Security Lab Assignment 4
Program :
import os
# Message to encrypt
padder = padding.PKCS7(128).padder()
encryptor = cipher.encryptor()
decryptor = cipher.decryptor()
unpadded_message = decryptor.update(ciphertext) +
decryptor.finalize()
unpadder = padding.PKCS7(128).unpadder()
print("Ciphertext:", ciphertext)
print("Key:", key)
B. RSA algorithm
Key Generation (Step 1)
1. Select Two Large Prime Numbers (p and q):
Choose two large primes. Their product n = p * q is used as the modulus.
2. Compute Euler’s Totient (φ(n)):
φ(n)=(p−1)⋅(q−1)
3. Choose a Public Exponent (e):
Select e such that 1<e<φ(n) and gcd(e,φ(n))=1
4. In the code, e = 65537 is a common choice.
5. Compute the Private Exponent (d):
Calculate d such that d⋅e≡1mod φ(n) using the modular multiplicative inverse.
The public key is (n, e) and the private key is (n, d).
Encryption (Step 2)
Decryption (Step 3)
Program:
public_key = private_key.public_key()
ciphertext = public_key.encrypt(
message,
plaintext = private_key.decrypt(
ciphertext,
print("Ciphertext:", ciphertext)