0% found this document useful (0 votes)
7 views

CSS_EXP_03

The document outlines the implementation and analysis of the RSA Cryptosystem and Diffie-Hellman Key Exchange Algorithm. It explains the key generation process for RSA and the steps involved in the Diffie-Hellman method for establishing a shared secret key. The conclusion emphasizes the importance of these cryptographic techniques in securing online communications.

Uploaded by

Atharva Deore
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

CSS_EXP_03

The document outlines the implementation and analysis of the RSA Cryptosystem and Diffie-Hellman Key Exchange Algorithm. It explains the key generation process for RSA and the steps involved in the Diffie-Hellman method for establishing a shared secret key. The conclusion emphasizes the importance of these cryptographic techniques in securing online communications.

Uploaded by

Atharva Deore
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Experiment No.

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:

●​ A public key (shared with everyone)


●​ A private key (kept secret)

Key Generation:

○​ Choose two large prime numbers, p and q.


○​ Compute n = p × q and φ(n) = (p−1)(q−1).
○​ Choose e such that 1 < e < φ(n) and gcd(e, φ(n)) = 1.
○​ Compute d such that (d × e) % φ(n) = 1.
○​ Public key = (e, n), Private key = (d, n).​

●​ Encryption:
○​ Ciphertext C = (M^e) mod n​

●​ Decryption:
○​ Message M = (C^d) mod n

Diffie-Hellman Key Exchange:

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)

for i in range(2, t):


if gcd(i, t) == 1:
e=i
break
j=0
while True:
if (j * e) % t == 1:
d=j
break
j += 1
ct = (message ** e) % n
print(f"Encrypted message is {ct}")
mes = (ct ** d) % n
print(f"Decrypted message is {mes}")

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): "))

x = calculate_power(generator, private_key_alice, prime)


y = calculate_power(generator, private_key_bob, prime)

secret_key_alice = calculate_power(y, private_key_alice, prime)


secret_key_bob = calculate_power(x, private_key_bob, prime)

print("Prime number (P):", prime)


print("Generator value (G):", generator)
print("Alice's private key (a):", private_key_alice)
print("Bob's private key (b):", private_key_bob)
print("Secret key for Alice:", secret_key_alice)
print("Secret key for Bob:", secret_key_bob)

Output:

Virtual Labs :
Conclusion:

The implementation of RSA and Diffie-Hellman demonstrates fundamental cryptographic


operations. RSA ensures confidentiality using public-key encryption, while Diffie-Hellman
securely establishes a shared secret between parties. Both are critical components of modern
cryptographic systems used in securing online communications.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy