import gmpy2 as gp import binascii p = gp.mpz(681782737450022065655472455411) q = gp.mpz(675274897132088253519831953441) e = gp.mpz(13) c = gp.mpz(275698465082361070145173688411496311542172902608559859019841) n = p*q phi = (p-1) * (q-1) d = gp.invert(e, phi) m = pow(c, d, n) print(m)
gcd(e1,e2)=1 再推出此式: e1*s1+e2*s2 = 1 由扩展欧几里德算法,可以得到一组解 (s1,s2) 假设s1为正、s2为负 因 c1 = m^e1 mod N c2 = m^e2 mod N 故推出此方程式 (c1^s1*c2^s2)mod N= ((m^e1 mod N)^s1*(m^e2 mod N)^s2)mod N 根据模运算性质,可以化简为此方程式 (c1^s1*c2^s2)mod N = ((m^e1)^s1*(m^e2)^s2)mod N (c1^s1*c2^s2)mod N = (m^(e1^s1+e2^s2))mod N 又因为 e1*s1+e2*s2 = 1 所以 (c1^s1*c2^s2)mod N = (m^(1))mod N (c1^s1c2^s2)mod N = m mod N 即 c1^s1*c2^s2 = m 因此可以在不知道d1,d2情况下,解出m
# 扩展欧几里得算法 defegcd(a, b): if a == 0: return b, 0, 1 else: g, y, x = egcd(b % a, a) return g, x - b // a * y, y defmain(): with open('flag.enc1', 'r') as f1: c1 = f1.read().encode('hex') c1 = string.atoi(c1, base=16) #string.atoi(s[,base]) 字符串转换成数字 base是16那么s就只能是0x23或0X12这种形式的字符串 with open('flag.enc2', 'r') as f2: c2 = f2.read().encode('hex') c2 = string.atoi(c2, base=16)
n = 0x00b0bee5e3e9e5a7e8d00b493355c618fc8c7d7d03b82e409951c182f398dee3104580e7ba70d383ae5311475656e8a964d380cb157f48c951adfa65db0b122ca40e42fa709189b719a4f0d746e2f6069baf11cebd650f14b93c977352fd13b1eea6d6e1da775502abff89d3a8b3615fd0db49b88a976bc20568489284e181f6f11e270891c8ef80017bad238e363039a458470f1749101bc29949d3a4f4038d463938851579c7525a69984f15b5667f34209b70eb261136947fa123e549dfff00601883afd936fe411e006e4e93d1a00b0fea541bbfc8c5186cb6220503a94b2413110d640c77ea54ba3220fc8f4cc6ce77151e29b3e06578c478bd1bebe04589ef9a197f6f806db8b3ecd826cad24f5324ccdec6e8fead2c2150068602c8dcdc59402ccac9424b790048ccdd9327068095efa010b7f196c74ba8c37b128f9e1411751633f78b7b9e56f71f77a1b4daad3fc54b5e7ef935d9a72fb176759765522b4bbc02e314d5c06b64d5054b7b096c601236e6ccf45b5e611c805d335dbab0c35d226cc208d8ce4736ba39a0354426fae006c7fe52d5267dcfb9c3884f51fddfdf4a9794bcfe0e1557113749e6c8ef421dba263aff68739ce00ed80fd0022ef92d3488f76deb62bdef7bea6026f22a1d25aa2a92d124414a8021fe0c174b9803e6bb5fad75e186a946a17280770f1243f4387446ccceb2222a965cc30b3929L e1 = 17 e2 = 65537 s = egcd(e1, e2) s1 = s[1] s2 = s[2]
# 读取公钥参数 with open('pubkey.pem', 'r') as f: key = RSA.importKey(f) N = key.n e = key.e with open('flag.enc', 'r') as f: cipher = f.read().encode('hex') cipher = string.atoi(cipher, base=16)