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

Rsa Exp-3

The document describes an RSA encryption/decryption lab experiment. It includes code to check if numbers are prime, find the greatest common divisor, and calculate the inverse to decrypt a ciphertext. The code takes in plaintext and encryption/decryption keys to encrypt and decrypt messages.

Uploaded by

imprecisehammer
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)
29 views3 pages

Rsa Exp-3

The document describes an RSA encryption/decryption lab experiment. It includes code to check if numbers are prime, find the greatest common divisor, and calculate the inverse to decrypt a ciphertext. The code takes in plaintext and encryption/decryption keys to encrypt and decrypt messages.

Uploaded by

imprecisehammer
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

Course Title: Cryptography and Course Code: BCSE309P

Network Security Lab


Faculty: Prof. Karthika V Slot: L11-L12
Regno: 21BCE1881 Name: Shrraddhas Mishra

EX-3 RSA

Code
def is_prime(n): count

=0

for i in range(1, n):


if(n%i == 0): count

+= 1

if(count > 1): return False return


True def gcd(a, b): result = min(a, b)

while result: if a % result == 0 and b


% result == 0:

break
result -= 1

return result

def find_inverse(e , mod):


ans = 1 while(True):
if((e * ans) % mod
== 1):
break ans

+= 1

return ans

p = int(input("first prime number: ")) q


= int(input("second prime number: "))

m = int(input("plain text: "))

if(not is_prime(p) or not is_prime(q)):


print("The numbers p and q are not prime")

exit(0)

n = p * q phi = (p -
1) * (q - 1)

e = -1 for i in range(2
, phi): if(gcd(i , phi)

== 1):

e=i

break

cypher_text = (pow(m , e) % n) decryption_key


= find_inverse(e , phi)

print("public: ", e) print("cypher: " ,

cypher_text) print("decryption: " ,


decryption_key)
OUTPUT:

You might also like