This is just a toy project i wrote for fun.
You can use this for studiyng RSA, but if you want proper RSA encryption in your project, use a cryptography library.
from math import gcd
def prime(x):
count = 0
for i in range(int(x/2)):
if x % (i+1) == 0:
count = count+1
return count == 1
def carmi(n):
copr = [x for x in range(1, n) if gcd(x, n) == 1]
k = 1
while not all(pow(x, k, n) == 1 for x in copr):
k += 1
return k
primes = [i for i in range(100,1000) if prime(i)]
# two primes
p = random.choice(primes)
q = random.choice(primes)
# modulus
n = p*q
# λ(n)
c = carmi(n)
# e integer (public exp)
e = random.choice([i for i in range(2, c - 2) if gcd(i,c) == 1]);
# d integer (private exp)
d = [i for i in range(1,c) if (1 == (e * i) % c)][0];
# test, assuming 5 is a secret message
a = 5;
# encrypt
a1 = (a**e) % n;
# decrypt a1 to a2,
# it should be 5
a2 = (a1**d) % n;
if (a2 == a): print('it workd =)');
else: print('something gone wrong');