defmul_THcurve(n, P): R = (0, 0) while n > 0: if n % 2 == 1: R = add_THcurve(R, P) P = add_THcurve(P, P) n = n // 2 return R
p = 10297529403524403127640670200603184608844065065952536889 a = 2 G = (8879931045098533901543131944615620692971716807984752065, 4106024239449946134453673742202491320614591684229547464)
FLAG = flag.lstrip(b'DASCTF{').rstrip(b'}') assertlen(FLAG) == 15 m = bytes_to_long(FLAG) assert m < p Q = mul_THcurve(m, G) print("Q =", Q) # Q = (6784278627340957151283066249316785477882888190582875173, 6078603759966354224428976716568980670702790051879661797)
p = 10297529403524403127640670200603184608844065065952536889 a = 2 P = (8879931045098533901543131944615620692971716807984752065, 4106024239449946134453673742202491320614591684229547464) Q = (6784278627340957151283066249316785477882888190582875173, 6078603759966354224428976716568980670702790051879661797)
########################################################### part1 get d d = (a*P[0]^3 + P[1]^3 + 1) * inverse(P[0]*P[1], p) % p
########################################################### part2 dlp R.<x,y,z> = Zmod(p)[] cubic = a*x^3 + y^3 + z^3 - d*x*y*z E = EllipticCurve_from_cubic(cubic,morphism=True) P = E(P) Q = E(Q) r = 60869967041981 m = (r*Q).log(r*P) print(long_to_bytes(m))
#!/usr/bin/env python # -*- coding: UTF-8 -*- import os import hashlib from sage.allimport * from Crypto.Cipher import AES from Crypto.Util.Padding import pad from secret import c, b, key, FLAG
defadd_curve(P, Q, K): a, d, p = K if P == (0, 0): return Q if Q == (0, 0): return P x1, y1 = P x2, y2 = Q x3 = (x1 * y2 + y1 * x2) * pow(1 - d * x1 ** 2 * x2 ** 2, -1, p) % p y3 = ((y1 * y2 + 2 * a * x1 * x2) * (1 + d * x1 ** 2 * x2 ** 2) + 2 * d * x1 * x2 * (x1 ** 2 + x2 ** 2)) * pow( (1 - d * x1 ** 2 * x2 ** 2) ** 2, -1, p) % p return x3, y3
defmul_curve(n, P, K): R = (0, 0) while n > 0: if n % 2 == 1: R = add_curve(R, P, K) P = add_curve(P, P, K) n = n // 2 return R
defAES_encrypt(k): key = hashlib.sha256(str(k).encode()).digest()[:16] iv = os.urandom(16) cipher = AES.new(key, AES.MODE_CBC, iv) cipher = cipher.encrypt(pad(FLAG, 16)) data = {} data["iv"] = iv.hex() data["cipher"] = cipher.hex() return data
p = 770311352827455849356512448287 G = (584273268656071313022845392380 , 105970580903682721429154563816) P = (401055814681171318348566474726 , 293186309252428491012795616690)
########################################################### part1 get a,b PR.<c,b> = PolynomialRing(Zmod(p)) f1 = G[1]^2 - (G[0]^3 - c*G[0] + b) f2 = P[1]^2 - (P[0]^3 - c*P[0] + b) res = Ideal([f1,f2]).groebner_basis() print(res) a = 770311352827455849356512448252 b = -770311352827455849356512448189
########################################################### part3 get flag iv = bytes.fromhex('bae1b42f174443d009c8d3a1576f07d6') c = bytes.fromhex('ff34da7a65854ed75342fd4ad178bf577bd622df9850a24fd63e1da557b4b8a4') key = hashlib.sha256(str(key).encode()).digest()[:16] cipher = AES.new(key, AES.MODE_CBC, iv) print(cipher.decrypt(c))
#DASCTF{THe_C0rv!_1s_Aw3s0me@!!}
RSA_loss
题目描述:
1
RSA怎么解不出来了呢?
题目:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
from Crypto.Util.number import * from gmpy2 import * p = getPrime(100) q = getPrime(100) n = p * q e = 65537 message = b"" m = bytes_to_long(message) c = pow(m, e, n) print(f'c = {c}') print(f'p = {p}') print(f'q = {q}') d = invert(e,(p-1)*(q-1)) newm = pow(c, d, n) print(long_to_bytes(newm)) #c = 356435791209686635044593929546092486613929446770721636839137 #p = 898278915648707936019913202333 #q = 814090608763917394723955024893 #b'X\xee\x1ey\x88\x01dX\xf6i\x91\x80h\xf4\x1f!\xa7"\x0c\x9a\x06\xc8\x06\x81\x15'
from Crypto.Util.number import * from itertools import * import string
e = 65537 c = 356435791209686635044593929546092486613929446770721636839137 p = 898278915648707936019913202333 q = 814090608763917394723955024893 n = p * q cc = b'X\xee\x1ey\x88\x01dX\xf6i\x91\x80h\xf4\x1f!\xa7"\x0c\x9a\x06\xc8\x06\x81\x15' cc = bytes_to_long(cc)
if(1): table = string.digits + string.ascii_letters + "_" nums = 3 for j in product(table,repeat = nums): fix = bytes_to_long(b"DASCTF{" + "".join(j).encode() + b"\x00"*25 + b"}") try: flag = "".join(j) + long_to_bytes((cc - fix)*inverse(256,n) % n).decode() if(all(i in table for i in flag)): print("DASCTF{" + flag + "}") break except: pass
from Crypto.Util.number import * from gmpy2 import * from secret import flag
defdecode_e(e): if e > 1: mul = 1 for i inrange(1, e): mul *= i if e - mul % e - 1 == 0: mulmod = mul % e - e else: mulmod = mul % e return mulmod + decode_e(e - 1) else: return0
q = getPrime(1024) p = next_prime(q) n = p * q phi = (p - 1) * (q - 1) e = abs(decode_e(703440151)) c = pow(bytes_to_long(flag), e, n) print('n = {}\n' 'c = {}'.format(n, c))
''' n = 18770575776346636857117989716700159556553308603827318013591587255198383129370907809760732011993542700529211200756354110539398800399971400004000898098091275284235225898698802555566416862975758535452624647017057286675078425814784682675012671384340267087604803050995107534481069279281213277371234272710195280647747033302773076094600917583038429969629948198841325080329081838681126456119415461246986745162687569680825296434756908111148165787768172000131704615314046005916223370429567142992192702888820837032850104701948658736010527261246199512595520995042205818856177310544178940343722756848658912946025299687434514029951 c = 2587907790257921446754254335909686808394701314827194535473852919883847207482301560195700622542784316421967768148156146355099210400053281966782598551680260513547233270646414440776109941248869185612357797869860293880114609649325409637239631730174236109860697072051436591823617268725493768867776466173052640366393488873505207198770497373345116165334779381031712832136682178364090547875479645094274237460342318587832274304777193468833278816459344132231018703578274192000016560653148923056635076144189403004763127515475672112627790796376564776321840115465990308933303392198690356639928538984862967102082126458529748355566 '''
from Crypto.Util.number import * from tqdm import * from gmpy2 import *
n = 18770575776346636857117989716700159556553308603827318013591587255198383129370907809760732011993542700529211200756354110539398800399971400004000898098091275284235225898698802555566416862975758535452624647017057286675078425814784682675012671384340267087604803050995107534481069279281213277371234272710195280647747033302773076094600917583038429969629948198841325080329081838681126456119415461246986745162687569680825296434756908111148165787768172000131704615314046005916223370429567142992192702888820837032850104701948658736010527261246199512595520995042205818856177310544178940343722756848658912946025299687434514029951 c = 2587907790257921446754254335909686808394701314827194535473852919883847207482301560195700622542784316421967768148156146355099210400053281966782598551680260513547233270646414440776109941248869185612357797869860293880114609649325409637239631730174236109860697072051436591823617268725493768867776466173052640366393488873505207198770497373345116165334779381031712832136682178364090547875479645094274237460342318587832274304777193468833278816459344132231018703578274192000016560653148923056635076144189403004763127515475672112627790796376564776321840115465990308933303392198690356639928538984862967102082126458529748355566
if(0): p = iroot(n,2)[0] for i inrange(1000): p += 1 if(n % p == 0): print(p)
p = 137005750887861042579675520137044512945598822783534629619239107541807615882572096858257909592145785126427095471870315367525847725823941391135851384962433640952546093687945848986528958373691860995753297871619638780075391669495117388905134584566094832853663864356912013900594295175075123578366393694884648557429 q = n // p e = abs(-prime_pi(703440151)+2) print(long_to_bytes(int(pow(c,inverse(e,(p-1)*(q-1)),n))))