def test_decrypt_repeating_xor(self): bytes1 = crypto.h2ba("1c0111001f010100061a024b53535009181c") xor_decrypter = XorDecrypter(bytes1, 1, 40) # we dont use the constructor params for this test output = crypto.h2ba("1d0010011e000001071b034a52525108191d") self.assertEqual(xor_decrypter.decrypt_repeating_xor(), output) original_plain_text = "1111111111111111" original_key = "2222" ciphertext = crypto.xor(crypto.h2ba("1111111111111111"),crypto.h2ba("2122")) xor_decrypter2 = XorDecrypter(ciphertext, 1, 40) # we dont use the constructor params for this test self.assertEqual(xor_decrypter2.decrypt_repeating_xor(), original_plain_text)
def test_guess_key_length(self): original_plain_text = "1111111111111111" original_key = "2222" ciphertext = crypto.xor(crypto.h2ba("1111111111111111"),crypto.h2ba("2122")) xor_decrypter = XorDecrypter(ciphertext, 2, 40) # we dont use the constructor params for this test output = 2 self.assertEqual(xor_decrypter.guess_key_length(), output) ciphertext2 = crypto.xor(crypto.h2ba("1111111111111111"),crypto.h2ba("21222324")) xor_decrypter2 = XorDecrypter(ciphertext, 2, 40) # we dont use the constructor params for this test output = 4 self.assertEqual(xor_decrypter.guess_key_length(), output)
def test_get_blocks(self): bytes = crypto.h2ba("1c0111001f010100061a024b53535009181c") xor_decrypter = XorDecrypter(bytes, 2, 40) blocks = xor_decrypter.get_blocks(12) self.assertEqual(len(blocks), 3) self.assertEqual(len(blocks[0]), 12) self.assertEqual(len(blocks[1]), 12) self.assertEqual(len(blocks[2]), 12) bytes2 = crypto.h2ba("1c0111001f010100061a024b5353500918") xor_decrypter2 = XorDecrypter(bytes2, 2, 40) blocks = xor_decrypter.get_blocks(12) self.assertEqual(len(blocks), 3) self.assertEqual(len(blocks[0]), 12) self.assertEqual(len(blocks[1]), 12) self.assertEqual(len(blocks[2]), 10)
def test_b2base64(self): base16 = crypto.h2ba( "49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d" ) b64 = "SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t" self.assertEqual(crypto.b2base64(base16), b64)
def test_h2ba(self): base16 = "49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d" self.assertEqual(type(crypto.h2ba(base16)), bytearray)
def test_xor_different_length(self): bytes1 = crypto.h2ba("1c0111001f010100061a024b53535009181c") bytes2 = crypto.h2ba("01") output = crypto.h2ba("1d0010011e000001071b034a52525108191d") self.assertEqual(crypto.xor(bytes1, bytes2), output)
def test_xor_samelength(self): bytes1 = crypto.h2ba("1c0111001f010100061a024b53535009181c") bytes2 = crypto.h2ba("686974207468652062756c6c277320657965") output = crypto.h2ba("746865206b696420646f6e277420706c6179") self.assertEqual(crypto.xor(bytes1, bytes2), output)
def test_get_single_byte_xor_histogram_score(self): block1 = crypto.xor(crypto.h2ba("Hello pretty city!"),crypto.h2ba("12")) block2 = crypto.xor(crypto.h2ba("Purple dogs are really neat, and I like them a bunch!"),crypto.h2ba("22")) block3 = crypto.xor(crypto.h2ba("Frogs jump really high sometimes and then fall like marshmallows onto big fluffy pillows."),crypto.h2ba("23"))