for i inrange(0, 0x59): c = bytes([v ^ i for v in data]) print(i, c)
buckeye{m1gh7y_m0rph1n_w1k1p3d14_p4g3}
Twin prime RSA/easy
題目
Real winners use twin primes
附件
import Crypto.Util.number as cun
whileTrue: p = cun.getPrime(1024) q = p + 2 if cun.isPrime(q): break
n = p * q e = 0x10001
phi = (p - 1) * (q - 1) d = pow(e, -1, phi)
FLAG = cun.bytes_to_long(b"buckeye{?????????????????????????????????????????????????????????????}") c = pow(FLAG, e, n) assertpow(c, d, n) == FLAG
print(f"n = {n}") print(f"c = {c}")
""" Output: n = 20533399299284046407152274475522745923283591903629216665466681244661861027880216166964852978814704027358924774069979198482663918558879261797088553574047636844159464121768608175714873124295229878522675023466237857225661926774702979798551750309684476976554834230347142759081215035149669103794924363457550850440361924025082209825719098354441551136155027595133340008342692528728873735431246211817473149248612211855694673577982306745037500773163685214470693140137016315200758901157509673924502424670615994172505880392905070519517106559166983348001234935249845356370668287645995124995860261320985775368962065090997084944099 c = 786123694350217613420313407294137121273953981175658824882888687283151735932871244753555819887540529041840742886520261787648142436608167319514110333719357956484673762064620994173170215240263058130922197851796707601800496856305685009993213962693756446220993902080712028435244942470308340720456376316275003977039668016451819131782632341820581015325003092492069871323355309000284063294110529153447327709512977864276348652515295180247259350909773087471373364843420431252702944732151752621175150127680750965262717903714333291284769504539327086686569274889570781333862369765692348049615663405291481875379224057249719713021 """
from Crypto.Util.number import long_to_bytes from gmpy2 import gmpy2, iroot from gmpy2 import mpz
n = mpz(20533399299284046407152274475522745923283591903629216665466681244661861027880216166964852978814704027358924774069979198482663918558879261797088553574047636844159464121768608175714873124295229878522675023466237857225661926774702979798551750309684476976554834230347142759081215035149669103794924363457550850440361924025082209825719098354441551136155027595133340008342692528728873735431246211817473149248612211855694673577982306745037500773163685214470693140137016315200758901157509673924502424670615994172505880392905070519517106559166983348001234935249845356370668287645995124995860261320985775368962065090997084944099) c = mpz(786123694350217613420313407294137121273953981175658824882888687283151735932871244753555819887540529041840742886520261787648142436608167319514110333719357956484673762064620994173170215240263058130922197851796707601800496856305685009993213962693756446220993902080712028435244942470308340720456376316275003977039668016451819131782632341820581015325003092492069871323355309000284063294110529153447327709512977864276348652515295180247259350909773087471373364843420431252702944732151752621175150127680750965262717903714333291284769504539327086686569274889570781333862369765692348049615663405291481875379224057249719713021) e = 0x10001
p = iroot(n, 2)[0] q = p + 2
fn = (p - 1) * (q - 1) d = gmpy2.invert(e, fn) m = gmpy2.powmod(c, d, n) print(long_to_bytes(m))
r if now thou not renewest,T hou dost beguile the world, unbless some mother.For wher e is she so fair whose unear â€:tm:d wombDisdains the tillag e of thy husbandry?Or who is he so fond will be the tomb Of his self-love, to stop po sterity?Thou art thy motherâ €:tm:s glass, and she in theeCa lls back the lovely April of her prime:So thou through w indows of thine age shall se eDespite of wrinkles this th y golden time.But if thou li buckeye{some_say_somefish:)}
powerball/easy
這題開始補題,賽中沒做出來。
題目
What could go wrong using a Linear Congruential Generator to get some random numbers?
FLAG = cun.bytes_to_long(b"buckeye{??????????????????????????????????????????????????????????????????????}") c_1 = pow(FLAG, e, n_1) c_2 = pow(FLAG, e, n_2)
assertpow(c_1, d_1, n_1) == FLAG assertpow(c_2, d_2, n_2) == FLAG
# from https://github.com/pablocelayes/rsa-wiener-attack/blob/master/ContinuedFractions.py
defrational_to_contfrac(x, y): ''' Converts a rational x/y fraction into a list of partial quotients [a0, ..., an] ''' a = x // y pquotients = [a] while a * y != x: x, y = y, x - a * y a = x // y pquotients.append(a) return pquotients
defcontfrac_to_rational(frac): '''Converts a finite continued fraction [a0, ..., an] to an x/y rational. ''' iflen(frac) == 0: return (0, 1) num = frac[-1] denom = 1 for _ inrange(-2, -len(frac) - 1, -1): num, denom = frac[_] * num + denom, num return (num, denom)
defconvergents_from_contfrac(frac): ''' computes the list of convergents using the list of partial quotients ''' convs = []; for i inrange(len(frac)): convs.append(contfrac_to_rational(frac[0:i])) return convs