CSS_EXP_03
CSS_EXP_03
Aim: To implement and analyze the RSA Cryptosystem and Diffie-Hellman Key Exchange
Algorithm.
Theory:
RSA Cryptosystem:
RSA is one of the most widely used methods for secure communication. It's an example of
asymmetric cryptography, which means it uses two different keys:
Key Generation:
● Encryption:
○ Ciphertext C = (M^e) mod n
● Decryption:
○ Message M = (C^d) mod n
Diffie-Hellman is not an encryption method like RSA, but a way for two people to agree on a
secret key that they can use for encryption — even if they're communicating over an insecure
channel.
● Steps:
1. Agree on a large prime number p and a primitive root g.
2. User A selects a secret number a, computes A = g^a mod p.
3. User B selects a secret number b, computes B = g^b mod p.
4. Exchange A and B.
5. Both compute the shared key:
6. A computes (B^a mod p), B computes (A^b mod p) — both are equal.
Input and Output
Input:-
RSA
from math import gcd
def RSA(p: int, q: int, message: int):
n=p*q
t = (p - 1) * (q - 1)
p = int(input("Enter p: "))
q = int(input("Enter q: "))
message = int(input("Enter the message: "))
RSA(p, q, message)
Output:
Diffie Hellman
def calculate_power(base, exponent, modulus):
if exponent == 1:
return base
else:
return pow(base, exponent) % modulus
if __name__ == "__main__":
prime = int(input("Enter Prime number: "))
generator = int(input("Enter generator: "))
private_key_alice = int(input("Enter key (Alice): "))
private_key_bob = int(input("Enter key (Bob): "))
Output:
Virtual Labs :
Conclusion: