# Python RSA implementation import lib.arithmetics as arithm import lib.miller_rabin as miller def get_p_and_q(n: int) -> (int, int): # Get two random prime numbers p = miller.get_random_prime(n) q = miller.get_random_prime(n) # Ensure the two numbers are differents while p == q: q = get_random_prime(n) return (p, q) def get_keys(l: int) -> int: # Get p and q p, q = get_p_and_q(l // 2) # Compute n and phy(n) n = p * q phy_n = (p - 1) * (q - 1) # Chose e = 65537 e = 65537 # Ensure e fits with the others numbers while arithm.gcd(e, phy_n) != 1: p, q = get_p_and_q(l // 2) n = p * q phy_n = (p - 1) * (q - 1) # Compute d d = arithm.mod_inverse(e, phy_n) # Return e, d and n return (e, d, n) def encrypt(m: int, e: int, n: int) -> int: return arithm.modpow(m, e, n) def decrypt(c: int, d: int, n: int) -> int: return arithm.modpow(c, d, n)