Exemplo n.º 1
0
def get_key(n=20):
    """
    获取公钥 p, g , (g^x)mod p 和私钥 x
    :param n: the long of random prime
    :return: p,g,y,x
    """
    import pyunit_prime
    p = pyunit_prime.get_large_prime_length(n)
    # p = 49924286347363285385002602166419178756593347008815637398138749880304666805251999
    yn = p - 1
    f = 2
    x_list = []
    while yn != 1:
        print('yn:', yn)
        print('f:', f)
        print(pyunit_prime.is_prime(int(yn)))
        if pyunit_prime.is_prime(int(yn)):
            x_list.append(int(yn))
            break
        if pyunit_prime.is_prime(f):
            if yn % f == 0:
                x_list.append(f)
                while yn % f == 0:
                    yn = yn // f
            if yn == 1:
                break
            f += 1
        else:
            f += 1

    yn = p - 1
    print('yn:', yn)
    print('x list:', x_list)
    error_list = []
    import random
    while True:
        i = random.randint(2, p)
        if i in error_list:
            continue
        else:
            error_list.append(i)
        k = 0
        for j in x_list:
            if fastExpMod(i, int(yn // j), p) == 1:
                break
            else:
                k += 1
        if k == len(x_list):
            import random
            x = random.randint(1, p - 1)

            return p, i, fastExpMod(i, x, p), x
Exemplo n.º 2
0
def primeFactorization(length):  #分解质因数
    global p, q
    q = get_large_prime_length(length)
    while True:
        d = random.randint(2, 10000)
        if d % 2 == 0:
            p = q * d + 1
            if is_prime(p) == True:
                break
            else:
                continue
        else:
            continue
    primeList = prime_range(2, int(math.sqrt(d)))
    result = [[0, 0] for i in range(len(primeList))]
    for i in range(len(primeList)):
        result[i][0] = primeList[i]
        while d % primeList[i] == 0:
            result[i][1] += 1
            d = d // primeList[i]
    if d != 1:
        result.append([d, 1])
    result.append([q, 1])
    #     请问这个result列表的作用是什么?
    return result
Exemplo n.º 3
0
def primeFactorization(k):  #分解质因数
    while True:
        q = get_large_prime_length(length)
        p = (q * k + 1) if (k % 2 == 0) else (q * (k + 1) + 1)
        if is_prime(p) == True:
            break
        else:
            continue
    primeList = prime_range(2, int(math.sqrt(k)))
    result = [[0, 0] for i in range(len(primeList))]
    for i in range(len(primeList)):
        result[i][0] = primeList[i]
        while k % primeList[i] == 0:
            result[i][1] += 1
            k = k // primeList[i]
    if k != 1:
        result.append([k, 1])
    result.append([q, 1])
    return result, p, q
Exemplo n.º 4
0
length = 128
global p,q,g,n,i,SKlist,PKlist,rlist,CHpublic,CHprivate,r1
p,q,g = 0,0,0
ChSign_public_key, ChSign_private_key = myrsa.newkeys()
global KeySPEC
KeySPEC=secp256k1.make_keypair()
global public_value,tn_scrtklist,tn_pubklist,value_scale,t_value,tn_users,total_CH_S,total_CH_PB
global secret_poly_list,result_list,Si_list
public_value,tn_scrtklist,tn_pubklist = [],[],[]
secret_poly_list,result_list,Si_list = [],[],[]
value_scale = 100
random.randint(1,1000)
primelist = []
for i in range(1000):
    if is_prime(i)==True:
        primelist.append(i)
ChSign_public_key, ChSign_private_key = myrsa.newkeys()

def primeFactorization(k):#分解质因数
    while True:
        q=get_large_prime_length(length)
        p=(q*k+1)if(k%2==0)else(q*(k+1)+1)
        if is_prime(p)==True:
            break
        else:
            continue
    primeList=prime_range(2,int(math.sqrt(k)))
    result=[[0,0] for i in range(len(primeList))]
    for i in range(len(primeList)):
        result[i][0]=primeList[i]
Exemplo n.º 5
0
def rm():
    for i in range(100000):
        if is_prime(i):
            print(i)