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

Rsa

The document demonstrates RSA encryption and decryption. It takes in prime numbers p and q from the user to generate the public and private keys, n and phi. It then finds e and d, the public and private exponents. Finally it encrypts and decrypts a message using the keys.

Uploaded by

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

Rsa

The document demonstrates RSA encryption and decryption. It takes in prime numbers p and q from the user to generate the public and private keys, n and phi. It then finds e and d, the public and private exponents. Finally it encrypts and decrypts a message using the keys.

Uploaded by

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

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

q = int(input("Enter a prime number Q:"))


message = int(input("Enter a msg:"))
n = p * q
phi = (p - 1) * (q - 1)

print(n)
print(phi)
e = 0 # public key
d = 0 # private key

# for e
for i in range(1, phi):
if (phi % i != 0) and (i != p) and (i != q):
e = i
break

print(f"E:{e}")

# for d
for i in range(1, phi):
x = (1 + (i * phi)) / e
# print(x)
f = x % 1 # to get the float value

if f == 0.0 and x != e and x != p and x != q:


d = int(x)
break

print(f"D:{d}")

# encrypt
def encrypt(msg):
return pow(msg, e, n)

#decrypt
def decrypt(cipher):
return pow(cipher, d, n)

if e and d:
cipher = encrypt(message)
print(f"Cipher Text:{cipher}")
pt = decrypt(cipher)
print(f"Plain Text:{pt}")

You might also like