from Crypto.Cipher import AES from Crypto.Util.Padding import pad, unpad from os import urandom from flag import flag
defcbc_encrypt(msg: bytes): msg = pad(msg, 16) msg = [msg[i:i+16] for i inrange(0, len(msg), 16)] key = urandom(16) out = [] for block in msg: cipher = AES.new(key, AES.MODE_ECB) next = cipher.encrypt(block) out.append(next) key = next out = b"".join(out) return key, out
from Crypto.Cipher import AES ct = b'\x179\xb8l\x97\xbew\xc2\xd5f~\x8e\xdc\xf2\x9b\xabR\xa9a\xd2\xf4\xde\xd6|\xd1\x9f\xe9q\x1d\xfcm\xfbj\xe9\x9e\xab\xf5fL\xb3\xb5_\xa5\x16\x8e\x7f\x9fV`\x8b\x16\xa1\xa6)\x08\x97\x91\xbd3\x1d\xeb\\\x86\xa2\xd6\x94>\xf3\xfdt\xd9\x14\xf3\xfc\xe2\x02\xd6\xc4\xcfq"\x1a\x14~2]4\x9f\xc9\x88\xf8\x12\xb6\xa2\xd7\xec\x0b\x7f\xd4d\xdc\xc6\xb4]\x10u\xc6f\x97m\xccA\x82\x02\xa5gh\x85\x85Wz\xd9.\xff\x9bx\x99J\x0e\x86\x16\x90\xad\x1e\x17\x86\x95\xb8S\x17\xea\x93v\xd0'
#每16个为一组 msg=[ct[i:i+16] for i inrange(0,len(ct),16)] for i inrange(len(msg)-1,-1,-1): enc=msg[i] key=msg[i-1] cipher=AES.new(key,AES.MODE_ECB) dec=cipher.decrypt(enc) print(dec)
#!/usr/bin/python3 from random import sample, choice from os import urandom from secret import flag from binascii import unhexlify from hashlib import sha256 from string import hexdigits from signal import alarm
defproof(): plain = "".join([choice(hexdigits) for i inrange(20)]) print(plain) s = sha256(plain.encode()).hexdigest() print(f"sha256({plain[:16]}xxxx) = {s}") xxxx = input("plz enter the xxxx: ") if xxxx != plain[16:]: exit()
defrev(x): for i inrange(32): x = rotl(x, 1) return x
classEnc: def__init__(self, key): self.K = key self.S = sample([i for i inrange(256)], 256)
defl(self, B: list) -> list: B = List2Int(B) B = B ^ rotl(B, 2) ^ rotl(B, 10) ^ rotl(B, 18) ^ rotl(B, 24) return Int2List(B)
defencrypt(self, plain): T = xorl(self.K[:4], plain) T = Int2List(rev(List2Int(T))) T = self.l([self.S[i] for i in T]) T = xorl(self.K[4:], T) returnbytes(T).hex()
defE(): plain = input("plz enter your plaintext: ") print(f"cipher = {C.encrypt(unhexlify(plain))}")
defGet_Flag(): print(f"cipher = {f}") plain = input("plz enter your plaintext: ") c = C.encrypt(unhexlify(plain)) if c == f: print(flag) exit(0) else: print("wrong!") exit(0)
if __name__ == "__main__": proof() print(logo) key = urandom(8) C = Enc(key) f = C.encrypt(urandom(4)) print(f"sbox: {C.S}") print(_memu) alarm(10) whileTrue: memu()
看到这题定义这么多函数,先看一下加密流程,再把定义的函数逐个拆开来看。 先通过proof:
1 2 3 4 5 6 7 8
defproof(): plain = "".join([choice(hexdigits) for i inrange(20)]) print(plain) s = sha256(plain.encode()).hexdigest() print(f"sha256({plain[:16]}xxxx) = {s}") xxxx = input("plz enter the xxxx: ") if xxxx != plain[16:]: exit()
defencrypt(self, plain): T = xorl(self.K[:4], plain) T = Int2List(rev(List2Int(T))) T = self.l([self.S[i] for i in T]) T = xorl(self.K[4:], T) returnbytes(T).hex()
from random import sample, choice from os import urandom from binascii import * from hashlib import sha256 from string import hexdigits from Crypto.Util.number import * from z3 import *
defrev(x): for i inrange(32): x = rotl(x, 1) return x
defl(B): B = List2Int(B) B = B ^ rotl(B, 2) ^ rotl(B, 10) ^ rotl(B, 18) ^ rotl(B, 24) return Int2List(B)
defdecrypt(K,cipher): T = List2Int(xorl(K[4:],cipher)) s = Solver() B = BitVec('B',32) s.add(B ^ rotl(B, 2) ^ rotl(B, 10) ^ rotl(B, 18) ^ rotl(B, 24) == T) if s.check() == sat: #检测是否有解 result = str(s.model()) T = Int2List(int(result[5:-1])) for i inrange(len(T)): T[i] = inv_S[T[i]] T = List2Int(xorl(K[:4],T)) return long_to_bytes(T).hex()
classEnc: def__init__(self, key): self.K = key self.S = sample([i for i inrange(256)], 256)
defl(self, B: list) -> list: B = List2Int(B) B = B ^ rotl(B, 2) ^ rotl(B, 10) ^ rotl(B, 18) ^ rotl(B, 24) return Int2List(B)
defencrypt(self, plain): T = xorl(self.K[:4], plain) T = Int2List(rev(List2Int(T))) temp = [self.S[i] for i in T] T = self.l(temp) T = xorl(self.K[4:], T) returnbytes(T)
defl(B): B = List2Int(B) B = B ^ rotl(B, 2) ^ rotl(B, 10) ^ rotl(B, 18) ^ rotl(B, 24) return Int2List(B)
defdecrypt(K,cipher,inv_S): T = List2Int(xorl(K[4:],cipher)) s = Solver() B = BitVec('B',32) s.add(B ^ rotl(B, 2) ^ rotl(B, 10) ^ rotl(B, 18) ^ rotl(B, 24) == T) if s.check() == sat: #检测是否有解 result = str(s.model()) T = Int2List(int(result[5:-1])) for i inrange(len(T)): T[i] = inv_S[T[i]] T = List2Int(xorl(K[:4],T)) return long_to_bytes(T).hex()
defgetflag(): while(1): try: r = remote("node4.anna.nssctf.cn",28579) temp = r.recvline().strip().decode()[-4:] r.sendline(temp.encode()) r.recvuntil(b"sbox: ") S = eval(r.recvline()) inv_S = [0for i inrange(256)] for i inrange(256): inv_S[S[i]] = i