def factorize(number): arr = [] gen = generate_primes() prime = next(gen) while number >= prime: if number % prime == 0: arr.append(prime) number /= prime continue else: prime = next(gen) return arr
def RSA_keygen(n=512): """ Perform steps 1. to 5. in the RSA Key Generation process. """ # step 1 import generate_primes p = generate_primes.generate_primes(n=n, k=1)[0] q = generate_primes.generate_primes(n=n, k=1)[0] # step 2 n = p * q # step 3 phi_n = (p - 1) * (q - 1) # step 4 and step 5 while True: e = random.randrange(1, phi_n - 1) if math.gcd(e, phi_n) == 1: # step 5 gcd, s, t = eea.EEA(phi_n, e) if gcd == (s * phi_n + t * e): d = t % phi_n break return (e, n, d)
def rsa(bits=512, e=pow(2, 16) + 1): r""" >>> e, d, n = rsa() >>> message = 109 >>> encrypted_message = encrypt(e, n, message) >>> decrypted_message = decrypt(d, n, encrypted_message) >>> message == decrypted_message True """ gcd = None while gcd != 1: p, q = generate_primes(bits=bits, n_primes=2) n = p * q phi = (p - 1) * (q - 1) d, k, gcd = extended_gcd(phi, e) while d < 0 and gcd == 1: d += phi return e % n, d % n, n
def is_prime(n): """Calculate if n is prime or not""" if n < 2: return False if n < 4: return True try: return manager.get(n) except KeyError: pass # Continue on to the rest of the function value = True for i in gp.generate_primes(end=int(sqrt(n))): if n % i == 0: value = False break manager.add(n, value) return value