Beispiel #1
0
def detect_potential_repeating_ecb_blocks(ciphertext, blocksize=16):
    seen = set()
    for block in brake_into_keysize_blocks(ciphertext, blocksize):
        if block in seen:
            return True
        else:
            seen.add(block)
    return False
Beispiel #2
0
def cbc_encrypt(key, iv, plaintext):
    blocks = brake_into_keysize_blocks(pad_pkcs_7(plaintext, 16), 16)
    cipher = AES.new(key, AES.MODE_ECB)

    def cryptoblocks():
        last_block = iv
        for block in blocks:
            chained = xor_buffers(last_block, block)
            last_block = cipher.encrypt(chained)
            yield last_block

    return b''.join([cb for cb in cryptoblocks()])
Beispiel #3
0
def cbc_decrypt(key, iv, ciphertext):
    assert len(ciphertext) % 16 == 0
    blocks = brake_into_keysize_blocks(ciphertext, 16)
    cipher = AES.new(key, AES.MODE_ECB)

    def plainblocks():
        last_block = iv
        for block in blocks:
            decrypted_block = cipher.decrypt(block)
            plain_block = xor_buffers(last_block, decrypted_block)
            last_block = block
            yield plain_block

    return strip_pkcs_7(b''.join(pb for pb in plainblocks()))
def _guess_xor_key_for_given_size(ciphertext, size):
    sections = _transpose_blocks(brake_into_keysize_blocks(ciphertext, size))
    decryptions = [break_single_byte_xor(section) for section in sections]
    key_bytes = (decryption[0] for decryption in decryptions)
    return b"".join(key_bytes)
Beispiel #5
0
 def test_rest(self):
     assert bf.brake_into_keysize_blocks(b"blablobl", 3) == \
            [b"bla", b"blo", b"bl"]
Beispiel #6
0
 def test_example(self):
     assert bf.brake_into_keysize_blocks(b"blablobliblu", 3) == \
            [b"bla", b"blo", b"bli", b"blu"]