def test_foobar(self): input_bytes = b'foobar foobar foobar foobar' xored_bytes = set01.fixed_xor(input_bytes, bytes([123] * len(input_bytes))) # rest is random hex bytes candidate_inputs = [ bytes.fromhex( '977dd32697ea9c91a11dbcac7c14443d442b21a59139e7447625b0'), bytes.fromhex( 'e78ec6dfb1c088c07ca4b739a2b584eebce4965c998508b304123b'), bytes.fromhex( 'bc8f6af1121680540926442b3a0947daaed972fe7e82c97e186bcf'), bytes.fromhex( '82bec6bddc69afd5171426e6c11b558438b0e3a0eed060f3c6baf2'), xored_bytes, bytes.fromhex( '9c9735c58952e04944307fa70e4b341719a8c4e5ac0aeb17b6ca25'), bytes.fromhex( '6c4829e24230a1d1353f896bda57fab4ba20ff9ff5520d75f4d465'), bytes.fromhex( 'e81dd9e29471f5d1f3f78f8b589dc51ecf33bb5cf27118aec42f97'), bytes.fromhex( '3019db123fa1281f4027720e9b4be526cb5902f2da5a6ce3d1df48'), bytes.fromhex( 'a7e458b2b0d831a0fd83b7ccdc30750586b2418a2a534be93f7fd4'), bytes.fromhex( 'b659b90920cb29f962532ae31edbe2bccd6d470f38b65017852eae'), bytes.fromhex( '58452865ba441420ea043b6886123bd9f2c946fb058609e72b47b9'), ] best_text, best_i = set01.find_single_byte_xor(candidate_inputs) self.assertEqual(best_text, b'foobar foobar foobar foobar') self.assertEqual(best_i, 4)
def test_foobar(self): input_bytes = b'foobar foobar foobar foobar' xored_bytes = set01.fixed_xor(input_bytes, bytes([123] * len(input_bytes))) best_text, xor_byte, _score = set01.break_single_byte_xor(xored_bytes) self.assertEqual(best_text, b'foobar foobar foobar foobar') self.assertEqual(xor_byte, 123)
def test_cryptopals(self): input1 = bytes.fromhex('1c0111001f010100061a024b53535009181c') input2 = bytes.fromhex('686974207468652062756c6c277320657965') expected_output = bytes.fromhex('746865206b696420646f6e277420706c6179') self.assertEqual(set01.fixed_xor(input1, input2), expected_output)
def test_xor_alternating(self): self.assertEqual( set01.fixed_xor(bytes.fromhex('5555'), bytes.fromhex('aaaa')), bytes.fromhex('ffff'))
def test_xor_same(self): self.assertEqual(set01.fixed_xor(b'foobar', b'foobar'), b'\0\0\0\0\0\0')
def test_second_longer(self): self.assertEqual(set01.fixed_xor(b'foobar', b'foo'), b'')
def test_empty(self): self.assertEqual(set01.fixed_xor(b'', b''), b'')