Esempio n. 1
0
def guess_xor_key(ciphertext, use_naiive=True, from_hex=True):
    ''' Try all bytes and return the results scored by English-ness.

    Returns (key, plaintext).

    :param ciphertext: Encrypted hex string to decode.
    '''

    best = 0
    best_string = ""
    xor_key = 0

    if from_hex:
        ciphertext = bytearray.fromhex(ciphertext)

    for xor in xrange(256):
        keytext = bytearray(chr(xor)) * len(ciphertext)
        plainbytes = cryptopals.fixed_xor(ciphertext, keytext)

        if use_naiive:
            score = cryptopals.scoring.score_naiive(plainbytes)
        else:
            score = cryptopals.scoring.score_freq(plainbytes)

        if score > best:
            best = score
            best_string = str(plainbytes)
            xor_key = xor

    return (xor_key, best_string)
Esempio n. 2
0
def guess_xor_key(ciphertext, use_naiive=True, from_hex=True):
    ''' Try all bytes and return the results scored by English-ness.

    Returns (key, plaintext).

    :param ciphertext: Encrypted hex string to decode.
    '''

    best = 0
    best_string = ""
    xor_key = 0

    if from_hex:
        ciphertext = bytearray.fromhex(ciphertext)

    for xor in xrange(256):
        keytext = bytearray(chr(xor)) * len(ciphertext)
        plainbytes = cryptopals.fixed_xor(ciphertext, keytext)

        if use_naiive:
            score = cryptopals.scoring.score_naiive(plainbytes)
        else:
            score = cryptopals.scoring.score_freq(plainbytes)

        if score > best:
            best = score
            best_string = str(plainbytes)
            xor_key = xor

    return (xor_key, best_string)
Esempio n. 3
0
    def test_fixed_xor(self):
        ''' Test the fixed_xor() function from Set 1, Challenge 2. '''
        input1 = bytearray.fromhex('1c0111001f010100061a024b53535009181c')
        input2 = bytearray.fromhex('686974207468652062756c6c277320657965')
        expected = bytearray.fromhex('746865206b696420646f6e277420706c6179')

        output = cryptopals.fixed_xor(input1, input2)
        self.assertEqual(output, expected, 'XOR produced invalid output')
Esempio n. 4
0
    def test_fixed_xor(self):
        ''' Test the fixed_xor() function from Set 1, Challenge 2. '''
        input1 = bytearray.fromhex('1c0111001f010100061a024b53535009181c')
        input2 = bytearray.fromhex('686974207468652062756c6c277320657965')
        expected = bytearray.fromhex('746865206b696420646f6e277420706c6179')

        output = cryptopals.fixed_xor(input1, input2)
        self.assertEqual(output, expected, 'XOR produced invalid output')
Esempio n. 5
0
    def test_old_fixed_xor(self):
        ''' See if the new bytearray implementation is faster than the old string implementation. '''
        iterations = 100000
        input1_str = '1c0111001f010100061a024b53535009181c'
        input2_str = '686974207468652062756c6c277320657965'
        input1_ba = bytearray.fromhex(input1_str)
        input2_ba = bytearray.fromhex(input2_str)

        def old_fixed_xor(buff1, buff2):
            ''' XOR two equal-length buffers by byte.

            :param buff1: First buffer of hex digits.
            :param buff2: Second buffer of hex digits.
            '''
            if len(buff1) != len(buff2):
                raise ValueError(
                    'Buffers must be equal size ({0} vs {1}.'.format(
                        len(buff1), len(buff2)))

            bytes1 = buff1.decode('hex')
            bytes2 = buff2.decode('hex')

            buff3 = ''.join([
                chr(ord(bytes1[x]) ^ ord(bytes2[x])).encode('hex')
                for x in xrange(len(bytes1))
            ])

            return buff3

        idx = 0
        old_start = time.time()
        while idx < iterations:
            _ = old_fixed_xor(input1_str, input2_str)
            idx += 1
        old_delta = time.time() - old_start

        idx = 0
        new_start = time.time()
        while idx < iterations:
            _ = cryptopals.fixed_xor(input1_ba, input2_ba)
            idx += 1
        new_delta = time.time() - new_start

        print('bytearray version: {0}s, string version: {1}s'.format(
            new_delta, old_delta))
        self.assertTrue(new_delta < old_delta,
                        "new {0}s, old {1}s".format(new_delta, old_delta))
Esempio n. 6
0
    def test_old_fixed_xor(self):
        ''' See if the new bytearray implementation is faster than the old string implementation. '''
        iterations = 100000
        input1_str = '1c0111001f010100061a024b53535009181c'
        input2_str = '686974207468652062756c6c277320657965'
        input1_ba = bytearray.fromhex(input1_str)
        input2_ba = bytearray.fromhex(input2_str)

        def old_fixed_xor(buff1, buff2):
            ''' XOR two equal-length buffers by byte.

            :param buff1: First buffer of hex digits.
            :param buff2: Second buffer of hex digits.
            '''
            if len(buff1) != len(buff2):
                raise ValueError('Buffers must be equal size ({0} vs {1}.'.format(len(buff1), len(buff2)))

            bytes1 = buff1.decode('hex')
            bytes2 = buff2.decode('hex')

            buff3 = ''.join([chr(ord(bytes1[x]) ^ ord(bytes2[x])).encode('hex') for x in xrange(len(bytes1))])

            return buff3

        idx = 0
        old_start = time.time()
        while idx < iterations:
            _ = old_fixed_xor(input1_str, input2_str)
            idx += 1
        old_delta = time.time() - old_start

        idx = 0
        new_start = time.time()
        while idx < iterations:
            _ = cryptopals.fixed_xor(input1_ba, input2_ba)
            idx += 1
        new_delta = time.time() - new_start

        print('bytearray version: {0}s, string version: {1}s'.format(new_delta, old_delta))
        self.assertTrue(new_delta < old_delta, "new {0}s, old {1}s".format(new_delta, old_delta))
Esempio n. 7
0
def c2():
	str1 = "1c0111001f010100061a024b53535009181c"
	str2 = "686974207468652062756c6c277320657965"
	expected_result = "746865206b696420646f6e277420706c6179"
	result = cryptopals.fixed_xor(str1, str2)
	expect(expected_result, result)
    def test_fixed_xor(self):
        expected = bytes.fromhex('746865206b696420646f6e277420706c6179')
        actual = fixed_xor(bytes.fromhex('1c0111001f010100061a024b53535009181c'),
                           bytes.fromhex('686974207468652062756c6c277320657965'))

        self.assertEqual(expected, actual)
Esempio n. 9
0
def test_fixed_xor():
    buffer1 = '1c0111001f010100061a024b53535009181c'
    buffer2 = '686974207468652062756c6c277320657965'
    result = '746865206b696420646f6e277420706c6179'
    assert fixed_xor(buffer1, buffer2) == result
Esempio n. 10
0
"""
Write a function that takes two equal-length buffers and produces
their XOR combination.

If your function works properly, then when you feed it the string
1c0111001f010100061a024b53535009181c after hex decoding, and when
XOR'd against 686974207468652062756c6c277320657965 should produce
746865206b696420646f6e277420706c6179.
"""

from cryptopals import fixed_xor

if __name__ == '__main__':
    import argparse

    parser = argparse.ArgumentParser(
            description='takes two equal-length buffers '
                        'and produces their XOR combination')
    parser.add_argument('buffer1')
    parser.add_argument('buffer2')
    args = parser.parse_args()

    print(fixed_xor(args.buffer1, args.buffer2))