> For the complete documentation index, see [llms.txt](https://0xss0rz.gitbook.io/0xss0rz/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://0xss0rz.gitbook.io/0xss0rz/ctf/cryptography-1/ecb-cbc.md).

# 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

{% embed url="<https://0xss0rz.github.io/2020-09-14-CSAW-CTF-Quals-2020/>" %}
