from Crypto.Util.number import * from Crypto.PublicKey import RSA from secret import s, FLAG
#题目所给的python文件使用的是python2,与python3语法、函数有些许不同 #得到一个1024bit的质数,不理解函数体也没关系,用于分析s的数值,大致猜出s不会太大 defgen_prime(s): whileTrue: #getPrime(s)返回一个随机N-Bit的质数 r = getPrime(s) R = [r] t = int(5 * s / 2) + 1 for i in range(0, t): #getRandomRange(a,b)函数得到一个在[a,b)之间的random R.append(r + getRandomRange(0, 4 * s ** 2)) ''' lambda作为一个表达式,定义了一个匿名函数,a,b为函数传参,a*b为函数体 reduce(function,iterable[,initializer]) 函数会对参数序列中元素进行累积。 也就是对iterable可迭代对象(如列表、元组)中的第1、2个元素进行函数操作, 将得到的结果与第三个元素用function运算,最后得到一个结果,如果有init参数, 则先将init与第一个元素进行运算 ''' p = reduce(lambda a, b: a * b, R, 2) + 1 if isPrime(p): if len(bin(p)[2:]) == 1024: return p
#循环,直到得到2048bit的n whileTrue: p = gen_prime(s) q = gen_prime(s) n = p * q e = 65537 d = inverse(e, (p-1)*(q-1)) if len(bin(n)[2:]) == 2048: break
msg = FLAG key = RSA.construct((long(n), long(e), long(d), long(p), long(p))) #循环,加密s次 for _ in xrange(s): #RSA加密得到密文 enc = key.encrypt(msg, 0)[0] msg = enc
# -*- coding: cp936 -*- import base64 from Crypto.PublicKey import RSA 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) defmodinv(a,m): g,x,y=egcd(a,m) if g!=1: raise Exception('modular inverse does not exist') else: return x%m p=139457081371053313087662621808811891689477698775602541222732432884929677435971504758581219546068100871560676389156360422970589688848020499752936702307974617390996217688749392344211044595211963580524376876607487048719085184308509979502505202804812382023512342185380439620200563119485952705668730322944000000001
q = 155617827023249833340719354421664777126919280716316528121008762838820577123085292134385394346751341309377546683859340593439660968379640585296350265350950535158375685103003837903550191128377455111656903429282868722284520586387794090131818535032744071918282383650099890243578253423157468632973312000000000000001 n = p*q e = 65537 d=modinv(e,(p-1)*(q-1))#RSA私钥