Python cryptographic XOR string and hexadecimal
topic description
the hexadecimal string output after sha256 encryption is to be XOR with the contents of an array (32 array elements, each with a length of one byte). My question is how to convert a hexadecimal string to the corresponding binary and XOR in the array.
sources of topics and their own ideas
The main problem of
is: each character in hexadecimal character is represented by a byte (8bit), and the original binary bit represented by each character is only (4bit), and every two such data is XOR with data such as 0x0D in the array. How should this be realized?
The key to performing bit operations in
python is that both operators must be of the same type and length. Each element of the
B type array is a 1-byte int, so you can convert your [hexadecimal string] into a sequence of 1-byte int. The quickest way is to convert directly to a byte string.
>>> import array
>>> my_arr = array.array('B', range(32)) -sharpB
>>> my_arr
array('B', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31])
>>> hex_str = '68656c6c6f20776f726c6421' -sharp
>>> hex_bytes = bytes.fromhex(hex_str) -sharp
>>> hex_bytes -sharp
b'hello world!'
>>> hex_bytes[0] ^ my_arr[0] -sharp
104