def generate_private_key(security_level=SECURITY_LEVEL): """ usage: generate_private_key(security_level=SECURITY_LEVEL) => private_key Returns the integer(s) that constitute a private key. """ a_i_size, b_i_size, p_size = calculate_parameter_sizes(security_level) p = random_integer(p_size) a_i = random_integer(a_i_size) b_i = random_integer(b_i_size) while gcd(a_i, p) != 1 or gcd(b_i, p) != 1: p = random_integer(p_size) a_i = random_integer(a_i_size) b_i = random_integer(b_i_size) ab_i = (a_i * b_i) % p return a_i, b_i, ab_i, p
def recover_pk(points, test_count): gcds = set() for count in range(test_count): a, b = two_points(points) gcds.add(gcd(a, b)) gcds = list(gcds) # sets do not support indexing and won't work with two_points return gcds _gcds = set() for count in range(test_count): a, b = two_points(gcds) _gcds.add(gcd(a, b)) if 1 in _gcds: _gcds.remove(1) print len(_gcds) print _gcds return min(_gcds) # the gcd of pk
def decrypt(ciphertext, key): plaintext = bytearray() for index, _ciphertext in enumerate(ciphertext): randomized1, randomized2 = _ciphertext _randomized1 = randomized1 randomized1 = choice(key[index], randomized1, randomized2) randomized2 = choice(key[index], randomized2, _randomized1) plaintext.append(INVERT_PRIMES[gcd(randomized1, randomized2)]) return plaintext
def encrypt(message, key): assert message is not 0 r1, r2 = random_bits(count=256), random_bits(count=256) while True: if gcd(r1, r2) == 1: break r1, r2 = random_bits(count=256), random_bits(count=256) c1 = (message * r1) + key c2 = (message * r2) + key return c1, c2
def encrypt(data, key, primes=PRIMES): ciphertext = [] for index, word in enumerate(bytearray(data)): randomized1 = primes[word] * primes[random_word()] randomized2 = primes[word] * primes[random_word()] assert gcd(randomized1, randomized2) == primes[word] # random_word will output word for both values every so often _randomized1 = randomized1 randomized1 = choice(key[index], randomized1, randomized2) randomized2 = choice(key[index], randomized2, _randomized1) ciphertext.append((randomized1, randomized2)) return ciphertext
def encrypt(data, key, primes=PRIMES): ciphertext = [] for index, word in enumerate(bytearray(data)): randomized1 = primes[word] * primes[random_word()] randomized2 = primes[word] * primes[random_word()] assert gcd(randomized1, randomized2) == primes[ word] # random_word will output word for both values every so often _randomized1 = randomized1 randomized1 = choice(key[index], randomized1, randomized2) randomized2 = choice(key[index], randomized2, _randomized1) ciphertext.append((randomized1, randomized2)) return ciphertext
def generate_private_key(parameters=PARAMETERS): inverse_size = parameters["inverse_size"] q_size = parameters["q_size"] while True: inverse = random_integer(inverse_size) q = random_integer(q_size) try: modular_inverse(inverse, q) except ValueError: continue else: if gcd(inverse, q) == 1: break return inverse, q
def generate_keypair(size_in_bytes, e=65537): assert e >= 3 print("Generating RSA keypair...") while True: modulus, totient = generate_random_rsa_modulus(size_in_bytes) while gcd(e, totient) != 1: modulus, totient = generate_random_rsa_modulus(size_in_bytes) try: d = modular_inverse(e, totient) except ValueError: # the prime test is probabilistic continue else: print("...done") return e, d, modulus
def generate_random_keypair(size_in_bytes): print("Generating keypair...") while True: prime = big_prime(size_in_bytes) e = random_integer(size_in_bytes) totient = prime - 1 while e >= prime and gcd(e, totient) != 1: e = random_integer(size_in_bytes) try: d = modular_inverse(e, totient) except ValueError: # the prime test is probabilistic continue else: print("...done") return e, d, prime
def generate_keypair_for_e(size_in_bytes, e=None): if e is None: raise ValueError("e not supplied") print("Generating keypair...") while True: prime = big_prime(size_in_bytes) totient = prime - 1 while e >= prime and gcd(e, totient) != 1: prime = big_prime(size_in_bytes) try: d = modular_inverse(e, totient) except ValueError: # the prime test is probabilistic continue else: print("...done") return e, d, prime
def decrypt(ciphertext, key): c1, c2 = ciphertext c1 -= key c2 -= key return gcd(c1, c2)
def generate_key(p1_size=4, p2_size=2): p1 = random_integer(p1_size) p2 = random_integer(p2_size) while gcd(p1, p2) != 1: p2 = random_integer(p2_size) return p1, p2
def gcd_test(p, q): if gcd(p * q, (p - 1) * (q - 1)) == 1: return True else: return False