Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(In Python) Write a program that uses all of your functions from above to carry

ID: 3147904 • Letter: #

Question

(In Python) Write a program that uses all of your functions from above to carry out an RSA key generation, encryption, and decryption. Your program should get user input for two primes pand q, as well as a plaintext integer M. Display the RSA public and private keys that are generated. Show the ciphertext Cthat results from encrypting Musing the public key. Finally, show the result from decrypting Cusing the private key. This decrypted output should match the original plaintext M.You can use the list of primes at http://primes.utm.edu/lists/small/1000.txtto help you test your program.Python tip:You can use the input()function like Java’s Scannerclass. By default, Python treats the entered information as a string, but you can call the int()function to convert it to an integer. Example:x = int(input("Enter an integer: "))# x is now storing an int valueprint("You entered " + str(x))

Explanation / Answer

import random
from fractions import gcd
def generate_public(phi):
#selecting the e such that 1 < e <phi and gcd(e,phi)= 1
e = random.randrange(1,phi)
coprime =gcd(phi,e)
while (coprime >1):
e = random.randrange(1,phi)
coprime =gcd(phi,e)
return e;

def generate_private(Pub_key,phi):
for i in range(1,phi):
if((Pub_key * i)% phi == 1):
return i
return

def encryption(M,e,n):
#encrypted text
ET = pow(M,e) % n
return ET

def decryption(C,d,n):
#plain text
PT = (pow(C,d)) % n
return PT

p = int(input("Enter P = "))
q = int(input("Enter Q = "))
M = int(input("Enter plain text M = "))

n = p*q
phi = (p-1)*(q-1)
Pub_key = generate_public(phi)
Pri_key = generate_private(Pub_key,phi)
print "Public key = "+str(Pub_key)
print "Private key = "+str(Pri_key)

Cipher_text = encryption(M,Pub_key,n)
print "Cipher_text = "+str(Cipher_text)

Plain_text = decryption(Cipher_text,Pri_key,n)
print "Decipher_text = "+str(Plain_text)