from Crypto.Util.number import * from secret import flag
m = bytes_to_long(flag)
defgen(bits): whileTrue: a = getPrime(bits) b = getPrime(bits) c = getPrime(bits) p = (a << (2*bits)) + (b << bits) + c q = (c << (2*bits)) + (a << bits) + b if isPrime(p) and isPrime(q): break n = p * q e = 65537 * a * b * c return n, e
n, e = gen(256) enc = pow(m, e, n) print(f'n = {n}') print(f'e = {e}') print(f'enc = {enc}')
""" n = 1224562468550864572988516321107388462006452125881847529675226398144888628055678744854491489016309262856785169494723943649344959507818155642772331582922466943539371681776924160647697558836379614689120727659593775446187326222964118917872973684996317614900715927751822277949264379149585370840318817143291878609357893969588131470982041272505875501444442064552286330626234504767040724907034678080283717062342383737341651784574675215207283219694413200065153603535550259 e = 47356701171507751941853094934330097161634963503549196148254287987823089762869775349307331223083118848869825102126184149696632299476124764277876323238594318983922914255635452587035212905468593961720866809724369270149104325019013500377581 enc = 307839781648837102719329833689146078918113606357673952357833605392673923316706392387378621203382529480917019155723632239435123748698548640308486267420983085309284306889248702165586731118889200017606360233948688879034822132551452439147516062116990766999765714755923073387252339782026780490661436777023426366620269445376047876173655782230659201893151372247389482285331969025687842851498151565880029377050013378302485301558801016888957357366922840214729734193614497 """
n = 1224562468550864572988516321107388462006452125881847529675226398144888628055678744854491489016309262856785169494723943649344959507818155642772331582922466943539371681776924160647697558836379614689120727659593775446187326222964118917872973684996317614900715927751822277949264379149585370840318817143291878609357893969588131470982041272505875501444442064552286330626234504767040724907034678080283717062342383737341651784574675215207283219694413200065153603535550259 e = 47356701171507751941853094934330097161634963503549196148254287987823089762869775349307331223083118848869825102126184149696632299476124764277876323238594318983922914255635452587035212905468593961720866809724369270149104325019013500377581 enc = 307839781648837102719329833689146078918113606357673952357833605392673923316706392387378621203382529480917019155723632239435123748698548640308486267420983085309284306889248702165586731118889200017606360233948688879034822132551452439147516062116990766999765714755923073387252339782026780490661436777023426366620269445376047876173655782230659201893151372247389482285331969025687842851498151565880029377050013378302485301558801016888957357366922840214729734193614497
#part1 factor n bcl = n % 2**256 ach = n // 2**(1024+256) * 2**256
for i inrange(10): b = e // ach // 65537 + i if(GCD(b,e) != 1): break
ac = e // 65537 // b a = inverse(bcl,2**256)*(e//65537%2**256) % 2**256
c = e // a // b // 65537
#part2 get flag bits = 256
p = (a << (2*bits)) + (b << bits) + c q = (c << (2*bits)) + (a << bits) + b phi = (p-1)*(q-1)
d = inverse(e,phi) print(long_to_bytes(int(pow(enc,d,n))))
#ctfpunk{2ffab167-af57-49d2-ba91-81d9864a98ef}
Masquerade
题目描述:
1
Masquerade🎭! Admission by ticket!
题目:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
from Crypto.PublicKey import DSA from Crypto.Util.number import * FLAG = "ctfpunk{XXXX_FAKE_FLAG_XXXX}"
print(Ticket_Office._key) Ticket = list(map(int, input("Gimme your ticket: ").split(','))) assert Auth_Code in long_to_bytes(Ticket[2]) \ and Ticket[2]%int(Ticket_Office._key['p'])
if Ticket_Office._verify(Ticket[2], Ticket[:2]): print(f"Welcome to the masquerade, Enjoy it! {FLAG}")
def_sign(self, m, k): ifnot self.has_private(): raise TypeError("DSA public key cannot be used for signing") ifnot (1 < k < self.q): raise ValueError("k is not between 2 and q-1")
x, q, p, g = [self._key[comp] for comp in ['x', 'q', 'p', 'g']]
r = pow(g, k, p) % q # r = (g**k mod p) mod q s = (inv_blind_k * (blind_factor * m + blind_x * r)) % q returnmap(int, (r, s))
def_verify(self, m, sig): r, s = sig y, q, p, g = [self._key[comp] for comp in ['y', 'q', 'p', 'g']] ifnot (0 < r < q) ornot (0 < s < q): returnFalse w = Integer(s).inverse(q) u1 = (w * m) % q u2 = (w * r) % q v = (pow(g, u1, p) * pow(y, u2, p) % p) % q return v == r
from Crypto.Util.number import * from Crypto.PublicKey import ElGamal from Crypto.Cipher import AES import os FLAG = "ctfpunk{XXXX_FAKE_FLAG_XXXX}" FLAG = bytes_to_long(FLAG[8:-1].encode())
MENU = """ /----------------------------\\ | options | | [O]racle | | [G]et FLAG | \\----------------------------/ """
sys = Oracle() print(MENU) whileTrue: op = input("> ").lower() if op == 'o': data = list(map(int, input("data: ").split(','))) print(f"[+] {sys.query(data).hex()}") if op == 'g': r = getRandomNBitInteger(512) print(f"[*]\nc = {sys.Cipher0._encrypt(FLAG, r)}\np = {sys.Cipher0.p}")
T = 256^(63-21) L = matrix(ZZ,len(a)+1,len(a)+1) L[0,0] = T for i inrange(len(a)): L[0,i+1] = a[i][0] L[i+1,i+1] = a[i][1] res = L.BKZ()[0] print(long_to_bytes(abs(int(res[0]//T))))