ECB / CBC

ECB or CBC

def get_blocks(bytes_, blocksize=16):
    return [bytes_[i:i+blocksize] for i in range(0, len(bytes_), blocksize)]
    
def cbc_or_ecb(rep):
	cipher = str(rep)
	cipher = cipher.replace('Ciphertext is:  ','')
	cipher = cipher.replace('ECB or CBC?','')
	#print cipher
	#Ref: https://github.com/JesseEmond/matasano-cryptopals/blob/master/src/set_2/11.py

	blocks = get_blocks(str.encode(cipher))

	unique_blocks = len(set(blocks))

	guess = "ECB" if unique_blocks != len(blocks) else "CBC"

	#print guess
	return guess

Sources

Last updated