Ejemplo n.º 1
0
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
Ejemplo n.º 2
0
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
Ejemplo n.º 3
0
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
Ejemplo n.º 4
0
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   
Ejemplo n.º 5
0
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
Ejemplo n.º 6
0
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
Ejemplo n.º 7
0
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
Ejemplo n.º 8
0
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
Ejemplo n.º 9
0
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
Ejemplo n.º 10
0
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
Ejemplo n.º 11
0
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
Ejemplo n.º 12
0
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
Ejemplo n.º 13
0
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
Ejemplo n.º 14
0
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
Ejemplo n.º 15
0
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
Ejemplo n.º 16
0
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
Ejemplo n.º 17
0
def decrypt(ciphertext, key):
    c1, c2 = ciphertext
    c1 -= key
    c2 -= key
    return gcd(c1, c2)
Ejemplo n.º 18
0
def decrypt(ciphertext, key):
    c1, c2 = ciphertext
    c1 -= key
    c2 -= key
    return gcd(c1, c2)
Ejemplo n.º 19
0
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
Ejemplo n.º 20
0
def gcd_test(p, q):
    if gcd(p * q, (p - 1) * (q - 1)) == 1:
        return True
    else:
        return False
Ejemplo n.º 21
0
def gcd_test(p, q):
    if gcd(p * q, (p - 1) * (q - 1)) == 1:
        return True
    else:
        return False