Ass.
No: 2 Title: RSA
Exp. No: 3
Date: “8/2/25”
Name- Ajat
shatru singh
Reg.no-
22BCE0337
Problem Statement
To implement an encryption and decryption process using RSA algorithm in python.
Problem Description
i) Key Generation
• RSA key generation involves selecting two large prime numbers and computing
their product.
• A public and private key pair is generated using mathematical functions.
• The public key is used for encryption, and the private key is used for decryption.
ii) Encryption Process
• The sender encrypts the message using the receiver’s public key.
• The message is converted into numerical form and raised to the power of e
modulo n.
iii) Decryption Process
• The receiver decrypts the ciphertext using their private key.
• The ciphertext is raised to the power of d modulo n to retrieve the original
message.
Algorithm:
i) Key Generation
➢ Choose two large prime numbers p and q.
[Type here] [Type here] [Type here]
➢ Compute n = p * q.
➢ Compute φ(n) = (p-1) * (q-1).
➢ Choose a public exponent e such that 1 < e < φ(n) and gcd(e, φ(n)) = 1.
➢ Compute the private exponent d such that (d * e) % φ(n) = 1.
➢ Public key: (n, e), Private key: (n, d).
[Type here] [Type here] [Type here]
ii) Encryption Process
➢ Convert the plaintext message into numerical format.
➢ Compute ciphertext C = M^e mod n.
➢ Send the ciphertext.
iii) Decryption Process
➢ Receive the ciphertext C.
➢ Compute plaintext M = C^d mod n.
➢ Convert M back to text format.
Program:
import random
from sympy import isprime, mod_inverse
def generate_prime(bits=8):
while True:
num = random.randint(2**(bits-1), 2**bits - 1)
if isprime(num):
return num
def generate_keys():
p = generate_prime()
q = generate_prime()
n=p*q
phi = (p-1) * (q-1)
e = 65537
d = mod_inverse(e, phi)
return ((n, e), (n, d))
def encrypt(plaintext, public_key):
n, e = public_key
numeric_message = [ord(char) for char in plaintext]
ciphertext = [pow(char, e, n) for char in numeric_message]
return ciphertext
def decrypt(ciphertext, private_key):
n, d = private_key
numeric_message = [pow(char, d, n) for char in ciphertext]
[Type here] [Type here] [Type here]
plaintext = ''.join(chr(num) for num in numeric_message)
return plaintext
message = input("Enter the message you want to encrypt: ")
bits = int(input("Enter the number of bits for the key (default is 8): ") or 8)
public_key, private_key = generate_keys()
print("\nOriginal Message:", message)
ciphertext = encrypt(message, public_key)
print("Ciphertext:", ciphertext)
decrypted_message = decrypt(ciphertext, private_key)
print("Decrypted Message:", decrypted_message)
Output:
[Type here] [Type here] [Type here]
Ass. No: 2 Title: Diffie-Hellman Key Exchange and Man-in-
the-MiddleAttack
Exp. No: 4
Problem Statement
To implement the algorithms of Diffie-Hellman Key Exchange and Man-in-the-Middle Attack
using python
Problem Description
i) Diffie-Hellman Key Exchange
• Diffie-Hellman Key Exchange is a method for securely exchanging cryptographic
keys over a public channel.
• It allows two parties to generate a shared secret key without directly transmitting
it.
• The security is based on the difficulty of computing discrete logarithms.
ii) Man-in-the-Middle Attack
• A Man-in-the-Middle (MITM) attack occurs when an attacker intercepts and alters
communication between two parties.
• The attacker establishes independent connections with each party and relays
messages between them.
• This allows the attacker to decrypt, modify, or even impersonate messages.
Algorithm:
i) Diffie-Hellman Key Exchange
➢ Select a large prime number p and a primitive root g.
➢ Each participant selects a private key a and b.
➢ Compute public keys: A = g^a mod p and B = g^b mod p.
➢ Exchange public keys.
➢ Compute shared secret: S = B^a mod p for one party and S = A^b mod p for the
other.
➢ The shared secret S is used for encryption.
ii) Man-in-the-Middle Attack
➢ The attacker intercepts the prime p and base g.
➢ The attacker pretends to be each party by generating their own keys.
➢ The attacker sends their public key to both parties instead of the legitimate ones.
[Type here] [Type here] [Type here]
➢ Both parties unknowingly establish a shared secret with the attacker.
➢ The attacker can decrypt, modify, and re-encrypt messages before forwarding
them.
Program:
import random
def mod_exp(base, exp, mod):
return pow(base, exp, mod)
def diffie_hellman(p, g, private_key):
public_key = mod_exp(g, private_key, p)
return public_key
def shared_secret(public_key, private_key, p):
return mod_exp(public_key, private_key, p)
def attacker(p, g, private_key):
attacker_private_key = random.randint(1, p-1)
attacker_public_key = diffie_hellman(p, g, attacker_private_key)
return attacker_private_key, attacker_public_key
p = int(input("Enter prime number p: "))
g = int(input("Enter base g: "))
a = int(input("Enter Alice's private key: "))
b = int(input("Enter Bob's private key: "))
A = diffie_hellman(p, g, a)
B = diffie_hellman(p, g, b)
attacker_A_private, attacker_A_public = attacker(p, g, a)
attacker_B_private, attacker_B_public = attacker(p, g, b)
shared_A = shared_secret(attacker_B_public, a, p)
shared_B = shared_secret(attacker_A_public, b, p)
print("Public Key A (Alice's view):", A)
[Type here] [Type here] [Type here]
print("Public Key B (Bob's view):", B)
print("Attacker Public Key (as Alice to Bob):", attacker_A_public)
print("Attacker Public Key (as Bob to Alice):", attacker_B_public)
print("Shared Secret (Alice's perspective):", shared_A)
print("Shared Secret (Bob's perspective):", shared_B)
print("Attacker's Shared Secret:", shared_secret(attacker_A_public, attacker_B_private, p))
Output:
[Type here] [Type here] [Type here]