def test_xor_single_byte_decode(self): source_str = binascii.hexlify(b'A').decode('ascii') key = binascii.hexlify(b'B').decode('ascii') crypt = crypt_utils.xor(source_str, key) self.assertEqual(decryptor.single_byte_xor_decrypt(crypt, key), source_str) source_str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' key = binascii.hexlify(b'h').decode('ascii') crypt = ''.join(crypt_utils.xor(binascii.hexlify(bytes(c, 'ascii')).decode('ascii'), key) for c in source_str) self.assertEqual(decryptor.single_byte_xor_decrypt(crypt, key), binascii.hexlify(bytes(source_str,'ascii')).decode('ascii').upper())
def encrypt_repeating_key_xor(data, key): """Implementation of repeating key xor. (ex 1.5).""" message = "" keygen = repeating_key_loop(key) for b in crypt_utils.get_next_hex(data): keybit = next(keygen) enc = crypt_utils.xor(b, keybit) if(len(enc) < 2): # ensure leading 0's are added to the byte if required enc = '0' + enc message += enc return message
def single_byte_xor_decrypt(message, key): '''Key should be a single character, represented in Hex.''' if not crypt_utils.is_hex(message) or not crypt_utils.is_hex(key): raise TypeError("Error: message and key expected in Hex format") decode = '' for i in range(len(message), 0, -2): b = message[i-2:i] tmp = crypt_utils.xor(b, key) if len(tmp) < 2: tmp = '0' + tmp decode = tmp + decode return decode
def decrypt_repeating_key_xor(cyphertext, key): """Decrypts the cyphertext using the given key, using repeating xor cypher Assumes the cyphertext is already hex""" text = "" keycode = encode.repeating_key_loop(key) loc = 0 while loc < len(cyphertext): b = cyphertext[loc:loc+2] keybyte = next(keycode) htext = crypt_utils.xor(b, keybyte) if len(htext) < 2: # ensure leading '0' if only a single digit returned htext = "0" + htext text += htext loc += 2 return binascii.unhexlify(text)
def test_xor(self): self.assertEqual(crypt_utils.xor('F0','0F'),'FF') self.assertEqual(crypt_utils.xor('1c0111001f010100061a024b53535009181c', '686974207468652062756c6c277320657965').lower(), '746865206b696420646f6e277420706c6179')