def detect_aes_mode(encrypt_fn): """ Detect if an encryption function is encrypting using ECB mode or CBC mode :param encrypt_fn: Encryption function to submit plaintexts to :return: AesMode.ECB or AesMode.CBC """ # Encrypt a single byte across multiple blocks. If the function is using ECB there will be repeating # cipertext blocks. plaintext = 'A' * 1024 ciphertext = encrypt_fn(plaintext) if set1.detect_aes_ecb([ciphertext]): return AesMode.ECB else: return AesMode.CBC
def test_detect_aes_ecb_mode(self): with open('8.txt') as f: lines = f.readlines() deciphered = set1.detect_aes_ecb(lines) self.assertEqual(deciphered, [133])
def test_detect_aes_ecb(): """ Set 1 Challenge 8 """ assert set1.detect_aes_ecb(open(SCRIPT_DIR + 'test_data/1_8.txt', 'r').read().splitlines()) == [132]