from Crypto.Util.number import getPrime, bytes_to_long, inverse, long_to_bytes from Crypto.PublicKey import DSA from hashlib import sha256 import random from secret import flag
defgen(a): p = getPrime(a) q = getPrime(a) r = getPrime(a) x = getPrime(a) n = p*q*r*x phi = (p-1)*(q-1)*(r-1)*(x-1)
return n, phi, [p, q, r, x]
defsign(m, k, x, p, q, g): hm = bytes_to_long(sha256(m).digest()) r = pow(g, k, p) % q s = (hm + x*r) * inverse(k, q) % q
return r,s
e = 65537 a = 256 x = bytes_to_long(flag) # print(x)
import hashlib from Crypto.Util.number import * from gmpy2 import * FLAG = b'******************' assert FLAG.startswith(b'NSSCTF{') and FLAG.endswith(b'}') FLAG = FLAG[7:-1]
defsign(msg, pub, pri, k): (p,q,g,y) = pub x = pri r = int(pow(g, k, p) % q) h = int(hashlib.sha256(msg).digest().hex(),16) s = int((h + x * r) * invert(k, q) % q) return (r, s)
p = 12521300600879647215212622604478307167614802603722694432672492518732844280050451647647783544918670551333939947202324314036106883627652934658092246151569719841172139651756731975948641752941369320985906043813128667949407263418091261521078015038472125264708315959943830171678415621896727622381651264882655845219115471323352719455064712014904581019529062436850895982568432772820884304927292484611574315452532539622439874476205201585972439739033662281856954069390915294301650596122027017512657387126870291348326594014789938826560641601265413964203409968207292456857314148848395645091850604205535043035332961436498283695843 q = 89333150710898097819726085329049525002843220030438497258487456281988064920981 g = 4659169190462665152432024005060362819268084070474399613244522271693166269703240438309526888954293382169994621221386886590606442329876391429681914154130453072540079426475110538234340272617964838185872575922598867083747162403217264242469640383596415974818773608247780785279490355462362699968367544837511541267300331418420849521244364899679912282991493675152166261501255315036943693486335864565853496499243373834189894710718862409646613179068080762011713847012853815796678705445232715083564615906424779193638984542271665075749327914765645357163924702265124479067028868769369414557728443665123548337757950887923116453268 x = bytes_to_long(FLAG) y = powmod(g, x, p)
IV = 0x7380166f4914b2b9172442d7da8a0600a96f30bc163138aae38dee4db0fb0e4e default_hm1 = b'HZNUCTFRound#1' default_hm2 = b'HZNUCTFRound#1'
defsolve_k(l): for i inrange(512): if (l + 1 + i - 448) % 512 == 0: return i
defpadding(msg): l = len(msg) k = solve_k(l) l64 = bin(l)[2:].rjust(64, '0') # what if msg is too long, how? # so mention the condition in guideline of Algorithm msg = msg + '1' + '0' * k + l64 assertlen(msg) % 512 == 0 return msg, l, k
defiteration(): pass
defring_shift_left(x, num): x = bin(x)[2:].rjust(32, '0') # shift in 32 bit, take care of it x = int(x[num:] + x[:num], 2) return x
defp0(x): return x ^ ring_shift_left(x, 9) ^ ring_shift_left(x, 17)
defp1(x): return x ^ ring_shift_left(x, 15) ^ ring_shift_left(x, 23)
defextending(msg): WW = [] for i inrange(len(msg) // 512): W = ['0'for _ inrange(132)] msgi = msg[i * 512:(i + 1) * 512] # 512 ====> 16 * 32 for ii inrange(len(msgi) // 32): W[ii] = msgi[ii * 32:(ii + 1) * 32] assertlen(W[ii]) == 32 for j inrange(16, 68): # how to xor word, change to number or bytes? # number, of course W[j] = p1(int(W[j - 16], 2) ^ int(W[j - 9], 2) ^ ring_shift_left(int(W[j - 3], 2), 15)) ^ \ ring_shift_left(int(W[j - 13], 2), 7) ^ int(W[j - 6], 2) W[j] = bin(W[j])[2:] W[j] = W[j].rjust(32, '0') for j inrange(68, 132): W[j] = int(W[j - 68], 2) ^ int(W[j - 68 + 4], 2) W[j] = bin(W[j])[2:] W[j] = W[j].rjust(32, '0') WW.append(W) return WW
defbool_ff(param, j): x, y, z = param if0 <= j <= 15: return x ^ y ^ z elif16 <= j <= 63: return (x & y) | (x & z) | (y & z)
defbool_gg(param, j): x, y, z = param if0 <= j <= 15: return x ^ y ^ z elif16 <= j <= 63: # take care of not iflen(bin(x)[2:]) < 32: ans = '' x = bin(x)[2:].rjust(32, '0') y = bin(y)[2:].rjust(32, '0') z = bin(z)[2:].rjust(32, '0') for i inrange(0, 32): if x[i] == '0': ans += str((int(x[i], 2) & int(y[i], 2)) | (1 & int(z[i], 2))) elif x[i] == '1': ans += str((int(x[i], 2) & int(y[i], 2)) | (0 & int(z[i], 2))) returnint(ans, 2) eliflen(bin(x)[2:]) == 32: return (x & y) | (~x & z)
defcf(v, w): W, W_ = w[:68], w[68:] # for i in W: # print(hex(int(i, 2)), end=' ') # for i in W_: # print(hex(int(i, 2)), end=' ') # print() v = bin(v)[2:].rjust(256, '0') A, B, C, D, E, F, G, H = [v[_ * 32:(_ + 1) * 32] for _ inrange(256 // 32)] for j inrange(64): # be care of plus, it may surpass 32 bits # remember j mod 32, or output will be wrong tmp = (ring_shift_left(int(A, 2), 12) + int(E, 2) + ring_shift_left(cons(j), j % 32)) & 0xffffffff SS1 = ring_shift_left(tmp, 7) SS1 = bin(SS1)[2:].rjust(32, '0')
D = C.rjust(32, '0') C = ring_shift_left(int(B, 2), 9) C = bin(C)[2:].rjust(32, '0') B = A.rjust(32, '0') A = TT1.rjust(32, '0') H = G.rjust(32, '0') G = ring_shift_left(int(F, 2), 19) G = bin(G)[2:].rjust(32, '0') F = E.rjust(32, '0') E = p0(int(TT2, 2)) E = bin(E)[2:].rjust(32, '0')
# print(j, end=' ') # for i in [A, B, C, D, E, F, G, H]: # print(hex(int(i, 2)), end=' ') # print() ans = A + B + C + D + E + F + G + H ans = int(ans, 2) return ans
from Crypto.Util.number import * import itertools import gmpy2
defsmall_roots(f, bounds, m=1, d=None): ifnot d: d = f.degree()
R = f.base_ring() N = R.cardinality()
f /= f.coefficients().pop(0) f = f.change_ring(ZZ)
G = Sequence([], f.parent()) for i inrange(m + 1): base = N ^ (m - i) * f ^ i for shifts in itertools.product(range(d), repeat=f.nvariables()): g = base * prod(map(power, f.variables(), shifts)) G.append(g)
factors = [monomial(*bounds) for monomial in monomials] for i, factor inenumerate(factors): B.rescale_col(i, factor)
B = B.dense_matrix().LLL()
B = B.change_ring(QQ) for i, factor inenumerate(factors): B.rescale_col(i, 1 / factor)
H = Sequence([], f.parent().change_ring(QQ)) for h infilter(None, B * monomials): H.append(h) I = H.ideal() if I.dimension() == -1: H.pop() elif I.dimension() == 0: roots = [] for root in I.variety(ring=ZZ): root = tuple(R(root[var]) for var in f.variables()) roots.append(root) return roots
return []
(r1, s1) = (43665657147136977892760835332544097729763754398125679419859037123212964274095, 11372107439153704547599978617809027960018057676066118055075660375442954789009) (r2, s2) = (29184887007213204285288676779168140587575609668559831035949650649308618592275, 5011738292572181542092375902756977363590922060964162373234404450451520414798) p = 31961141251107494919420190534228520246958409864267239760354623819192809291490262139213317490432416411403367763443527530375117617196123131270496004125231254335150221348901335274505489844222882171272650010562960614279185073793274638651086760235178963210965828168433516820007716846876686795459738332444629111764967204355463398049697867061034126529189537688874999118692225915790053920062142349951686250122300061810240375783724631961234942175580462986265098353263395579346466921241016500821787793395554444982717141449909744838267161237273856377774256250949274635575801148994817767751541256849860886577256992383324866941911 q = 69375998045163628324086568160767337544901252262545889505892695427466730978301 g = 23095306638137759877487469277470910487928442296144598697677211337473146684728707820084075779044942034329888686699655576145455963231144004571165817481066424910959951439014314776050521403558035997997820617824839889597136772108383034876458141163933312284054415480674388788905935457149956424898637134087874179010376667509489926236214865373552518669840236207944772752416668193786003948717604980584661094548997197117467440864460714843246250800575997370964173558788145639802963655916833143883799542309432910222224223561677245110195809587171802538978009246887077924173034608600837785506594525481696000424121705524449481831586 y = 30195133393879069638917191223585579396119430591488890396938821804398771785068454607425044458865556053274470709839502680269466948174813926392729790863065933078609827279352860810689776644132512095691760326095517755483748554008211568781998662554432781285208646921699265866446498342049913829592480268053599307065979016922204438675164034767731708343084371572648019835171087671868322447023378942812010740490724160077164191297435291229504616686997442254543493394641023587237077429236872101951650325361004443988267286616139798736713430746804524113024341440435623834197278500144543476528466395780355874841379098027115073850819 h = 19905280947443115569469777697852124038269468456842113763109865796452965095134
flag = "flag{" + hashlib.sha256(str(pri).encode()).hexdigest() + "}"
for i inrange(5): r,s = sign(str(i).encode(),pri) print(f"r = {r}") print(f"s = {s}") """ p = q = g = pub = r = s = r = s = r = s = r = s = r = s = """
import os import hashlib from Crypto.Util.number import * from Crypto.PublicKey import DSA import random defgen_proof_key(): password = 'happy_the_year_of_loong' getin = '' for i in password: if random.randint(0, 1): getin += i.lower() else: getin += i.upper() ans = hashlib.sha256(getin.encode()).hexdigest() return getin,ans
defsign(m,pri): k = int(hashlib.md5(os.urandom(20)).hexdigest(),16) H = int(hashlib.sha256(m).hexdigest(),16) r = pow(g,k,p) % q s = pow(k,-1,q) * (H + pri * r) % q return r,s
defverify(pub,m,signature): r,s = signature if r <= 0or r >= q or s <= 0or s >= q: returnFalse w = pow(s,-1,q) H = int(hashlib.sha256(m).hexdigest(),16) u1 = H * w % q u2 = r * w % q v = (pow(g,u1,p) * pow(pub,u2,p) % p) % q return v == r deflogin(): print('Hello sir,Plz login first') menu = ''' 1.sign 2.verify 3.get my key ''' times = 8 whileTrue: print(menu) if times < 0: print('Timeout!') returnFalse choice = int(input('>')) if choice == 1: name = input('Username:').encode() ifb'admin'in name: print('Get out!') returnFalse r,s = sign(name,pri) print(f'This is your signature -- > {r},{s}') times -= 1 elif choice == 2: print('Sure,Plz input your signature') print(pri) r = int(input('r:')) s = int(input('s:')) if verify(pub,b'admin',(r,s)) == True: print('login success!') returnTrue else: print('you are not admin') returnFalse elif choice == 3: print(f'Oh,your key is {(p,q,g)}') getin,ans = gen_proof_key() print(f'Your gift --> {ans[:6]}') your_token = input('Plz input your token\n>') if your_token != getin: print('Get out!') exit(0)
key = DSA.generate(1024) p, q, g = key.p, key.q, key.g pri, pub = gen_key() if login() == False: exit(0) print(open('/flag','r').read())
import hashlib import itertools from tqdm import * from pwn import * from Crypto.Util.number import *
defpass_proof(head): password = 'happytheyearofloong' table = itertools.product([0,1],repeat=19) for i in tqdm(table): getin = "" for j inrange(len(i)): if i[j] == 0: getin += password[j].lower() else: getin += password[j].upper() msg = getin[:5] + "_" + getin[5:8] + "_" + getin[8:12] + "_" + getin[12:14] + "_" + getin[14:] h = hashlib.sha256(msg.encode()).hexdigest() if h[:6] == head: print(msg) return msg sh = remote("8.147.132.12",41792) head = sh.recvline().strip().decode().split(" ")[-1] msg = pass_proof(head) sh.recvuntil(b"Plz input your token") sh.sendlineafter(b">",msg.encode()) sh.recvuntil(b"3.get my key\n") sh.sendlineafter(b">",b"3") (p,q,g) = eval(sh.recvline().strip().decode().split("Oh,your key is ")[-1])
H = [] R = [] S = []
for i inrange(8): name = b"a"*(i+1) sh.recvuntil(b"3.get my key\n") sh.sendlineafter(b">",b"1") sh.sendlineafter(b"Username:",name) data = sh.recvline().strip().decode() print(data) r = int(data.split(" ")[-1].split(',')[0]) s = int(data.split(" ")[-1].split(',')[1]) h = int(hashlib.sha256(name).hexdigest(),16) R.append(r) S.append(s) H.append(h)
defget_k(): n = len(R) r0 = R[0] h0 = H[0] s0 = S[0] A = [] B = []
for i inrange(n): a = inverse((r0 * S[i]),q) * (R[i] * s0) % q b = inverse((r0 * S[i]),q) * (H[i]*r0 - h0 * R[i]) A.append(a) B.append(b) Ge = Matrix(ZZ,n+2,n+2) for i inrange(n): Ge[i,i] = q Ge[-2,i] = A[i] Ge[-1,i] = B[i] K = 2**128 Ge[-2,-2] = 1 Ge[-1,-1] = K
for line in Ge.LLL(): ifabs(line[-1]) == K: return line[-2]
k0 = get_k() print(f"k0 = {k0}") sh.recvuntil(b"3.get my key\n") sh.sendlineafter(b">",b"2") sh.recvline() x = int(sh.recvline().strip().decode()) r = pow(g,k0,p) % q hh = int(hashlib.sha256(b"admin").hexdigest(),16) s = pow(k0,-1,q) * (hh + x*r) % q sh.sendlineafter(b"r:",str(r).encode()) sh.sendlineafter(b"s:",str(s).encode()) print(sh.recvline().strip().decode()) print(sh.recvline().strip().decode())
from Crypto.Util.number import * from Crypto.PublicKey import DSA from random import randint import hashlib from sage.allimport *
defgen_key(): pri = randint(2,q-2) pub = pow(g,pri,p) return pri,pub
defsign(khigh,m,pri): k = int(khigh + bin(getPrime(120))[2:],2) H = int(hashlib.sha256(m).hexdigest(),16) r = pow(g,k,p) % q s = pow(k,-1,q) * (H + pri * r) % q return r,s
key = DSA.generate(1024) p, q, g = key.p, key.q, key.g pri, pub = gen_key() khigh = bin(getPrime(8))[2:].zfill(8)
R = [] S = []
for i inrange(5): r,s = sign(khigh,str(i).encode(),pri) R.append(r) S.append(s) H = [int(hashlib.sha256(str(i).encode()).hexdigest(),16) for i inrange(5)]
for j inrange(256): A = [(S[0]*R[i] * inverse((S[i]*R[0]),q)) % q for i inrange(5)] B = [(H[i]*R[0] - H[0]*R[i] + S[0]*R[i]*2**120*j - S[i]*R[0]*2**120*j) * inverse((S[i]*R[0]),q) % q for i inrange(5)] n = len(A) K = 2**120
Ge = Matrix(ZZ,n+2,n+2) for i inrange(n): Ge[i,i] = q Ge[-2,i] = A[i] Ge[-1,i] = B[i] Ge[-2,-2] = 1 Ge[-1,-1] = K
for line in Ge.LLL(): if line[-1] == K: k_low = abs(line[-2]) k0 = j * 2**120 + k_low x = (k0 * S[0] - H[0]) * inverse(R[0],q) % q ifpow(g,x,p) == pub: print(x)
from Crypto.Util.number import * from Crypto.PublicKey import DSA from random import randint import hashlib from sage.allimport *
defgen_key(): pri = randint(2,q-2) pub = pow(g,pri,p) return pri,pub
defsign(klow,m,pri): k = int(bin(getPrime(120))[2:] + klow,2) H = int(hashlib.sha256(m).hexdigest(),16) r = pow(g,k,p) % q s = pow(k,-1,q) * (H + pri * r) % q return r,s
key = DSA.generate(1024) p, q, g = key.p, key.q, key.g pri, pub = gen_key() klow = bin(getPrime(8))[2:].zfill(8)
R = [] S = []
for i inrange(5): r,s = sign(klow,str(i).encode(),pri) R.append(r) S.append(s) H = [int(hashlib.sha256(str(i).encode()).hexdigest(),16) for i inrange(5)]
for j inrange(256): A = [((S[0]*R[i]*2**8) * inverse((S[i]*R[0]*2**8),q)) % q for i inrange(5)] B = [(H[i]*R[0] - H[0]*R[i] + S[0]*R[i]*j - S[i]*R[0]*j) * inverse((S[i]*R[0]*2**8),q) % q for i inrange(5)] n = len(A) K = 2**120
Ge = Matrix(ZZ,n+2,n+2) for i inrange(n): Ge[i,i] = q Ge[-2,i] = A[i] Ge[-1,i] = B[i] Ge[-2,-2] = 1 Ge[-1,-1] = K
for line in Ge.LLL(): if line[-1] == K: k0_high = abs(line[-2]) k0 = k0_high * 2**8 + j x = (k0 * S[0] - H[0]) * inverse(R[0],q) % q ifpow(g,x,p) == pub: print(x)
from Crypto.Util.number import * from Crypto.PublicKey import DSA from random import randint import hashlib from sage.allimport *
defgen_key(): pri = randint(2,q-2) pub = pow(g,pri,p) return pri,pub
defsign(khigh,klow,m,pri): k = int(khigh + bin(getPrime(120))[2:] + klow,2) H = int(hashlib.sha256(m).hexdigest(),16) r = pow(g,k,p) % q s = pow(k,-1,q) * (H + pri * r) % q return r,s
key = DSA.generate(1024) p, q, g = key.p, key.q, key.g pri, pub = gen_key() klow = bin(getPrime(4))[2:].zfill(4) khigh = bin(getPrime(4))[2:].zfill(4)
R = [] S = []
for i inrange(5): r,s = sign(khigh,klow,str(i).encode(),pri) R.append(r) S.append(s) H = [int(hashlib.sha256(str(i).encode()).hexdigest(),16) for i inrange(5)]
for high inrange(16): for low inrange(16): A = [((S[0]*R[i]*2**4) * inverse((S[i]*R[0]*2**4),q)) % q for i inrange(5)] B = [(H[i]*R[0] - H[0]*R[i] + S[0]*R[i]*(high*2**124 + low) - S[i]*R[0]*(high*2**124 + low)) * inverse((S[i]*R[0]*2**4),q) % q for i inrange(5)] n = len(A) K = 2**120
Ge = Matrix(ZZ,n+2,n+2) for i inrange(n): Ge[i,i] = q Ge[-2,i] = A[i] Ge[-1,i] = B[i] Ge[-2,-2] = 1 Ge[-1,-1] = K
for line in Ge.LLL(): if line[-1] == K: k0_unknown = abs(line[-2]) k0 = high * 2**124 + k0_unknown * 2**4 + low x = (k0 * S[0] - H[0]) * inverse(R[0],q) % q ifpow(g,x,p) == pub: print(x)
from random import getrandbits, randint from Crypto.Util.number import getPrime, isPrime, inverse from hashlib import sha1 import signal
defgen(l, n): q = getPrime(l) whileTrue: t = getrandbits(n - l) p = t * q + 1 if isPrime(p): break h = randint(1, p - 1) g = pow(h, t, p) x = randint(1, q) y = pow(g, x, p) return (p, q, g, y), x
defsign(pubkey, x, msg, lsb, msb): p, q, g, y = pubkey k = gen_ephemeral_key(150, lsb, msb) r = pow(g, k, p) % q Hm = int(sha1(msg).hexdigest(), 16) s = (Hm + x * r) * inverse(k, q) % q return (r, s)
defverify(pubkey, sig, msg): p, q, g, y = pubkey r, s = sig ifnot0 < r < q ornot0 < s < q: returnFalse w = inverse(s, q) Hm = int(sha1(msg).hexdigest(), 16) u1 = Hm * w % q u2 = r * w % q v = pow(g, u1, p) * pow(y, u2, p) % p % q return v == r
signal.alarm(900) withopen("flag.txt", "r") as f: flag = f.read()
l, n = 160, 1024 pub, x = gen(l, n) print("your pubKey: {}".format(pub)) msb = getrandbits(8) lsb = getrandbits(2)
menu = """ [1] sign message [2] verify signature """
for i inrange(20): print(menu) op = int(input(">").strip()) if op == 1: msg = input("Which message to sign?: ").strip().encode() if msg == b"get flag": print("I'm afraid I can't do that.") break else: sig = sign(pub, x, msg, lsb, msb) print(f"Signature: {sig}") elif op == 2: msg = input("Which message to verify?: ").strip().encode() r = int(input("r:").strip()) s = int(input("s:").strip()) v = verify(pub, (r, s), msg) if v and msg == b"get flag": print(flag) else: print(v) else: print("Invalid option")
from random import choices from hashlib import sha1 from Crypto.Util.number import * import string from pwn import * from sage.allimport * from tqdm import * import gmpy2 import time
table = string.ascii_lowercase
host = ''#ip地址 port = #端口
sh = remote(host,port) #建立连接 sh.recvuntil(b"your pubKey:")
pub = eval(sh.recvline().decode().strip())
p,q,g,y = pub
R = [] S = [] H = [] for i inrange(19): sh.recvuntil(b">") sh.sendline(b"1") sh.recvuntil(b"Which message to sign?: ") m = "".join(choices(table,k=16)) msg = m.encode() h = bytes_to_long(sha1(msg).digest()) sh.sendline(msg) sh.recvuntil(b"Signature:") data1 = eval(sh.recvline().decode().strip()) r,s = data1 S.append(s) R.append(r) H.append(h)
n = len(S) r0 = R[0] s0 = S[0] h0 = H[0]
defsign(pubkey, x, msg, k): p, q, g, y = pubkey r = pow(g, k, p) % q Hm = int(sha1(msg).hexdigest(), 16) s = (Hm + x * r) * inverse(k, q) % q return (r, s)
for high in trange(256): for low inrange(4): lowbit = low.bit_length() A = [] B = [] tt = 2**lowbit for i inrange(1,len(R)): a = tt*R[i]*s0 * gmpy2.invert(tt*r0*S[i],q) % q b = (r0*H[i] - R[i]*h0 + R[i]*s0*(high*2**152+low) - r0*S[i]*(high*2**152+low)) * gmpy2.invert(tt*r0*S[i],q) % q A.append(a) B.append(b) n = len(A) Ge = Matrix(ZZ,n+2,n+2) for i inrange(n): Ge[i,i] = q Ge[-2,i] = A[i] Ge[-1,i] = B[i] K = 2**150 Ge[-2,-2] = 1 Ge[-1,-1] = K for line in Ge.BKZ(block_size=30): ifabs(line[-1]) == K: k0_unknown = line[-2] k0 = high*2**152 + k0_unknown*tt + low d = (k0 * s0 - h0) * gmpy2.invert(r0,q) % q ifpow(g,d,p) == y: print(1) sig = sign(pub,d,b"get flag",k0) r,s = sig sh.recvuntil(b">") sh.sendline(b"2") sh.recvuntil(b"Which message to verify?: ") sh.sendline(b"get flag") sh.sendlineafter(b"r:",str(r).encode()) sh.sendlineafter(b"s:",str(s).encode()) print(sh.recvline())
if __name__ == '__main__': n = 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141 a = random.randint(0,n) flag = 'flag{' + str(a) + "}" b = random.randint(0,n) print(ecdsa_test(a,b))