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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
| # coding=utf-8 from Crypto.Util.number import * from gmpy2 import *
#求最大公因数 def gcd(a, b): m = max(a, b) n = min(a, b) r = m % n while r != 0: m = n n = r r = m % n return n
m=bytes_to_long("flag") p = 26440615366395242196516853423447 q = 27038194053540661979045656526063 r = 32581479300404876772405716877547 n = 23292710978670380403641273270002884747060006568046290011918413375473934024039715180540887338067 e=65537 phi=(p-1)*(q-1)*(r-1) d=invert(e,phi) #print d dp=d %(p-1) dq=d %(q-1) dr=d %(r-1)
#CRT签名 s1=pow(m,dp,p) s2=pow(m,dq,q) s3=pow(m,dr,r) pinv=invert(r*q,p) qinv=invert(p*r,q) rinv=invert(p*q,r) s=(s1*(r*q)*pinv+s2*(p*r)*qinv+s3*(q*p)*rinv)%n #print s #print long_to_bytes(pow(s,e,n)) #print (pow(m,d,n))
# 原始签名 b=pow(m,d,n) c=long_to_bytes(pow(b,e,n)) #print b #print c
# 故障攻击 #print long_to_bytes(pow(s,e,r)) #与原始明文相等 s2=s2+1 #在q的情况下翻转 s3=s3+1 s=(s1*(r*q)*pinv+s2*(p*r)*qinv+s3*(q*p)*rinv)%n print s print long_to_bytes(pow(s,e,n)) print long_to_bytes(pow(s,e,p)) print long_to_bytes(pow(s,e,q)) print long_to_bytes(pow(s,e,r))
print gcd(pow(s,e)-m,p) print p
print gcd(pow(s,e)-m,r) print r# coding=utf-8 from Crypto.Util.number import * from gmpy2 import *
#求最大公因数 def gcd(a, b): m = max(a, b) n = min(a, b) r = m % n while r != 0: m = n n = r r = m % n return n
m=bytes_to_long("flag") p = 26440615366395242196516853423447 q = 27038194053540661979045656526063 r = 32581479300404876772405716877547 n = 23292710978670380403641273270002884747060006568046290011918413375473934024039715180540887338067 e=65537 phi=(p-1)*(q-1)*(r-1) d=invert(e,phi) #print d dp=d %(p-1) dq=d %(q-1) dr=d %(r-1)
#CRT签名 s1=pow(m,dp,p) s2=pow(m,dq,q) s3=pow(m,dr,r) pinv=invert(r*q,p) qinv=invert(p*r,q) rinv=invert(p*q,r) s=(s1*(r*q)*pinv+s2*(p*r)*qinv+s3*(q*p)*rinv)%n #print s #print long_to_bytes(pow(s,e,n)) #print (pow(m,d,n))
# 原始签名 b=pow(m,d,n) c=long_to_bytes(pow(b,e,n)) #print b #print c
# 故障攻击 #print long_to_bytes(pow(s,e,r)) #与原始明文相等 s2=s2+1 #在q的情况下翻转 #3=s3+1 s=(s1*(r*q)*pinv+s2*(p*r)*qinv+s3*(q*p)*rinv)%n print s print long_to_bytes(pow(s,e,n)) print long_to_bytes(pow(s,e,p)) print long_to_bytes(pow(s,e,q)) print long_to_bytes(pow(s,e,r))
print gcd(pow(s,e)-m,p) print p
print gcd(pow(s,e)-m,r) print r
|