// key code (oldtest) PrintWriter localPrintWriter = new PrintWriter("old.txt", "UTF-8"); for (int i = 0; i < 20; i++) { int j = this.random.nextInt(); localPrintWriter.println(j); } localPrintWriter.close();
// key code (initseed) Scanner localScanner = new Scanner(Paths.get("flag", new String[0]), "UTF-8"); String str1 = localScanner.next(); String str2 = convert_2_binary(str1); this.seed = str2; // so what we need is `this.seed`
// key code (generate_init_state) BigInteger localBigInteger = BigInteger.valueOf(0L); char[] arrayOfChar1 = this.seed.toCharArray(); // seed's first encryption for (int k : arrayOfChar1) { localBigInteger = localBigInteger.shiftLeft(1); if (k == 49) { // k == '1' localBigInteger = localBigInteger.xor(new BigInteger(this.seed, 2)); } if (localBigInteger.shiftRight(256) != BigInteger.ZERO) { localBigInteger = localBigInteger.xor(new BigInteger("10000000000000000000000000000000000000000000000000000000000000223", 16)); } } return localBigInteger;
// key code (gen_states) BigInteger localBigInteger1 = generate_init_state(); // what we want(seed after first encryption) BigInteger localBigInteger2 = BigInteger.valueOf(17L); // param e in RSA ArrayList localArrayList1 = new ArrayList(24); ArrayList localArrayList2 = new ArrayList(24); for (int i = 0; i < 24; i++) { BigInteger localBigInteger3 = BigInteger.probablePrime(512, this.random); // p BigInteger localBigInteger4 = BigInteger.probablePrime(512, this.random); // q BigInteger localBigInteger5 = localBigInteger3.multiply(localBigInteger4); // n BigInteger localBigInteger6 = localBigInteger1.modPow(localBigInteger2, localBigInteger5); // c localArrayList1.add(localBigInteger5); localArrayList2.add(localBigInteger6); } try { PrintWriter localPrintWriter = new PrintWriter("product", "UTF-8"); for (int j = 0; j < 24; j++) { localPrintWriter.println(((BigInteger)localArrayList1.get(j)).toString()); this.states.add(localArrayList2.get(j)); // states storing seed after second encryption(RSA) } localPrintWriter.close(); }
// key code (lrandout) if (this.stateselse > 0) { // stateselse's initiation is 24 this.stateselse -= 1; BigInteger localBigInteger = (BigInteger)this.states.get(this.statespoint); this.statespoint += 1; return stateconvert(localBigInteger); // return seed after third encryption(AES) } // stateconvert contains `encrypt` function gen_new_states(); return lrandout();
import re from Crypto.Cipher import AES from binascii import unhexlify from Crypto.Util.number import *
defgetData(): pqs = [] with open('prikey', 'r') as fRSA: for line in fRSA: pq = re.findall(r"\((.*), (.*)\)", line)[0] pqs.append((int(pq[0]), int(pq[1]))) with open('AESkey', 'r') as fAES: key = [unhexlify(line.strip()) for line in fAES] key = [key[:24], key[24:48], key[48:]] with open('new.txt', 'r') as fCIPHER: cipher = [unhexlify(line.strip()) for line in fCIPHER] return pqs, key, cipher
defdec1(p, q, c, key1, key2): aes2 = AES.new(key2, AES.MODE_CBC, iv=b"\x00"*16) c = aes2.decrypt(c) aes1 = AES.new(key1, AES.MODE_CBC, iv=b"\x00"*16) c = bytes_to_long(aes1.decrypt(c)) e = 17 n = p * q phi = (p - 1) * (q - 1) d = inverse(e, phi) m = pow(c, d, n) return m
defmul(x): a = 0 for i in bin(x)[2:]: a = a << 1 if (int(i)): a = a ^ x if a >> 256: a = a ^ 0x10000000000000000000000000000000000000000000000000000000000000223 return a
defdec2(m): whileTrue: m = mul(m) ifb"flag"in long_to_bytes(m): print(long_to_bytes(m)) break
from hashlib import sha256 from Crypto.Util.number import *
# genKey for DSA q = getPrime(128) whileTrue: t = getRandomNBitInteger(1024 - 128 - 1) p = (t * 2*q + 1) if isPrime(p): break e = (p - 1) // q g = pow(2, e, p) x = getRandomRange(1, q - 1) y = int(pow(g, x, p))
defsign(Hm): k = getRandomRange(1, 2**121) r = int(pow(g, k, p)) % q s = (inverse(k, q) * (Hm + x * r)) % q return r, s
defcrack_x(): M_list = [] for i in range(35): M_list.append([q * int(i == j) for j in range(37)]) Hm = int.from_bytes(sha256(b"0xDktb").digest(), 'big') As = [] Bs = [] for i in range(35): r, s = sign(Hm) A = (inverse(s, q) * Hm) % q B = (inverse(s, q) * r) % q As.append(A) Bs.append(B) Bs = Bs + [1/(2**64), 0] As = As + [0, 2**64] M_list.append(Bs) M_list.append(As) M = Matrix(QQ, M_list) ML = M.LLL() for line in ML: if line[-1] == (2**64): prikey = (line[-2] * (2**64)) % q print("[+] SVP's solution found! prikey = {}".format(prikey)) return prikey
for i in range(32): if x != crack_x(): print("Failed")
from sys import argv from hashlib import sha256 from Crypto.Util.number import * from binascii import hexlify, unhexlify from pwn import * from pwnlib.util.iters import mbruteforce
defsign(m): io.sendlineafter("$ ", "1") io.sendlineafter("Please input your username: ", m) io.recvuntil("k.bit_length() == ") kbits = int(io.recvline().strip().decode()) io.recvuntil("Here is your signature in hex: ") sig = io.recvline().strip().decode("latin-1") r = int(sig[2*len(m):2*(len(m)+20)], 16) s = int(sig[2*(len(m)+20):], 16) return r, s, kbits
deflogin_as_admin(pubkey, prikey): g, p, q, x = pubkey['g'], pubkey['p'], pubkey['q'], prikey k = 114514 Hm = int.from_bytes(sha256(b"admin").digest(), 'big') r = int(pow(g, k, p)) % q s = (inverse(k, q) * (Hm + x*r)) % q payload = hexlify(b"admin" + long_to_bytes(r).rjust(20, b"\x00") + long_to_bytes(s).rjust(20, b"\x00")) io.sendlineafter("$ ", "2") io.sendlineafter("Please send me your signature: ", payload) io.interactive()
defcrack_x(pubkey): q, g = pubkey['q'], pubkey['g'] M_list = [] for i in range(35): M_list.append([q * int(i == j) for j in range(37)]) m = b"0xDktb" Hm = int.from_bytes(sha256(m).digest(), 'big') As = [] Bs = [] i = 0 while i < 35: r, s, kbits = sign(m) if kbits > 121: continue A = (inverse(s, q) * Hm) % q B = (inverse(s, q) * r) % q As.append(A) Bs.append(B) i += 1 print("[{:0>2d}] k({} bits) found!".format(i, kbits)) Bs = Bs + [1/(2**64), 0] As = As + [0, 2**64] M_list.append(Bs) M_list.append(As) M = Matrix(QQ, M_list) ML = M.LLL() for line in ML: if line[-1] == (2**64): prikey = (line[-2] * (2**64)) % q print("[+] SVP's solution found! prikey = {}".format(prikey)) return prikey
orig_ct = ct upper_side = [] for ud in tqdm(range(0x10000)): guess_upper_dk = format((ud << 4) + upper_dk, '020b') for i in range(1010): _ = (i * 2) % 20 idx = int(guess_upper_dk[_:_+2], 2) k = upper_key[i][idx*8:idx*8+8] des = DES.new(k, DES.MODE_CBC, bytes(8)) ct = des.decrypt(ct) upper_side.append(ct) ct = orig_ct ''' 100%|████████████████████████████████████████████████████████████████████████████| 65536/65536 [19:31<00:00, 55.93it/s] ''' orig_pt = pt lower_side = [] for ld in tqdm(range(0x10000)): guess_lower_dk = format(ld + lower_dk, '020b') for i in range(1010): _ = 18 - ((i * 2) % 20) idx = int(guess_lower_dk[_:_+2], 2) k = lower_key[i][idx*8:idx*8+8] des = DES.new(k, DES.MODE_CBC, bytes(8)) pt = des.encrypt(pt) lower_side.append(pt) pt = orig_pt ''' 100%|████████████████████████████████████████████████████████████████████████████| 65536/65536 [19:33<00:00, 55.85it/s] ''' crash = list(set(upper_side) & set(lower_side))[0] ud = upper_side.index(crash) ld = lower_side.index(crash) SECRET = int(format(ld + lower_dk, '020b') + format((ud << 4) + upper_dk, '020b'), 2) enc_flag = open("flag.enc", "rb").read()
defscramble(s): ret = "".join( [format(s & 0xfffff, '020b')]*101 ) ret += "".join( [format(s >> 20, '020b')]*101 ) return int(ret, 2)
dk = scramble(SECRET) dk = format(dk, '04040b') i = 0 for v in keys[::-1]: idx = int(dk[i:i+2], 2) k = v[idx*8:(idx+1)*8] des = DES.new(k, DES.MODE_CBC, bytes(8)) enc_flag = des.decrypt(enc_flag) i += 2 enc_flag # b'pctf{Two_tO_thE_s1xt33n7h?_E4sy-p3asy..}'