0% found this document useful (0 votes)
10 views3 pages

Ins 9

ins practical
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)
10 views3 pages

Ins 9

ins practical
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/ 3

Faculty of Engineering & Technology

Information and Network Security (203105311)


B. Tech CSE 4th Year 7th Semester

PRACTICAL: 09

AIM: - Implement RSA encryption-decryption algorithm.

THEORY:
❖ What is RSA Algorithm? :
• The RSA algorithm is a widely used public-key
cryptosystem that enables secure data transmission. It was
developed by Ron Rivest, Adi Shamir, and Leonard
Adleman in 1977. RSA is based on the mathematical
properties of prime numbers and the difficulty of factoring
large composite numbers. It uses two keys: a public key for
encryption and a private key for decryption. The encryption
process involves raising the message to the power of the
public key, and decryption requires using the private key.
RSA ensures data confidentiality and is used in various
applications like secure communication, digital signatures,
and online transactions.

❖ Steps:
1) Select two prime number p and Q
p= 3 q=11
2) Calculate n=p*q
n=3*11=33

3) Calculate Φ(n)=(p-1)*(q-1)

Φ(n)=(3-1)*(11-1)=2*10=20

4) Select e such that e is relatively prime to Φ(n)


( e,Φ(n) ) = 1 and 1<e<Φ(n)
(7,20)=1
q r1 r2 r
2 20 7 6
1 7 61
6 6 1 0
1 0
Enrollment No.: 210303105082
Name: Manthan Patel
P a g e | 39
Div: 7B25
Faculty of Engineering & Technology
Information and Network Security (203105311)
B. Tech CSE 4th Year 7th Semester

5) Calculate d=e^-1 mod Φ(n) or ed=1 mod Φ(n)


7*d mod 20 = 1 but answer is 7
7*7 mod 20 = 14
21 mod 20 = 1

6) Public key = {e, n}


{7, 33}
Private key = {d, n}
{3, 33}

7) Find out cipher text using formula


Encryption =p^e mod n
=2^7 mod 33
=29

8) P= c^d mod n
Decryption = c^d mod 33
=29^3 mod 33
=2

CODE:-

def encrypt(public_key, message):

e, n = public_key
return [pow(ord(char) - ord('A'), e, n) for char in message]

def decrypt(private_key, ciphertext):


d, n = private_key
return ''.join([chr(pow(char, d, n) + ord('A')) for char in
ciphertext])

p = int(input("Enter a prime number (p): "))


q = int(input("Enter another prime number (q): "))
message = input("Enter message in uppercase letters (A-Z):
").upper()

Enrollment No.: 210303105082


Name: Manthan Patel
P a g e | 40
Div: 7B25
Faculty of Engineering & Technology
Information and Network Security (203105311)
B. Tech CSE 4th Year 7th Semester

public_key, private_key = generate_keys(p, q)

ciphertext = encrypt(public_key, message)


print("Encrypted message:", ciphertext)

decrypted = decrypt(private_key, ciphertext)


print("Decrypted message:", decrypted

Output:

Enrollment No.: 210303105082


Name: Manthan Patel
P a g e | 41
Div: 7B25

You might also like