Esempio n. 1
0
def gen_params():
    p_len = int(input('Длина p (биты): '))
    q_len = int(input('Длина q (биты): '))
    p = random.randint(2 ** (p_len - 1), 2 ** p_len)
    q = random.randint(2 ** (q_len - 1), 2 ** q_len)
    while not prime_test.miller_rabin(p):
        p = random.randint(2 ** (p_len - 1), 2 ** p_len)
    while not prime_test.miller_rabin(q) or p == q:
        q = random.randint(2 ** (q_len - 1), 2 ** q_len)
    with open('pq.txt', 'w') as fout_pq:
        fout_pq.write(str(p) + ' ' + str(q))

    n = p*q
    phi = (p-1)*(q-1)

    while True:
        e = random.randint(10, phi-1)
        if gcd(e, phi) == 1:
            break
    with open('pub_key.txt', 'w') as fout_public:
        fout_public.write(str(n) + ' ' + str(e))

    with open('n_phi.txt', 'w') as fout_n:
        fout_n.write(str(n) + ' ' + str(phi))

    d = inverse(e, phi)
    with open('sec_key.txt', 'w') as fout_secret:
        fout_secret.write(str(d))
    print('Сохранен файл pub_key.txt')
    print('Сохранен файл n_phi.txt')
    print('Сохранен файл sec_key.txt')
Esempio n. 2
0
def trial_division_mode():
    mod1 = int(input('1 - создание базы\n' '2 - метод пробного деления\n> '))
    if mod1 == 1:
        base = list(map(int, open('base.txt', 'r').read().split()))
        t = int(input('Введите t: '))
        ans = trial_division.make_base(base, t)
        with open('trial_base.txt', 'w') as fout:
            for item in ans:
                fout.write(str(item) + ' ')
        print('Сгенерированная база:', ans)
        print('Сохранен файл trial_base.txt')

    if mod1 == 2:
        base = list(map(int, open('base.txt', 'r').read().split()))
        trial_base = list(map(int, open('trial_base.txt', 'r').read().split()))
        n = int(input('Введите нечетное число: '))
        assert n % 2 != 0, "Четное число"
        assert not prime_test.miller_rabin(n), "Простое число"
        assert sqrt(n) <= trial_base[len(trial_base) - 1], "Недостаточная база"

        ans = trial_division.decomposition(n, base, trial_base)
        ans = sorted(ans)
        print(ans)
        with open('Ответ для пробного деления.txt', 'w') as fout:
            for item in ans:
                fout.write(str(item) + ' ')
        print('Сохранен файл Ответ для пробного деления.txt')
Esempio n. 3
0
def make_base(l):
    base = [2, 3, 5]
    tmp = 7
    while len(base) != l:
        if prime_test.miller_rabin(tmp):
            base.append(tmp)
        tmp += 1
    return base
Esempio n. 4
0
def make_base_dixon(n):
    base = [2, 3, 5]
    p = int(sqrt(exp(sqrt(log(n) * log(log(n))))))
    tmp = 7
    while tmp <= p:
        if prime_test.miller_rabin(tmp):
            base.append(tmp)
        tmp += 1
    base = [-1] + list(filter(lambda x: legendre_symbol(n, x) == 1, base))
    return base
Esempio n. 5
0
def produce_primes(binary_bits):
    """产生指定位数的随机数"""
    while True:
        random.seed()#改变随机数种子
        odd = random.getrandbits(binary_bits)#步长为2,产生一个随机奇数
        if len(bin(odd))-2 != binary_bits:
            continue
        if prime_test.fast_prime_test(odd) == False:#先快速判断一下是否为素数
            continue
        is_prime = prime_test.miller_rabin(odd)#miller_rabin算法素性测试
        if is_prime == True:
            return odd
        elif is_prime == False:
            continue
Esempio n. 6
0
File: RSA.py Progetto: fireboy38/RSA
def produce_primes(decimal_bits):
    """产生指定位数的随机数"""
    start = 10 ** (decimal_bits-1) + 1
    stop = 10 ** (decimal_bits+1) - 1
    
    while True:
        random.seed()#改变随机数种子
        odd = random.randrange(start,stop,2)#步长为2,产生一个随机奇数
        
        if prime_test.fast_prime_test(odd) == False:#先快速判断一下是否为素数
            continue
        is_prime = prime_test.miller_rabin(odd)#miller_rabin算法素性测试
        if is_prime == True:
            return odd
        elif is_prime == False:
            continue
Esempio n. 7
0
def produce_primes(decimal_bits):
    """产生指定位数的随机素数"""
    begin = 10 ** (decimal_bits -1) + 1 # like: 1000000000000000001
    end = 10 ** (decimal_bits + 1) - 1  # like: 99999999999999999999

    while True:
        random.seed()
        #产生一个指定范围的随机奇数(伪随机数)
        odd = random.randrange(begin, end, 2)
        print("随机产生的素数----",odd)
        #先用快速法判断一下,如果不是素数,则重新产生一个奇数
        if prime_test.fast_prime_test(odd) == False:
            continue
        #对奇数的素性测试
        is_prime = prime_test.miller_rabin(odd)
        if is_prime == True:
            return odd
        elif is_prime == False:
            continue