def hexbinstrxor(_hex, _bin): # convert both the hex and bin string into ASCII-characters string str1 = bits_to_string( pad_to_block(convert_to_bits(int(_hex, 16)), len(_hex) * 4)) str2 = bits_to_string([b for b in _bin]) size = min(len(str1), len(str2)) return ''.join( [chr(ord(c1) ^ ord(c2)) for c1, c2 in zip(str1[:size], str2[:size])])
def hex2str(_hex): """ the following function will be used to convert a hex message into a string of characters """ if not is_hex(_hex): raise Exception('Key to AES_CTR must be hex-digits') hex_int = int(_hex, 16) hex_bin = convert_to_bits(hex_int) return bits_to_string(pad_to_block(hex_bin, len(_hex) * 4))
def hex_blks2str(_blks_list): """ the following function will be used to convert hex-digits blocks of the plaintext into a single string of ASCII-characters """ list_str = ''.join((' '.join(_blks_list)).split()) num_hex_digits = len(list_str) hex_int = int(list_str, 16) hex_bin = convert_to_bits(hex_int) return bits_to_string(pad_to_block(hex_bin, num_hex_digits * 4))
def msg_hex2str(self): """ the following function will be used to convert a hex message into a string of characters """ if not self.is_empty(): hex_int = int(self.data, 16) hex_bin = convert_to_bits(hex_int) return bits_to_string(pad_to_block(hex_bin, ASCII_BITS)) else: raise Exception('Message is EMPTY')
def counter_block(): return bits_to_string(bin_nonce + bin_iv + string_to_bits(ctr()))
def message_value(message): message = bits_to_string(pad_to_block(convert_to_bits(message), 8)) return bill_value(message)
def int_to_string(n): return bits_to_string(pad_bits(convert_to_bits(n), 8))