import gmpy2 p = q = e = phi = (p-1)(q-1) d = gmpy2.invert(e,phi) print(d)
2.已知(p,q,e,c),求m
密文c,明文m
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
import gmpy2 import binascii c = e = p = q = # 计算私钥 d phi = (p-1)*(q-1) d = gmpy2.invert(e, phi) # 解密 m m = gmpy2.powmod(c,d,p*q) print(binascii.unhexlify(hex(m)[2:]))
3.已知(p,q,dp,dq,c),求m
1 2 3 4 5 6 7 8 9 10 11 12 13 14
import gmpy2 import binascii p = q = dp = dq = c = I = gmpy2.invert(p,q) mp = gmpy2.powmod(c,dp,p) mq = gmpy2.powmod(c,dq,q) m = ((I*(mp-mq))%q)*p+mp print(binascii.unhexlify(hex(m)[2:]))
4.已知(e,dp,n,c),求m
1 2 3 4 5 6 7 8 9 10 11 12 13 14
import gmpy2 import binascii e = n = dp = c = for i in range(1,e): if (e*dp-1)%i == 0 and n%((e*dp-1)//i+1)==0: q = n//((e*dp-1)//i+1) phi = (q-1)*((e*dp-1)//i) d = gmpy2.invert(e,phi) m = gmpy2.powmod(c,d,n) print(binascii.unhexlify(hex(m)[2:]))
import gmpy2 import binascii n = c1 = c2 = e1 = e2 = s = gmpy2.gcdext(e1,e2) a = s[1] b = s[2] if a<0: a = -a c1 = gmpy2.invert(c1,n) else: b = -b c2 = gmpy2.invert(c2,n) m = (gmpy2.powmod(c1,a,n)*gmpy2.powmod(c2,b,n))%n print(binascii.unhexlify(hex(m)[2:]))
import gmpy2 import binascii e = n = c = p = q = phi = (p-1)*(q-1) d = gmpy2.invert(e,phi) m = gmpy2.powmod(c,d,n) print(binascii.unhexlify(hex(m)[2:]))
9.已知(e,n,c),求m(e极小,如3,低加密指数攻击)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
import gmpy2 import binascii e = n = c = i = 0 while True: if gmpy2.iroot((c+i*n),3)[1] == True: m = gmpy2.iroot((c+i*n),3)[0] break i += 1 print(binascii.unhexlify(hex(m)[2:]))
import gmpy2 import binascii import RSAwienerHacker e = n = c = d = RSAwienerHacker.hack_RSA(e,n) m = gmpy2.powmod(c,d,n) print(binascii.unhexlify(hex(m)[2:]))
11.已知(c,n,p(q-1),q*(p-1)),求m
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
import gmpy2 from Crypto.Util.number import * #pq = p*(q-1) #qp = q*(p-1) c= n= pq= qp= e = 65537 p = n - pq q = n - qp phi = (p - 1)*(q - 1) d = gmpy2.invert(e,phi) m = gmpy2.powmod(c,d,n) print(long_to_bytes(m))