- Code: Select all
`import random, math`

def encrypt():

message = input("Enter message to encrypt: ")

myl = []

for x in message:

myl.append(ord(x))

primeNums = []

for x in range(2,100):

for y in range(2,x):

if x%y == 0:

break

else:

primeNums.append(x)

p = primeNums[random.randint(0,len(primeNums)-1)]

q = primeNums[random.randint(0,len(primeNums)-1)]

N = p*q

f = (p-1)*(q-1)

for e in range(2,f):

for h in range(2,e):

if e%h == 0:

break

else:

if f%e == 1:

break

print("Public Key 1:",N)

print("Public Key 2:",e)

for a in myl:

C = (a**e)%N

print(C,end=" ")

def decrypt():

N = eval(input("Enter public key 1: "))

e = eval(input("Enter public key 2: "))

message = input("Enter message to decrypt: ")

message = message.split()

for q in range(2,int(math.floor(math.sqrt(N)))+1):

for a in range(2,q+1):

for b in range(2,a):

if a%b == 0:

break

else:

if a == q:

p = N//q

if p*q == N:

break

f = (p-1)*(q-1)

d = 0

x = 1

while True:

if (e*x)%f == 1:

d = x

break

x += 1

for g in message:

decryption = (int(g)**d)%N

print(chr(decryption),end="")

You can refer to: http://mathcircle.berkeley.edu/BMC3/rsa/node4.html to learn more about RSA encryption, I have used pretty much the same variable names and everything.