根据题意,已知 $p、q、e、d、c$,求 $m$。此题属于 RSA 加密算法基础题,思路也容易想到,直接求 $m = c^d \ mod \ n$ 即可得到明文,且 $n = p \times q$。下面给出 Python 解题代码,代码中的函数使用方法请自行查阅相应模块的使用手册。
1 2 3 4 5 6 7 8 9 10 11
import gmpy2 import binascii
p = gmpy2.mpz(0xddb9fe428c938abdce551751b299feed367c97b52b17062d) q = gmpy2.mpz(0x265ad78c77eab2c5ab9b69fb44a11624818bed56c003d2c5e6d29f7) d = gmpy2.mpz(0x9737352a92d198aaeddb8db9cc5cb81a788aa7d0ec0ec9bac7f6ffece7ff928c8db6a47656dffb0421ef9ca595665b6b55d35f) c = gmpy2.mpz(0x1c0aa41505131e967d3db09227ef9572337a5e79f07484428efd6262eddee2fb67b6e6e6b5506871891ece1949eabf7a03cafdb)
m = hex(pow(c, d, p*q)) # Figure out c^d modulo n, then change to hex. Note that n = p * q. print"plaintext:", m print"flag:", binascii.a2b_hex(str(m)[2:])
p = gmpy2.mpz(0xf5621a5fd44994f720c1b971fea84f63) q = gmpy2.mpz(0xdfb85e3d22c0b59271884df021a57123) e = gmpy2.mpz(0xf36698ed9d9fedd7) c = gmpy2.mpz(0x448040a0a4757a630c4d8401fb3c0518ab0bce9a02085329536244c91727775c)
phi_n = (p - 1) * (q - 1) d = gmpy2.invert(e, phi_n) # Figure out the multiplicative inverse of e modulo phi_n (i.e. the private key). m = hex(pow(c, d, p*q)) # Figure out c^d modulo n, then change to hex. Note that n = p * q.
print"private key:", hex(d) print"plaintext:", m print"flag:", binascii.a2b_hex(str(m)[2:])
public key 1: n = 0x9ff2e873e1fcafbed22341b4eafdae01afec540e4e84e6041b0a0e83253f1c5da5dabc73004faa82cfaff8e83e5a99255f9790a7744dd18a694d09a89a5caf638d0cf4fe1150a9e894d47a17f386c107a083ae227efab851196de992a2441b7af3442f31a757234ef8997d8af1a3c3aecf2b6100d393a7b632913c2b1c921409 e = 0xe9a44960483b5ca224cfd18818944eaae47de3a158debbc7886b74d7e11165e2e4158c86add4ccc5317256e5323596c9947513766645aefdac4f0375a0296743
encrypted message 1: c = 0x636f86fb2b1991d4788092563adf87d14b975e9c7ab7279b10f4741f515788bba2e6e788d6f6c165f4daf65eabee93cebfc55a1d651b1dfb1190174ab338d959775658cf1c6d42b0fe6b7b1abaf5a9aa4ca239367bfcbe88b304c99d5e5f8aac019ec74b11662a5deba523c2f93b7c68a731c019578e3ac64db64cfd3533e91b
public key 2: n = 0x9ff2e873e1fcafbed22341b4eafdae01afec540e4e84e6041b0a0e83253f1c5da5dabc73004faa82cfaff8e83e5a99255f9790a7744dd18a694d09a89a5caf638d0cf4fe1150a9e894d47a17f386c107a083ae227efab851196de992a2441b7af3442f31a757234ef8997d8af1a3c3aecf2b6100d393a7b632913c2b1c921409 e = 0xd9b47cdd777deb3e94cfa3d416aa91b04f9391af0504a83de03e9e0c49faae8b79cf7c99f575af99ed2e9e5a7edb09219c4f79cf961092f9919ab33bc3c9a74f
encrypted message 2: c = 0x53b601daa8f93166495a69fa747f8553bb8317cfe6dc3f7fec8c8511e209f9288038405fdee399f3ed68ab25dcd91be8bb2ef2ecac1173318b5d2bba932afdcab2d4e5b46987a0f774a29204ce481f79ea422943118f2eaf6c6820b501d9da8d3fbbcea464a2d158a39de6bae6ab845555e4646ae556d7b1e00567b00d41b06c
# Extended Euclid Algorithm defextendedGCD(a, b): # a*xi + b*yi = ri if b == 0: return (1, 0, a) # a*x1 + b*y1 = a x1 = 1 y1 = 0 # a*x2 + b*y2 = b x2 = 0 y2 = 1 while b != 0: q = a / b # ri = r(i-2) % r(i-1) r = a % b a = b b = r # xi = x(i-2) - q*x(i-1) x = x1 - q*x2 x1 = x2 x2 = x # yi = y(i-2) - q*y(i-1) y = y1 - q*y2 y1 = y2 y2 = y return (x1, y1, a)
public key 1: n = 0xba298d721fadbadb15dabd393db296c13610b33bfeb3aea844815439df3b025bcc6a7085a21eeb3b904a17071c01f05229873518828a8eb8a9129cff611f3481 e = 0x3
encrypted message 1: c = 0x7d4f6c0953ec517212b6c778da72245820a749254d21b62d09e36b44e073f858114f174b71cee25104b4d3b0abbf7eb31f031201bf40846290344c865c4b9cf8
public key 2: n = 0xe37a3cab324cc0a5ea1030b498f3838f674e6ee9b4e441900c604e4d095b04c70cd32a7c4a5be0b463e3fd94594b3bd25ada9bc9ca17a80d72b7928e233f726d e = 0x3
encrypted message 2: c = 0xc09aea0a9b6e10d7db7a5c2071b46f5801896c536152badb81db37848ef373cf6c6842737a87c12f6aba1d39bdf5d2aaf40e919628a64e4cd78a42c2cdde651a
public key 3: n = 0xd8b6924687baaffe1c205ac0474fd5b5f894cb97abb3d427df0e47f30c7f035c07586430679ab65c5bbdccbc53cea9c95c466f3171d24efb85433bd05bc36c5d e = 0x3
encrypted message 3: c = 0x6e3591536b9aadcdb412d6b05a755d603d0272434cc27447a8877707861363c8408b47da377474924db89a3e104717855613cbea16ad439c98b6e7bfdb7ae14f