from Crypto.Util.number import * from Crypto.PublicKey import RSA
e = 65537 flag = b'ACTF{...}'
whileTrue: p = getPrime(1024) q = inverse(e, p) ifnot isPrime(q): continue n = p * q public = RSA.construct((n, e)) withopen("public.pem", "wb") as file: file.write(public.exportKey('PEM')) withopen("flag", "wb") as file: file.write(long_to_bytes(pow(bytes_to_long(flag), e, n))) break
# 读取密文c with open('flag', 'rb') as f: c = libnum.s2n(f.read())
n = 15987576139341888788648863000534417640300610310400667285095951525208145689364599119023071414036901060746667790322978452082156680245315967027826237720608915093109552001033660867808508307569531484090109429319369422352192782126107818889717133951923616077943884651989622345435505428708807799081267551724239052569147921746342232280621533501263115148844736900422712305937266228809533549134349607212400851092005281865296850991469375578815615235030857047620950536534729591359236290249610371406300791107442098796128895918697534590865459421439398361818591924211607651747970679849262467894774012617335352887745475509155575074809 e = 65537
# e*q = kp+1 # n*e = p*q*e = p*(kp+1) # k确实大不了
for k in range(1, 9999999): print(k) p = gmpy2.iroot((n*e)//k, 2) if n % p[0] == 0: # 不需要也不能判断是否整除 p = p[0] break
q = n // p phi = (p-1)*(q-1) d = int(gmpy2.invert(e, phi)) m = int(pow(c, d,n)) print(libnum.n2s(m)) # b'ACTF{F1nD1nG_5pEcia1_n_i5_nOt_eA5y}'
from sage.allimport * from secret import flag from Crypto.Util.number import bytes_to_long
defleak(a, b): # 可求出p、q、phi、d p = random_prime(pow(2, 64)) q = random_prime(pow(2, 64)) n = p * q e = 65537 print(n) print((pow(a, e) + pow(b, e) + 0xdeadbeef) % n)
defgen_key(): a = randrange(0, pow(2, 256)) b = randrange(0, pow(2, 256)) p = pow(a, 4) # a**4 q = pow(b, 4) # b**4 rp = randrange(0, pow(2, 24)) rq = randrange(0, pow(2, 24)) pp = next_prime(p + rp) # p + rp + p_ qq = next_prime(q + rq) # q + rq + q_ if pp % pow(2, 4) == (pp - p) % pow(2, 4) and qq % pow(2, 4) == (qq - q) % pow(2, 4): # a、b是偶数 # p % 16 = 0, a^4 mod 2^4 = 0, a = 2*k n = pp * qq rp = pp - p # 稍微放大了些 rq = qq - q return n, rp, rq
n, rp, rq = gen_key() e = 65537 c = pow(bytes_to_long(flag), e, n) print("n =", n) print("e =", e) print("c =", c) print("=======leak=======") leak(rp, rq)
''' n = 3183573836769699313763043722513486503160533089470716348487649113450828830224151824106050562868640291712433283679799855890306945562430572137128269318944453041825476154913676849658599642113896525291798525533722805116041675462675732995881671359593602584751304602244415149859346875340361740775463623467503186824385780851920136368593725535779854726168687179051303851797111239451264183276544616736820298054063232641359775128753071340474714720534858295660426278356630743758247422916519687362426114443660989774519751234591819547129288719863041972824405872212208118093577184659446552017086531002340663509215501866212294702743 e = 65537 c = 48433948078708266558408900822131846839473472350405274958254566291017137879542806238459456400958349315245447486509633749276746053786868315163583443030289607980449076267295483248068122553237802668045588106193692102901936355277693449867608379899254200590252441986645643511838233803828204450622023993363140246583650322952060860867801081687288233255776380790653361695125971596448862744165007007840033270102756536056501059098523990991260352123691349393725158028931174218091973919457078350257978338294099849690514328273829474324145569140386584429042884336459789499705672633475010234403132893629856284982320249119974872840 =======leak======= n_ = 122146249659110799196678177080657779971 tmp = 90846368443479079691227824315092288065 '''
n = 3183573836769699313763043722513486503160533089470716348487649113450828830224151824106050562868640291712433283679799855890306945562430572137128269318944453041825476154913676849658599642113896525291798525533722805116041675462675732995881671359593602584751304602244415149859346875340361740775463623467503186824385780851920136368593725535779854726168687179051303851797111239451264183276544616736820298054063232641359775128753071340474714720534858295660426278356630743758247422916519687362426114443660989774519751234591819547129288719863041972824405872212208118093577184659446552017086531002340663509215501866212294702743 e = 65537 c = 48433948078708266558408900822131846839473472350405274958254566291017137879542806238459456400958349315245447486509633749276746053786868315163583443030289607980449076267295483248068122553237802668045588106193692102901936355277693449867608379899254200590252441986645643511838233803828204450622023993363140246583650322952060860867801081687288233255776380790653361695125971596448862744165007007840033270102756536056501059098523990991260352123691349393725158028931174218091973919457078350257978338294099849690514328273829474324145569140386584429042884336459789499705672633475010234403132893629856284982320249119974872840 pq = int((iroot(n, 4)[0])**4)