Exemplo n.º 1
0
def generate_n(n_size):    
    # ensures N is exactly the size it is supposed to be
    factor_size = n_size / 2
    most_significant_bit = 1 << (factor_size * 8) 
    while True:
        p = random_integer(factor_size) | most_significant_bit
        if is_prime(p):
            break
    while True:
        q = random_integer(factor_size) | most_significant_bit
        if is_prime(q):
            break    
    n = p * q
    return n
Exemplo n.º 2
0
def generate_n(n_size):
    # ensures N is exactly the size it is supposed to be
    factor_size = n_size / 2
    most_significant_bit = 1 << (factor_size * 8)
    while True:
        p = random_integer(factor_size) | most_significant_bit
        if is_prime(p):
            break
    while True:
        q = random_integer(factor_size) | most_significant_bit
        if is_prime(q):
            break
    n = p * q
    return n
Exemplo n.º 3
0
def find_p(security_level=SECURITY_LEVEL):
    from crypto.utilities import is_prime
    p = 2 ** (security_level * 8)
    offset = 1
    while not is_prime(p + offset):
        offset += 2
    return p, offset
Exemplo n.º 4
0
def find_q(q_size=Q_SIZE):
    from crypto.utilities import is_prime
    q = 2 ** (q_size * 8)
    offset = 1
    while not is_prime(q + offset):
        offset += 2
    return q, offset
Exemplo n.º 5
0
def find_p(parameters):
    from crypto.utilities import is_prime
    p_size = parameters["p_size"]
    p = 2 ** ((p_size * 8) + 1)    
    offset = 1
    while not is_prime(p + offset):
        offset += 2
    return p, offset
Exemplo n.º 6
0
def find_q(security_level=SECURITY_LEVEL):
    from crypto.utilities import is_prime
    q_size = security_level * 3
    offset = 1
    q = (2 ** (q_size * 8)) 
    while not is_prime(q + offset):
        offset += 2                
    return q, offset
Exemplo n.º 7
0
def find_q(q_size):
    from crypto.utilities import is_prime
    q_size *= 8
    q = 2**q_size
    offset = 1
    while not is_prime(q + offset):
        offset += 2
    return q, offset
Exemplo n.º 8
0
def find_q(q_size):
    from crypto.utilities import is_prime
    q_size *= 8
    q = 2 ** q_size
    offset = 1
    while not is_prime(q + offset):
        offset += 2
    return q, offset
Exemplo n.º 9
0
def generate_private_key(parameters=PARAMETERS):
    ab_size = parameters["ab_size"]
    while True:
        a = random_integer(ab_size)
        b = random_integer(ab_size)
        if not is_prime(pow(a, 2) + pow(b, 2)):
            break
    return a, b
Exemplo n.º 10
0
def find_q(security_level=SECURITY_LEVEL):
    from crypto.utilities import is_prime
    q_size = security_level * 3
    offset = 1
    q = (2**(q_size * 8))
    while not is_prime(q + offset):
        offset += 2
    return q, offset
Exemplo n.º 11
0
def generate_private_key(parameters=PARAMETERS):
    ab_size = parameters["ab_size"]
    while True:
        a = random_integer(ab_size)
        b = random_integer(ab_size)
        if not is_prime(pow(a, 2) + pow(b, 2)):
            break
    return a, b
Exemplo n.º 12
0
def find_q(parameters=PARAMETERS):
    from crypto.utilities import is_prime
    q_size = parameters["q_size"]
    offset = 1
    q_base = (2 ** q_size)
    while not is_prime(q_base + offset):
        offset += 2
    return q_base, offset
Exemplo n.º 13
0
def find_q(parameters=PARAMETERS):
    from crypto.utilities import is_prime
    q_size = parameters["q_size"]
    offset = 1
    q_base = (2**q_size)
    while not is_prime(q_base + offset):
        offset += 2
    return q_base, offset
Exemplo n.º 14
0
def find_p(parameters):
    from crypto.utilities import is_prime
    p_size = parameters["p_size"]
    p = 2**((p_size * 8) + 1)
    offset = 1
    while not is_prime(p + offset):
        offset += 2
    return p, offset
Exemplo n.º 15
0
def generate_private_key(parameters=PARAMETERS):
    ab_size = parameters["ab_size"]
    a = random_integer(ab_size)
    while True:        
        b = random_integer(ab_size)
        if is_prime(pow(a, 2) + pow(b, 2)):
            break
    print("Generated a, b")
    c = random_integer(ab_size)
    while True:
        d = random_integer(ab_size)
        if is_prime(pow(c, 2) + pow(d, 2)):
            break    
    print("Generated c, d")
    x = (a * c) - (b * d)
    y = (a * d) + (b * c)
    
    assert (pow(x, 2) + pow(y, 2)) == (pow(a, 2) + pow(b, 2)) * (pow(c, 2) + pow(d, 2))
    return x, y
Exemplo n.º 16
0
def generate_public_key(p, q_size=32):
    q = big_prime(q_size)
    N = quicksum(p * q)
    #assert not( N % p != 0 or quicksum(N) % p != 0)
    while  N % p != 0 or quicksum(N) % p != 0:
        q = random_integer(q_size)
        N = quicksum(p * q)        
    assert is_prime(q)
    assert N % p == 0, (N % p)
    assert quicksum(N) % p == 0
    return N
Exemplo n.º 17
0
def generate_public_key(p, q_size=32):
    q = big_prime(q_size)
    N = quicksum(p * q)
    #assert not( N % p != 0 or quicksum(N) % p != 0)
    while N % p != 0 or quicksum(N) % p != 0:
        q = random_integer(q_size)
        N = quicksum(p * q)
    assert is_prime(q)
    assert N % p == 0, (N % p)
    assert quicksum(N) % p == 0
    return N
Exemplo n.º 18
0
def test_break():
    N = 65537#big_prime(3)
    assert is_prime(N)    
    p_i = 256
    p = modular_inverse(p_i, N)
    
    q = random_integer(1)
    e = random_integer(1)
    c = ((p * q) + e) % N
    print q, e, p_i * e < N
    _q, _e = break_qtpie(p, c, N)
    assert (q, e) == (_q, _e), ((q, e), (_q, _e))
    assert _e == e, (e, _e)