Exemplo n.º 1
0
    def test_single_xor_empty_s_returns_unchanged_bytes(self):
        b = b'Hey'
        expected_bytes = b'Hey'

        actual_bytes = single_xor(b, b'')

        self.assertEqual(expected_bytes, actual_bytes)
Exemplo n.º 2
0
    def test_single_xor_s_more_than_one_byte_only_first_byte_is_used(self):
        b = b'Hey'
        s = b'Aloha'
        expected_bytes = b'\x09\x24\x38'

        actual_bytes = single_xor(b, s)

        self.assertEqual(expected_bytes, actual_bytes)
Exemplo n.º 3
0
    def test_single_xor_nominal_case(self):
        b = b'Hey'
        s = b'A'
        expected_bytes = b'\x09\x24\x38'

        actual_bytes = single_xor(b, s)

        self.assertEqual(expected_bytes, actual_bytes)
Exemplo n.º 4
0
def break_single_xor(encrypted: bytes) -> list:
    """
    params:
        encrypted: bytes encrypted using single-xor
    returns:
        list of dict {'message': bytes, 'key': bytes: 'score': float}
        sorted in descending order by score
    """
    res = list()
    for i in range(0, 256):
        b = i.to_bytes(1, byteorder=sys.byteorder)
        xored = single_xor(encrypted, b)
        score = freq_score(xored)
        res.append({'message': xored, 'key': b, 'score': score})
    res.sort(key=lambda d: d['score'], reverse=True)
    return res
Exemplo n.º 5
0
 def test_single_xor_none_input_raises_typeerror(self):
     with self.assertRaises(TypeError):
         single_xor(None, b'A')
Exemplo n.º 6
0
 def test_single_xor_empty_input_returns_empty_bytes(self):
     self.assertEqual(single_xor(b'', b'A'), b'')