Exemple #1
0
 def test_26(self):
     """As in the CBC bitflip attack, we'll assume we know the exact prefix
     size (2 blocks) and how the forbidden characters are handled."""
     plain = b';admin=True'
     cipher = bytearray(encrypt16(plain, CTR=True))
     cipher[32] = cu.XOR_bytes(b'?', cu.XOR_bytes(bytes([cipher[32]]),
                                                  b';'))[0]
     cipher[38] = cu.XOR_bytes(b'?', cu.XOR_bytes(bytes([cipher[38]]),
                                                  b'='))[0]
     self.assertTrue(decrypt16(cipher, CTR=True))
Exemple #2
0
 def test_16(self):
     """Assume that we know the prefix length is exactly 32 bytes
     (2 blocks). Assume also that we know ';' and '=' are changed to
     '?'. No clue how to do this otherwise..."""
     plain = b';admin=True'
     cipher = bytearray(encrypt16(plain))
     cipher[16] = cu.XOR_bytes(b'?', cu.XOR_bytes(bytes([cipher[16]]),
                                                  b';'))[0]
     cipher[22] = cu.XOR_bytes(b'?', cu.XOR_bytes(bytes([cipher[22]]),
                                                  b'='))[0]
     self.assertTrue(decrypt16(cipher))
Exemple #3
0
    def test_25(self):
        key = cu.random_bytes()
        nonce = cu.random_bytes(count=AES.block_size // 2)
        plain = cu.read_base64('data/Set_1_7.txt')
        ctr = cb.AES_CTR(key, nonce=nonce)
        cipher = ctr.process(plain)

        cipher_len = len(cipher)
        new_plain = bytes([14] * cipher_len)
        new_cipher = ctr.edit(cipher, 0, new_plain)
        keystream = cu.XOR_bytes(new_plain, new_cipher)
        recovered_plain = cu.XOR_bytes(keystream, cipher)

        self.assertEqual(recovered_plain, plain)
Exemple #4
0
 def test_27(self):
     for _ in range(10):
         cipher, key = encrypt27()
         c_0 = cipher[:AES.block_size]
         c_end = cipher[3 * AES.block_size:]
         cipher = c_0 + bytes([0] * AES.block_size) + c_0 + c_end
         try:
             decrypt27(cipher, key)
         except ValueError as e:
             plain = e.args[0]
             break
     p_0 = plain[:AES.block_size]
     p_2 = plain[2 * AES.block_size:3 * AES.block_size]
     d_0 = cu.XOR_bytes(p_2, bytes([0] * AES.block_size))
     recovered_key = cu.XOR_bytes(d_0, p_0)
     self.assertTrue(recovered_key, key)
Exemple #5
0
    def test_19_20(self):
        cipher_list = []
        plain_list = []
        key = cu.random_bytes()
        ctr = cb.AES_CTR(key)
        with open('data/Set_3_19.txt', 'r') as f:
            for line in f:
                plain = cu.base64_to_bytes(line)
                plain_list.append(plain)
                cipher_list.append(ctr.process(plain))
                ctr.reset()
        with open('data/Set_3_20.txt', 'r') as f:
            for line in f:
                plain = cu.base64_to_bytes(line)
                plain_list.append(plain)
                cipher_list.append(ctr.process(plain))
                ctr.reset()

        min_len = len(min(cipher_list, key=len))
        cipher_trunc = [x[:min_len] for x in cipher_list]
        cipher_cat = b''.join(cipher_trunc)
        keystream = cs.get_repeating_XOR_key(cipher_cat, min_len)
        plain_cat = cu.XOR_bytes(keystream, cipher_cat)
        plain_trunc = [
            plain_cat[x:x + min_len] for x in range(0, len(plain_cat), min_len)
        ]
        total, correct = 0, 0
        for real, guess in zip(plain_list, plain_trunc):
            total += 1
            correct += real[:min_len].decode('utf-8').lower() == guess.decode(
                'utf-8').lower()
        self.assertTrue(correct / total > 0.95)
Exemple #6
0
 def test_5(self):
     plain_bytes = b"Burning 'em, if you ain't quick and nimble\n" \
                  +b'I go crazy when I hear a cymbal'
     key_bytes = b'ICE'
     expected_bytes = cu.hex_to_bytes(
         '0b3637272a2b2e63622c2e69692a23693a2a3c6324202d623d63343c2a26226324' \
        +'272765272a282b2f20430a652e2c652a3124333a653e2b2027630c692b202831652' \
        +'86326302e27282f')
     cipher_bytes = cu.XOR_bytes(plain_bytes, key_bytes)
     self.assertEqual(cipher_bytes, expected_bytes)
Exemple #7
0
 def test_2(self):
     bytes_1 = cu.hex_to_bytes('1c0111001f010100061a024b53535009181c')
     bytes_2 = cu.hex_to_bytes('686974207468652062756c6c277320657965')
     bytes_result = cu.hex_to_bytes('746865206b696420646f6e277420706c6179')
     self.assertEqual(cu.XOR_bytes(bytes_1, bytes_2), bytes_result)