from Crypto.Cipher import AES import binascii import hashlib from secret import flag assert flag[:5] == "flag{"and flag[-1:] == "}" key = b"J1fx2g1jDak1c***" l = len(key) message = b"I have had my invitation to this world's festival, and thus my life has been blessed" + binascii.unhexlify(hashlib.sha256(key).hexdigest())[:10] iv = flag[5:-1] message = message + bytes((l - len(message) % l) * chr(l - len(message) % l), encoding = "utf-8") aes = AES.new(key, AES.MODE_CBC, iv) print(binascii.hexlify(aes.encrypt(message))) #******************************************************************************************************************************************************6ece036e495d363b647d7f2749c4c2f3dd78f8637b
from Crypto.Cipher import AES from tqdm import tqdm import binascii import hashlib # 1. 生成key字典 import string dic = string.printable[:62] # print(dic) # 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ withopen('key_table.txt','wb') as file: for i in dic: for j in dic: for k in dic: key = b"J1fx2g1jDak1c"+i.encode()+j.encode()+k.encode() file.write(key+b'\n') file.close() # 2.爆破key defxor(m: bytes, c: bytes): returnbytes([i ^ j for i, j inzip(m, c)]) enc = binascii.unhexlify('5d363b647d7f2749c4c2f3dd78f8637b') five_part = binascii.unhexlify(b"6ece036e49") f = open("key_table.txt","rb+") pbar = tqdm(range(238328)) for i in f: key = i[:16] aes = AES.new(key, AES.MODE_ECB) dec = aes.decrypt(enc) # 最后一块 16个字节 4+10+2 m = b"ssed" +binascii.unhexlify(hashlib.sha256(key).hexdigest())[:10]+b'\x02\x02' xor_result = xor(m,dec) pbar.update(1) if five_part in xor_result: print("[+] key:",key) break f.close() # 3.求IV l = len(key) # 192位 message = b"I have had my invitation to this world's festival, and thus my life has been blessed" + binascii.unhexlify(hashlib.sha256(key).hexdigest())[:10] message = message + bytes((l - len(message) % l) * chr(l - len(message) % l), encoding="utf-8") for i inrange(0,len(message),16): aes_ecb = AES.new(key,AES.MODE_ECB) dec_c = aes_ecb.decrypt(enc) enc = xor(message[len(message)-i-16:len(message)-i],dec_c) print(b'flag{'+enc+b'}')
withopen("flag_cipher", "rb") as fp: # print(len(fp.read())) cipher = fp.read() fp.close()
key0 = cipher[:32] print(key0)
defMyOwnCBC(key0,cipher): cipher = [cipher[i:i + 32] for i inrange(0, len(cipher), 32)] m = b'' tmpkey = key0 # 上一次的密文作为key参与下一次ECB for i inrange(1,len(cipher)): aes = AES.new(tmpkey,AES.MODE_ECB) m += aes.decrypt(cipher[i]) tmpkey = cipher[i] return m
print(MyOwnCBC(key0,cipher)) #b'\xe5\xdf\x94sJ\xc2\xcd\x04\xeb\xb7\xcf\x05(\xbe\x98\\\xe9\xc3^\x1f!\xfb\xea6\xdac\x1f\xfe\x901\xbb\x13' #b"mode of operation is an algorithm that uses a block cipher to provide an information service such as confidentiality or authenticity. A block cipher by itself is only suitable for the secure cryptographic transformation (encryption or decryption) of one fixed-length group of bits called a block. A mode of operation describes how to repeatedly apply a cipher's single-block operation to securely transform amounts of data larger than a block.\n\nMost modes require a unique binary sequence, often called an initialization vector (IV), for each encryption operation. The IV has to be non-repeating and, for some modes, random as well. The initialization vector is used to ensure distinct ciphertexts are produced even when the same plaintext is encrypted multiple times independently with the same key. Block ciphers have one or more block size(s), but during transformation the block size is always fixed. Block cipher modes operate on whole blocks and require that the last part of the data be padded to a full block if it is smaller than the current block size. There are, however, modes that do not require padding because they effectively use a block cipher as a stream cipher.\n\nHistorically, encryption modes have been studied extensively in regard to their error propagation properties under various scenarios of data modification. Later development regarded integrity protection as an entirely separate cryptographic goal. Some modern modes of operation combine confidentiality and authenticity in an efficient way, and are known as authenticated encryption modes.\n\nAh you found it~ afctf{Don't_be_fooled_by_yourself}~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"