from Crypto.Util.number import * from pwn import * from tqdm import * from hashlib import sha256 from base64 import b64decode,b64encode
#context.log_level = 'debug'
defproof_of_work(): table = string.digits + string.ascii_letters temp = r.recvuntil(b"sha256(XXXX+") temp = r.recvline() suffix = temp[:16].decode() hex1 = temp[20:].strip().decode() for i in tqdm(table): for j in table: for k in table: for m in table: temp1 = i+j+k+m if(sha256((temp1+suffix).encode()).hexdigest() == hex1): r.sendline(temp1.encode()) return
r = remote("59.110.20.54", 5526) proof_of_work() r.recvline() print(r.recvline())
#SYC{st3p_1nt0_1nter4ctive_Crypt0graphy}
SimpleRSA
题目描述:
1 2
So simple RSA! Wait... Are you kidding me? https://en.wikipedia.org/wiki/RSA_(cryptosystem) hint: flag<p
题目:
1 2 3 4 5 6 7 8 9 10 11 12
import gmpy2 from Crypto.Util.number import * flag = b"SYC{Al3XEI_FAKE_FLAG}" assertlen(flag) == 35 p,q = [getPrime(2048) for _ in"__"] n = p*q e = 65537 c = gmpy2.powmod(bytes_to_long(flag),e,n) print(p) print(c) #24724324630507415330944861660078769085865178656494256140070836181271808964994457686409910764936630391300708451701526900994412268365698217113884698394658886249353179639767806926527103624836198494439742123128823109527320850165486500517304731554371680236789357527395416607541627295126502440202040826686102479225702795427693781581584928770373613126894936500089282093366117940069743670997994742595407158340397268147325612840109162997306902492023078425623839297511182053658542877738887677835528624045235391227122453939459585542485427063193993069301141720316104612551340923656979591045138487394366671477460626997125944456537 #510345661718450375632304764819724223824018609359964259503762283253350010161515190912152623604019093266967095847334388281390406831587663253164256543905694021952211220652820225527413861208452760215767828927039893435528572148282529198773772864255061213208279999011194952146362748485103032149806538140693537361755210176698895104708379400806511907719904867068865970241208806615061055047254026118016836750283966478103987375361826198930529462261013324904522014804502582865716441828895047550041401172127129749969507853355531197814919603963664646220505672302543085959372679395717892060245461464861507164276442140407308832537707450729432224150754603518526288767105682399190438680085925078051459448618725871249563011864525585870188123725554411655044152994826056900502298772802133526591794328224932405680583757307064395792317383571866619582974377344736930271554160701478385763426091091686496788999588340419226785217028504684542197970387916262126278955278523452903043316452825738030645100271595942652498852506660789605846309602343932245435421425673058238785509280366229754404949219663043627431437755087855502139890639468481922788973821783957766433857773771229298328019250652625289700950165414584983487319078090573179470893450632419467111117341472
用p解密就好。
exp:
1 2 3 4 5 6 7 8 9
from Crypto.Util.number import *
p = 24724324630507415330944861660078769085865178656494256140070836181271808964994457686409910764936630391300708451701526900994412268365698217113884698394658886249353179639767806926527103624836198494439742123128823109527320850165486500517304731554371680236789357527395416607541627295126502440202040826686102479225702795427693781581584928770373613126894936500089282093366117940069743670997994742595407158340397268147325612840109162997306902492023078425623839297511182053658542877738887677835528624045235391227122453939459585542485427063193993069301141720316104612551340923656979591045138487394366671477460626997125944456537 c = 510345661718450375632304764819724223824018609359964259503762283253350010161515190912152623604019093266967095847334388281390406831587663253164256543905694021952211220652820225527413861208452760215767828927039893435528572148282529198773772864255061213208279999011194952146362748485103032149806538140693537361755210176698895104708379400806511907719904867068865970241208806615061055047254026118016836750283966478103987375361826198930529462261013324904522014804502582865716441828895047550041401172127129749969507853355531197814919603963664646220505672302543085959372679395717892060245461464861507164276442140407308832537707450729432224150754603518526288767105682399190438680085925078051459448618725871249563011864525585870188123725554411655044152994826056900502298772802133526591794328224932405680583757307064395792317383571866619582974377344736930271554160701478385763426091091686496788999588340419226785217028504684542197970387916262126278955278523452903043316452825738030645100271595942652498852506660789605846309602343932245435421425673058238785509280366229754404949219663043627431437755087855502139890639468481922788973821783957766433857773771229298328019250652625289700950165414584983487319078090573179470893450632419467111117341472 e = 65537 d = inverse(e,p-1) print(long_to_bytes(pow(c,d,p)))
#SYC{Just_a_s1mple_modular_equation}
OTPTwice
题目描述:
1
I invented a new symmetric cryptosystem, and I believe you will never break it!
from Crypto.Util.number import * from Crypto.Cipher import DES3 from pwn import * from tqdm import * from hashlib import sha256
#context.log_level = 'debug'
defproof_of_work(): table = string.digits + string.ascii_letters temp = r.recvuntil(b"sha256(XXXX+") temp = r.recvline() suffix = temp[:16].decode() hex1 = temp[20:].strip().decode() for i in tqdm(table): for j in table: for k in table: for m in table: temp1 = i+j+k+m if(sha256((temp1+suffix).encode()).hexdigest() == hex1): r.sendline(temp1.encode()) return
from Crypto.Util.number import * from Crypto.Cipher import DES3 from pwn import * from tqdm import * from hashlib import sha256
#context.log_level = 'debug'
defproof_of_work(): table = string.digits + string.ascii_letters temp = r.recvuntil(b"sha256(XXXX+") temp = r.recvline() suffix = temp[:16].decode() hex1 = temp[20:].strip().decode() for i in tqdm(table): for j in table: for k in table: for m in table: temp1 = i+j+k+m if(sha256((temp1+suffix).encode()).hexdigest() == hex1): r.sendline(temp1.encode()) return
defsox(n, d): x, y, t = 0, 0, d for s inrange(n - 1): u = 1 & t // 2 v = 1 & t ^ u x, y = spin(2**s, x, y, u, v) x += 2**s * u y += 2**s * v t = t // 4 return x, y
defspin(n, x, y, u, v): if v == 0: if u == 1: x = n - 1 - x y = n - 1 - y x, y = y, x return x, y
n = 256 pix = [] for d in trange(n**2): x, y = sox(n, d) pix.append((x,y))
defproof_of_work(): if DEBUG: returnTrue proof = ''.join([random.choice(string.ascii_letters+string.digits) for _ inrange(20)]) digest = hashlib.sha256(proof.encode()).hexdigest() print("sha256(XXXX+%s) == %s" % (proof[4:], digest)) x = input("Give me XXXX: ") iflen(x)!=4or hashlib.sha256((x+proof[4:]).encode()).hexdigest() != digest: returnFalse print("Right!") returnTrue
defcheck(a,b,p,turn,ans): if DEBUG: returnTrue try: if turn == "a": returnint(a) == ans if turn == "b": returnint(b) == ans if turn == "p": returnint(p) == ans except Exception: exit()
try: ifnot proof_of_work(): exit() print(banner) print('\nHi Crypto-ers! AL3XEI here. I know you are excellent at math, so I prepared a game for u.') print('In the equation y^2 = x^3+ a*x + b (mod p), 4 points are given. Plz give me the right a, b or p to contine the game.') print('Good Luck!\n') print(banner+'\n')
for i inrange(10): turn = random.choice(abp) p = getPrime(pbits) a,b = [next_prime(random.randint(2,p)) for _ in"ab"] curve = EllipticCurve(GF(p),[a,b]) pts = [curve.random_point() for _ inrange(4)] pts = [(_[0], _[1]) for _ in pts] for _ in pts: print(_,end=" ") print('\nGive me '+turn+" :") ans = int(input('> ')) if check(a,b,p,turn,ans): print("Good! Next challenge->\n") print(banner+'\n') pbits+=5 continue else: print("Something goes wrong...\n") print(banner+'\n') exit()
from Crypto.Util.number import * from pwn import * from tqdm import * from hashlib import sha256
#context.log_level = 'debug'
defproof_of_work(): table = string.digits + string.ascii_letters temp = r.recvuntil(b"sha256(XXXX+") temp = r.recvline() suffix = temp[:16].decode() hex1 = temp[20:].strip().decode() for i in tqdm(table): for j in table: for k in table: for m in table: temp1 = i+j+k+m if(sha256((temp1+suffix).encode()).hexdigest() == hex1): r.sendline(temp1.encode()) return
import os import random import string import hashlib
flag = os.environ.get("FLAG", b"SYC{Al3XEI_FAKE_FLAG}") DEBUG = False banner = '|'*70 if DEBUG: print("==DEBUG MODE==")
defproof_of_work(): if DEBUG: returnTrue proof = ''.join([random.choice(string.ascii_letters+string.digits) for _ inrange(20)]) digest = hashlib.sha256(proof.encode()).hexdigest() print("sha256(XXXX+%s) == %s" % (proof[4:], digest)) x = input("Give me XXXX: ") iflen(x)!=4or hashlib.sha256((x+proof[4:]).encode()).hexdigest() != digest: returnFalse print("Right!") returnTrue
try: ifnot proof_of_work(): exit() print(banner) parms = [random.getrandbits(32) for _ inrange(128)] res = res = int(input('Give me x calculating f(x) :\n> ')) if res >= 2**32: print("Give me something smaller.\n") print(banner+'\n') exit()
cnt = 0 for _ inrange(128): cnt += pow(res,_)*parms[_] print(cnt) ans = input('Give me Coefficients :\n> ') ans = [int(_) for _ in ans.split(",")] if ans == parms: print('Congrats! Your flag is:',flag) else: exit()
from Crypto.Util.number import * from pwn import * from tqdm import * from hashlib import sha256
#context.log_level = 'debug'
defproof_of_work(): table = string.digits + string.ascii_letters temp = r.recvuntil(b"sha256(XXXX+") temp = r.recvline() suffix = temp[:16].decode() hex1 = temp[20:].strip().decode() for i in tqdm(table): for j in table: for k in table: for m in table: temp1 = i+j+k+m if(sha256((temp1+suffix).encode()).hexdigest() == hex1): r.sendline(temp1.encode()) return
r = remote("59.110.20.54", 2613) proof_of_work() r.recvuntil(b"> ") res = -2**64 r.sendline(str(res).encode()) cnt = int(r.recvline().strip().decode()) tt = cnt r.recvuntil(b"> ")
parms = [0for i inrange(128)] for i inrange(128): parms[i] = cnt & ((1<<64)-1) if(i%2): parms[i] = 2**64-parms[i] elif(i!=0and i%2 == 0): parms[i] += 1 cnt >>= 64
from Crypto.Util.number import * from libnum import* from secret import flag
p = getPrime(512) q = getPrime(512) r = getPrime(512) e = getPrime(32) n = p*q*r phi = (p-1)*(q-1)*(r-1) d = inverse(e,phi) dP = d%((q-1)*(r-1)) dQ = d%((p-1)*(r-1)) dR = d%((p-1)*(q-1)) m = s2n(flag.encode()) c = pow(m,e,n)
Hi Crypto-ers! AL3XEI Here. In number theory, if there exists an integer q satisfying x^2=q(mod n), q is so called a quadratic residue. We write this calculation as L(a,p), which its value shows a isorisnot quadratic residue modulo p. Below, you need to give me the answer of the sum of L(a*l**2+b*l+1,p), where a,b are integers, p is a prime, and l rise from0 to p-1. For example, given a = 2, b = 3, c = 1, p = 5, the answer will be L(1, 5) + L(6, 5) + L(15, 5) + L(28, 5) + L(45, 5) = 1. Hope you success!
from Crypto.Util.number import * from pwn import * from tqdm import * from hashlib import sha256 import re
#context.log_level = 'debug'
#part1 proof defproof_of_work(): table = string.digits + string.ascii_letters temp = r.recvuntil(b"sha256(XXXX+") temp = r.recvline() suffix = temp[:16].decode() hex1 = temp[20:].strip().decode() for i in tqdm(table): for j in table: for k in table: for m in table: temp1 = i+j+k+m if(sha256((temp1+suffix).encode()).hexdigest() == hex1): r.sendline(temp1.encode()) return
r.recvuntil(b"|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||") for i inrange(7): r.recvuntil(b"|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||") r.recvline() r.recvline() temp = r.recvline().strip().decode() a,b,c = find_coeffcients(temp) p = find_p(r.recvline().strip().decode()) #print("a,b,c,p = "+str(a)+","+str(b)+","+str(c)+","+str(p)) r.recvuntil(b"> Type your answer:") r.sendline(str(p-1).encode())
from Crypto.Util.number import * import random from gmpy2 import * from tqdm import *
defcalcsum(a,b,c,p): sum = 0 for i inrange(0,p): temp = a*i**2+b*i+1 if(powmod(temp,(p-1)//2,p) == 1): sum += 1 elif(powmod(temp,(p-1)//2,p) == p-1): sum += -1 else: sum += 0 returnsum
for i inrange(10): p = getPrime(18) a = random.randint(1,p-1) b = random.randint(1,p-1) c = 1 print(powmod(a,(p-1)//2,p),calcsum(a,b,c,p))
from Crypto.Util.number import * from pwn import * from tqdm import * from hashlib import sha256 import re
#context.log_level = 'debug'
#part1 proof defproof_of_work(): table = string.digits + string.ascii_letters temp = r.recvuntil(b"sha256(XXXX+") temp = r.recvline() suffix = temp[:16].decode() hex1 = temp[20:].strip().decode() for i in tqdm(table): for j in table: for k in table: for m in table: temp1 = i+j+k+m if(sha256((temp1+suffix).encode()).hexdigest() == hex1): r.sendline(temp1.encode()) return
r.recvuntil(b"|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||") for i in trange(10): r.recvuntil(b"|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||") r.recvline() r.recvline() temp = r.recvline().strip().decode() a,b,c = find_coeffcients(temp) p = find_p(r.recvline().strip().decode()) #print("a,b,c,p = "+str(a)+","+str(b)+","+str(c)+","+str(p)) r.recvuntil(b"> Type your answer:") if((b*inverse(2,p))**2 % p == a%p): res = p-1 elif(pow(a,(p-1)//2,p) == 1): res = -1 else: res = 1 r.sendline(str(res).encode())
''' p = 1068910928091265978478887270179608140018534288604159452828300604294675735481804963679672853224192480667904101881092533866322948043654533322038484907159945421 q = 1711302770747802020613711652777299980542669713888988077474955896217408515180094849053961025086865697904731088087532944829046702427480842253022459937172565651 r = 132969813572228739353704467775972551435751558645548804253458782569132362201099158857093676816706297676454547299888531536236748314013888413096371966359860637 y = 5385116324746699759660077007129548063211490907227715474654765255668507958312745677683558789874078477569613259930365612562164095274660123330458355653249805062678976259429733060364358954180439218947514191603330532117142653558803034110759332447742304749985874760435453594107494324797235909651178472904825071375135846093354526936559640383917210702874692725723836865724807664892994298377375580807917514349966834376413176898806591411038129330967050554114677719107335006266 '''
from Crypto.Util.number import * from sympy.ntheory.modular import crt
p = 1068910928091265978478887270179608140018534288604159452828300604294675735481804963679672853224192480667904101881092533866322948043654533322038484907159945421 q = 1711302770747802020613711652777299980542669713888988077474955896217408515180094849053961025086865697904731088087532944829046702427480842253022459937172565651 r = 132969813572228739353704467775972551435751558645548804253458782569132362201099158857093676816706297676454547299888531536236748314013888413096371966359860637 y = 5385116324746699759660077007129548063211490907227715474654765255668507958312745677683558789874078477569613259930365612562164095274660123330458355653249805062678976259429733060364358954180439218947514191603330532117142653558803034110759332447742304749985874760435453594107494324797235909651178472904825071375135846093354526936559640383917210702874692725723836865724807664892994298377375580807917514349966834376413176898806591411038129330967050554114677719107335006266 g = 3
xp = discrete_log(mod(y,p),mod(g,p)) xq = discrete_log(mod(y,q),mod(g,q)) xr = discrete_log(mod(y,r),mod(g,r))
n = [p-1,q-1,r-1] c = xp,xq,xr M = crt(n,c)[0] print(long_to_bytes(int(M)))
meum = '''option: 1: start game 2: get hint 3: exit ''' print(meum)
whileTrue: print('input your option: ', end='') your_input = input()
if your_input == '1': n = getPrime(36) m = getPrime(16) c = getPrime(16) seed = getPrime(36) out = seed round = 0 score = 0 res = [] whileTrue: round += 1 res = [] print(f'round:{round}') print(f'score:{score}') for i inrange (3): out = (out*m+c)%n res.append(out) ifround == 1: for i in res: card, suit = choose_card(i) print(card) elifround==2orround==3: #gift for i in res: card, suit = choose_card(i) print(card) print(f'gift: {res}') else: cards = [] suits = [] for i inrange(len(res)): card, suit = choose_card(res[i]) cards.append(card) suits.append(suit) print("Give me your guess: (example: Heart_1 Club_2 Diamond_3)") try: g_1, g_2, g_3 = input().split() g_1, g_2, g_3 = g_1.split('_'), g_2.split('_'), g_3.split('_') except ValueError: print("Please enter in the correct format.") return if (g_1[0] == suits[0] and g_1[1] == cards[0][15]) and (g_2[0] == suits[1] and g_2[1] == cards[1][15]) and (g_3[0] == suits[2] and g_3[1] == cards[2][15]): for i in cards: print(i) print("Congratulations! You matched the cards!") score += 1 else: for i in cards: print(i) print("Try again!") if score == 50: print('The flag is your reward!') print(flag) return else: continue if your_input == '2': print("Have you ever heard of LCG?")
if your_input == '3': break
if __name__ == '__main__': GAME()
card文件是给了很多如下形式的牌面字符串:
1 2 3 4 5 6 7 8
''' ┌─────────┐ │ A │ │ │ │ ♥ │ │ │ │ A │ └─────────┘'''
res = "" for i in out: suits_order = (i >> 5) % 4 num_order = (i >> 6) % 13 res += (table_suits[suits_order] + "_" + table_num[num_order] + " ")
return res sequence = [] r = remote("59.110.20.54",4953) r.recvuntil(b"input your option:") r.sendline(b"1") r.recvuntil(b"gift: ") sequence += eval(r.recvline().decode().strip()) r.recvuntil(b"gift: ") sequence += eval(r.recvline().decode().strip()) m,c,n = solve_LCG(sequence) out = getnext(sequence,m,c,n) res = gen_card(out) r.recvuntil(b"(example: Heart_1 Club_2 Diamond_3)") r.sendline(res.encode())
for i in trange(49): out = getnext(out,m,c,n) res = gen_card(out) r.recvuntil(b"(example: Heart_1 Club_2 Diamond_3)") r.sendline(res.encode()) r.recvuntil(b"The flag is your reward!") r.recvline() print(r.recvline())
#SYC{lcg_a@@@@@ttack}
Week 4
EzComplex
题目描述:
1
And u, my friend: Complex factors! (In a double sense)
题目:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#sage9.3 from Crypto.Util.number import * flag = b'FAKE{Do_You_know_Complex_numbers}' p = random_prime(1 << 384) q = random_prime(1 << 384) n = p * q e = 0x10001 N = pow(p, 2) + pow(q, 2) m = bytes_to_long(flag) c = pow(m,e,n)
c = 122977267154486898127643454001467185956864368276013342450998567212966113302012584153291519651365278888605594000436279106907163024162771486315220072170917153855370362692990814276908399943293854077912175867886513964032241638851526276 N = 973990451943921675425625260267293227445098713194663380695161260771362036776671793195525239267004528550439258233703798932349677698127549891815995206853756301593324349871567926792912475619794804691721625860861059975526781239293017498 e = 65537
zn = ZZ[i](N) for d in divisors(zn): p = int(d[0]) q = int(d[1]) if isPrime(p) and isPrime(q) and p.bit_length() > 380: break phi = (p-1)*(q-1) n = p*q d = inverse(e,phi) print(long_to_bytes(int(pow(c,d,n))))
Hi Crypto-ers! AL3XEI here. Solving extended gcd over 2 primes can be easy, but what about 7 primes? primes : [938770989594038574883307091377, 731124780451059048515699872973, 705906522337130351303390881793, 1259253321424214851154594220511, 638192849150199238389371476021, 1160157641563492827848830377527, 1225294646722407790694425867847] ( 100 bits ) Give me a0,...a5,a6:
from Crypto.Util.number import * from pwn import * from tqdm import * from hashlib import sha256,sha1 import string from gmpy2 import gcdext
#context.log_level = 'debug'
defproof_of_work(): table = string.digits + string.ascii_letters temp = r.recvuntil(b"sha256(XXXX+") temp = r.recvline() suffix = temp[:16].decode() hex1 = temp[20:].strip().decode() for i in tqdm(table): for j in table: for k in table: for m in table: temp1 = i+j+k+m if(sha256((temp1+suffix).encode()).hexdigest() == hex1): r.sendline(temp1.encode()) return r = remote("59.110.20.54", 1789) proof_of_work()
#part1 get key Get_key = p,M,c = Get_key["p"],Get_key["M"],Get_key["c"]
MM = matrix(ZZ,32,16,M) E = diagonal_matrix([1]*32) P = diagonal_matrix([p]*16) C = matrix(ZZ,c) L = block_matrix(ZZ,[[MM,E],[P,0],[C,0]]) ML = L.LLL() key = list(ML[1][16:]) for i inrange(len(key)): key[i] = abs(key[i]) key = bytes(key)
#part2 get e PR.<i> = PolynomialRing(ZZ) f = i^3 - 10*i^2 + 31*i - 30 res = f.roots() e = [i[0] for i in res]