예제 #1
0
    def test_cbc_custom_decrypt(self):
        k = b'\x81\x0ff\t\x04\xb6\xcf\x1f.\x10\x8frd\xb4E\x19'
        cipher = b'e|\x92\xd0\x8b\xd9\x00\xc8X\xf2Noi\xa1\x155\x8b\xa5\xb7\xdcka\xaa\x94=a_!x\x1a\xcf\xf4'
        n = 1
        assert len(cipher) == 32
        iv = b'e|\x92\xd0\x8b\xd9\x00\xc8X\xf2Noi\xa1\x155'
        cipher_text = AES.new(k, AES.MODE_CBC, iv)
        assert cipher_text.decrypt(cipher[16:]) == cbc_custom_decrypt(
            k, n, cipher)

        k = b'\x81\x0ff\t\x04\xb6\xcf\x1f.\x10\x8frd\xb4E\x19'
        iv = b'9193470938756473'
        cipher = iv + b'93746483900993873765345729285341'
        n = 2
        assert len(cipher) == 48
        cipher_text = AES.new(k, AES.MODE_CBC, iv)
        assert cipher_text.decrypt(cipher[16:]) == cbc_custom_decrypt(
            k, n, cipher)

        k = b'3939378487348557'
        iv = b'8870123765252522'
        cipher = iv + b'937464839009938737653457292853411010109394847645'
        n = 3
        assert len(cipher) == 64
        cipher_text = AES.new(k, AES.MODE_CBC, iv)
        assert cipher_text.decrypt(cipher[16:]) == cbc_custom_decrypt(
            k, n, cipher)
예제 #2
0
def test_cbc_custom_decrypt(num_tests=1000):
    from Crypto.Random import get_random_bytes

    # we'll use AES CBC
    key = get_random_bytes(block_size)
    aes = AES.new(key, AES.MODE_CBC, iv=get_random_bytes(block_size))

    total_cipher = aes.iv
    total_message = bytes([])

    for i in range(num_tests):
        # create a random message
        message = get_random_bytes(block_size)

        # concatenate the new ciphertext and mdessage to the whole strings
        total_cipher += aes.encrypt(message)
        total_message += message

        decryption = cbc_custom_decrypt(key, i + 1, total_cipher)

        if decryption != total_message:
            # raise Exception("Failed a test")
            print("Failed a test")
            break
    else:
        print(f"Passed all cbc_custom_decrypt tests ({num_tests} random tests)")
예제 #3
0
def output_example_test():
    # key = b'\x81\x0ff\t\x04\xb6\xcf\x1f.\x10\x8frd\xb4E\x19'
    # key_hex = '810f660904b6cf1f2e108f7264b44519'
    # iv = b'e|\x92\xd0\x8b\xd9\x00\xc8X\xf2Noi\xa1\x155'
    # cipher = b'e|\x92\xd0\x8b\xd9\x00\xc8X\xf2Noi\xa1\x155\xb5b\xe7r\xb1\xec\xb5\xed\xca\xca\x1f$\xf8\xe33%'
    # output = b'\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01'

    key = b'\x81\x0ff\t\x04\xb6\xcf\x1f.\x10\x8frd\xb4E\x19'
    cipher = b'e|\x92\xd0\x8b\xd9\x00\xc8X\xf2Noi\xa1\x155\xb5b\xe7r\xb1\xec\xb5\xed\xca\xca\x1f$\xf8\xe33%'
    message = b'1111111111111111'

    dec = cbc_custom_decrypt(key, 1, cipher)
    if message == dec:
        print("Passed example test")
    else:
        print("Failed example test")
예제 #4
0
def testing_decrypt(testsing_dict):
    errors = 1
    test_num = 1
    for test in testsing_dict:
        key, iv = test["key"], test["iv"]
        for plain_text, c in zip(test["plain"], test["cipher"]):
            num_of_blocks = len(c) // 16
            my_output = cbc_custom_decrypt(key, num_of_blocks, iv + c)
            if my_output != plain_text:
                print("Error #{} in Test #{}".format(errors, test_num))
                print("in: {}\n\tplain text should be: {}\n\tcipher is: {}\n".format(errors, my_output, plain_text, c))
                errors += 1
            else:
                print("Passed Test #{}".format(test_num))
            test_num += 1
    if errors == 1:
        print("Passed The Test: testing_decrypt")
    else:
        print("Didn't Pass The Test")