Exemple #1
0
def str2hex(_str):
    """
	the following function will be used to convert
	a strin chr message into hex-digits string
	"""
    str_bin = pad_to_block(string_to_bits(_str), ASCII_BITS)
    return hex(int(display_bits(str_bin), 2))[2:-1].zfill(len(str_bin) / 4)
def str2hex(_str):
	"""
	the following function will be used to convert
	a strin chr message into hex-digits string
	"""
	str_bin = pad_to_block(string_to_bits(_str), ASCII_BITS)
	return hex(int(display_bits(str_bin), 2))[2:-1].zfill(len(str_bin) / 4)
def test_counter_block_function():	
	hex_nonce = ''.join('00 6C B6 DB'.split())
	print hex_nonce
	hex_iv = ''.join('C0 54 3B 59 DA 48 D9 0B'.split())
	print hex_iv
	bin_nonce = pad_to_block(convert_to_bits(int(hex_nonce, 16)), len(hex_nonce)*4)
	print bin_nonce
	bin_iv = pad_to_block(convert_to_bits(int(hex_iv, 16)), len(hex_iv)*4)
	ctr = Counter.new(32)
	def counter_block():
		return bits_to_string(bin_nonce + bin_iv + string_to_bits(ctr()))

	final_str = hex(int(display_bits(string_to_bits(counter_block())), 2))[2:-1].zfill(32)
	print final_str
	print final_str == ''.join('00 6C B6 DB C0 54 3B 59 DA 48 D9 0B 00 00 00 01'.lower().split())
	final_str = hex(int(display_bits(string_to_bits(counter_block())), 2))[2:-1].zfill(32)
	print final_str
	print final_str == ''.join('00 6C B6 DB C0 54 3B 59 DA 48 D9 0B 00 00 00 02'.lower().split())
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))
Exemple #6
0
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))
Exemple #7
0
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 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 decode(self, ciphertext, hex_iv):
		"""
		ciphertext: a string representing the ciphertext which will be decrypted
		hex_iv: a hex-digit string representing the initialization vector
		return hex-digit string representing the plaintext
		"""
		bin_iv = pad_to_block(convert_to_bits(int(hex_iv, 16)), len(hex_iv)*4)
		ctr = Counter.new(32)	# counter is 32-bits (4-bytes) long
		def counter_block():
			return bits_to_string(self.bin_nonce + bin_iv + string_to_bits(ctr()))
		decoder = AES.new(self.key, AES.MODE_CTR, counter=counter_block)
		plaintext = decoder.decrypt(ciphertext)
		return str2hex(plaintext)
Exemple #11
0
def test_counter_block_function():
    hex_nonce = ''.join('00 6C B6 DB'.split())
    print hex_nonce
    hex_iv = ''.join('C0 54 3B 59 DA 48 D9 0B'.split())
    print hex_iv
    bin_nonce = pad_to_block(convert_to_bits(int(hex_nonce, 16)),
                             len(hex_nonce) * 4)
    print bin_nonce
    bin_iv = pad_to_block(convert_to_bits(int(hex_iv, 16)), len(hex_iv) * 4)
    ctr = Counter.new(32)

    def counter_block():
        return bits_to_string(bin_nonce + bin_iv + string_to_bits(ctr()))

    final_str = hex(int(display_bits(string_to_bits(counter_block())),
                        2))[2:-1].zfill(32)
    print final_str
    print final_str == ''.join(
        '00 6C B6 DB C0 54 3B 59 DA 48 D9 0B 00 00 00 01'.lower().split())
    final_str = hex(int(display_bits(string_to_bits(counter_block())),
                        2))[2:-1].zfill(32)
    print final_str
    print final_str == ''.join(
        '00 6C B6 DB C0 54 3B 59 DA 48 D9 0B 00 00 00 02'.lower().split())
Exemple #12
0
    def decode(self, ciphertext, hex_iv):
        """
		ciphertext: a string representing the ciphertext which will be decrypted
		hex_iv: a hex-digit string representing the initialization vector
		return hex-digit string representing the plaintext
		"""
        bin_iv = pad_to_block(convert_to_bits(int(hex_iv, 16)),
                              len(hex_iv) * 4)
        ctr = Counter.new(32)  # counter is 32-bits (4-bytes) long

        def counter_block():
            return bits_to_string(self.bin_nonce + bin_iv +
                                  string_to_bits(ctr()))

        decoder = AES.new(self.key, AES.MODE_CTR, counter=counter_block)
        plaintext = decoder.decrypt(ciphertext)
        return str2hex(plaintext)
	def __init__(self, hex_key, hex_nonce):
		self.key = hex2str(hex_key)
		self.bin_nonce = pad_to_block(convert_to_bits(int(hex_nonce, 16)), len(hex_nonce)*4)
Exemple #14
0
 def __init__(self, hex_key, hex_nonce):
     self.key = hex2str(hex_key)
     self.bin_nonce = pad_to_block(convert_to_bits(int(hex_nonce, 16)),
                                   len(hex_nonce) * 4)
def message_value(message):
    message = bits_to_string(pad_to_block(convert_to_bits(message), 8))
    return bill_value(message)