DSA python

import hashlib
def Euclid(a,b):
    x1 = 1
    x2 = 0
    x3 = a
    y1 = 0
    y2 = 1
    y3 = b

    while y3 != 0 :
        q = int(x3/y3)
        t1 = x1-q*y1
        t2 = x2-q*y2
        t3 = x3-q*y3

        x1 = y1
        x2 = y2
        x3 = y3

        y1 = t1
        y2 = t2
        y3 = t3

        if x1<0:
            x1 = x1+b
    return x1

p = 0x800000000000000089e1855218a0e7dac38136ffafa72eda7859f2171e25e65eac698c1702578b07dc2a1076da241c76c62d374d8389ea5aeffd3226a0530cc565f3bf6b50929139ebeac04f48c3c84afb796d61e5a4f9a8fda812ab59494232c7d2b4deb50aa18ee9e132bfa85ac4374d7f9091abc3d015efc871a584471bb1
q = 0xf4f47f05794b256174bba6e9b396a7707e563c5b
g = 0x5958c9d3898b224b12672c0b98e06c60df923cb8bc999d119458fef538b8fa4046c8db53039db620c094c9fa077ef389b5322a559946a71903f990f1f7e0e025e2d7f7cf494aff1a0470f5b64c36b625a097f1651fe775323556fe00b3608c887892878480e99041be601a62166ca6894bdd41a7054ec89f756ba9fc95302291
y = 0x84ad4719d044495496a3201c8ff484feb45b962e7302e56a392aee4abab3e4bdebf2955b4736012f21a08084056b19bcd7fee56048e004e44984e2f411788efdc837a0d2e5abb7b555039fd243ac01f0fb2ed1dec568280ce678e931868d23eb095fde9d3779191b8c0299d6e07bbb283e6633451e535c45513b2d33c99ea17

r = 548099063082341131477253921760299949438196259240
s = 857042759984254168557880549501802188789837994940
hm = 0xd2d0714f014a9784047eaeccf956520045c45265

for k in range(2**16):
    r_inv=Euclid(r,q)
    x=((s*k-hm)*r_inv) %q
    y1=pow(g,x,p)

    if y1==y:             #计算出的公开钥与给出的相比较
        print("user key x:",x)
        print("random number k",k)

ss=hex(x)[2:]
result=hashlib.sha1(ss.encode()).hexdigest()  #返回摘要作为16进制数据字符串的指
print("usr key x’SHA1:",result)

 

你可能感兴趣的:(python)