Meet in the middle


from Crypto.Cipher import AES
from itertools import product
plaintext="MessaggioChiaro0"
pchiave1="ChiaveSegretaX"
k1=pchiave1+"23"
k2=pchiave1+"bc"

enc1=AES.new(k1,AES.MODE_ECB)
enc2=AES.new(k2,AES.MODE_ECB)

cifra=enc2.encrypt(enc1.encrypt(plaintext))


## MEET IN THE MIDDLE

# INIZIALIZZA DIZIONARI
encoded={}
decoded={}

# POSSIBILI PASSWORDS
for x in product(range(128),repeat=2):
    psx=pchiave1
    suff="".join([ chr(t) for t in x])
    psx=psx+suff
    enc=AES.new(psx,AES.MODE_ECB)
    encoded[enc.encrypt(plaintext)]=psx
    decoded[enc.decrypt(cifra)]=psx

# Cerca collisioni
meet=[ (encoded[key],decoded[key])
      for key in encoded.keys() if key in decoded.keys() ]

print(meet)