#!/usr/bin/python from Crypto.Cipher import AES import binascii from Crypto.Util.number import bytes_to_long,long_to_bytes from flag import flag from key import key
iv = flag.strip(b'd0g3{').strip(b'}')
LENGTH = len(key) assert LENGTH == 16
hint = os.urandom(4) * 8 print(bytes_to_long(hint)^bytes_to_long(key))
msg = b'Welcome to this competition, I hope you can have fun today!!!!!!'
from Crypto.Cipher import AES from Crypto.Util.strxor import strxor m1=b'xxxxxxxxxxxxxxxx' m2=b'yyyyyyyyyyyyyyyy' aes=AES.new(key,AES.MODE_ECB) enc1=aes.encrypt(strxor(m1,iv)) enc2=aes.encrypt(strxor(m2,enc1)) enc=enc1+enc2 print(enc)
此处msg有64字节,所以有4组,等效于ECB模式的
1 2 3 4 5 6 7 8 9 10 11 12 13
from Crypto.Cipher import AES from Crypto.Util.strxor import strxor msg = b'Welcome to this competition, I hope you can have fun today!!!!!!' m1=msg[:16] m2=msg[16:32] m3=msg[32:48] m4=msg[48:] aes=AES.new(key,AES.MODE_ECB) enc1=aes.encrypt(strxor(m1,iv)) enc2=aes.encrypt(strxor(m2,enc1)) enc3=aes.encrypt(strxor(m3,enc2)) enc4=aes.encrypt(strxor(m4,enc3)) enc=enc1+enc2+enc3+enc4
那我们可以直接从enc4递推回iv
1 2 3 4 5 6 7 8 9 10 11 12 13 14
from Crypto.Cipher import AES from Crypto.Util.strxor import strxor msg = b'Welcome to this competition, I hope you can have fun today!!!!!!' m1=msg[:16] m2=msg[16:32] m3=msg[32:48] m4=msg[48:] enc4=b'3c976c92aff4095a23e885b195077b66' enc4=long_to_bytes(int(enc4,16))#从16进制字符串转化回原字符串 aes=AES.new(key,AES.MODE_ECB) enc3=strxor(aes.decrypt(enc4),m4) enc2=strxor(aes.decrypt(enc3),m3) enc1=strxor(aes.decrypt(enc2),m2) iv=strxor(aes.decrypt(enc1),m1)
exp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
from Crypto.Cipher import AES from Crypto.Util.number import bytes_to_long,long_to_bytes msg = b'Welcome to this competition, I hope you can have fun today!!!!!!' a=56631233292325412205528754798133970783633216936302049893130220461139160682777 enc4=b'3c976c92aff4095a23e885b195077b66' enc4=long_to_bytes(int(enc4,16)) msgs=[bytes_to_long(msg[i:i+16]) for i inrange(0,len(msg),16)] hint='{:<4X}'.format(a)[:4*2]*8 key=long_to_bytes(a^int(hint,16)) aes=AES.new(key,AES.MODE_ECB) enc=[0]*5 enc[4]=enc4 for i inrange(3,-1,-1): enc[i]=long_to_bytes(bytes_to_long(aes.decrypt(enc[i+1]))^msgs[i]) print(enc[0])