在做CTF的题时遇到了一个RSA的解密问题,后来自己做了一下解密代码,放到下面,顺便把题目也带上
题目描述:
给定RSA密文[971,922,605,1446,1704,889,2090,605,1899,1915,2088,1988,1235,1032,65,922,958,1988,2144,591,1988,2270,2088,1032,65,958,2233],已知RSA的公钥为{7,2449},请还原出对应的明文。
考察意图:
考察选手对RSA加密算法的了解,以及基本的RSA算法攻击方法。
import math
def isPrime(n):
if n<=1:
return False
for i in xrange(2,int(math.sqrt(n)+1)):
if n%i==0:
return False
return True
def crack(n):
for p in xrange(2,n):
for q in xrange(p+1,n):
if p*q==n and isPrime(p) and isPrime(q):
return (p,q)
def GetD(e,fai):
count = 1
while((fai*count+1)%e!=0):
count = count+1
return (fai*count+1)/e
def decrypt(n,e,ciphertext):
(x, y) = crack(n)
fai = (x - 1) * (y - 1)
plaintext = []
d = GetD(e,fai)
for num in ciphertext:
num = pow(num,d,n)
plaintext.append(chr(num))
return "".join(plaintext)
if __name__ == "__main__":
# (x,y) = crack(2449)
# fai = (x-1)*(y-1)
# print GetD(7,fai)
#print ((x-1)*(y-1))
n = 2449
e = 7
ciphertext = [971,922,605,1446,1704,889,2090,605,1899,
1915,2088,1988,1235,1032,65,922,958,1988,
2144,591,1988,2270,2088,1032,65,958,2233]
plaintext = decrypt(n,e,ciphertext)
print plaintext
运算结果为:
flag{shamir_would_be_proud}