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

(In Pyhton): Write a function generateRSAKeys(p, q) that returns the public key

ID: 3812712 • Letter: #

Question

(In Pyhton): Write a function generateRSAKeys(p, q) that returns the public key (n, e) and the private key d, given parameters for the two prime factors p and q. Your function can assume that the arguments will be primes. The function can find a value for e by starting at 3 and incrementing it by 2 until it finds a value where e is relatively prime to – that is, gcd(e, ) = 1. (Why does it make sense that even values for e need not be checked?) The return value should be a list of two elements containing one list (the public key) and one integer (the private key).

Explanation / Answer

You can run it in Python 3.6

from random import randint #for generating the value of k
import math#for gcd


def generateRSAkey(p,q):
n=p*q
phin=(p-1)*(q-1) # this is phi of n
#now e need to calculate e
e=2
while(e<phin):
if(math.gcd(e,phin)==1):
break
e+=1
  
#let k be a random integer
k=randint(1,100)
d=1+k*phin

return [[n,e],d]

#sample test
print(generateRSAkey(13,17))

Output: [[221, 5], 4225]