1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| from random import randint import sympy import sys with open('C:\\Users\\吴文之\\Desktop\\验收数据\\plaintext4.txt') as file_object: lines = file_object.readlines() m = int(lines[0]) def fast(a,e,m): a = a % m r = 1 while e != 0: if e&1: r= (r*a)%m e >>= 1 a = (a * a)%m return r def primitive(p, q): while True: g = randint(2,p-2) if fast(g,2,p) != 1 and fast(g,q,p) != 1: return g def e_gcd(a,b): if b == 0: return a,1,0 g,x,y = e_gcd(b,a%b) return g,y,x-a//b*y while True: q = sympy.randprime(pow(10,149),pow(10,150)/2-1) if sympy.isprime(q): p = 2 * q + 1 if sympy.isprime(p) and len(str(p)) == 150: break print("q = " + str(q)) print("p = " + str(p)) g = primitive(p, q) print("g = "+str(g)) a = randint(1,11) print("公钥对: (p,g,g^a) = "+"\n"+str(p)+"\n"+str(g)+"\n"+str(pow(g,a)%p)) y = fast(g,a,p)
k = randint(2, p-2) c1 = fast(g, k, p) c2 = (m*fast(y,k,p))%p print("c1 = " + str(c1)) print("c2 = " + str(c2))
v = fast(c1,a,p) v_ = e_gcd(v,p)[1] m_ = c2*v_%p if(m == m_): print("相同") else: print("不同")
|