简体   繁体   中英

How to convert from binary to bytes to hex in Python 3?

I'm trying to write a program that converts two hex strings to bytes, and from bytes to binary. Once in binary, I want to perform an XOR transposition on them. This having been accomplished, I want to convert the binary strings back to bytes, and again into hex. I already know what the answer should be, it's just a question of getting from A to B.

The code I have so far is as follows:

input1 = "1c0111001f010100061a024b53535009181c"
input2 = "686974207468652062756c6c277320657965"

target = "746865206b696420646f6e277420706c6179"

d = conversions.hexconvert(input1)
e = conversions.hexconvert(input2)

print(d)
print(e)

f = bitstring.BitArray(d)
g = bitstring.BitArray(e)

xor1 = f.bin
xor2 = g.bin

print("xor1 is", xor1)
print("xor2 is", xor2)

xor1, xor2 = xor2, xor1

print("xor1 is now:", xor1)

The function "hexconvert" is comprised of the following code:

import codecs

def hexconvert(input):
    output = codecs.decode(input, 'hex')
    return(output)

My code is currently spitting out the following:

b'\x1c\x01\x11\x00\x1f\x01\x01\x00\x06\x1a\x02KSSP\t\x18\x1c'
b"hit the bull's eye"
xor1 is    : 000111000000000100010001000000000001111100000001000000010000000000000110000110100000001001001011010100110101001101010000000010010001100000011100
xor2 is    : 011010000110100101110100001000000111010001101000011001010010000001100010011101010110110001101100001001110111001100100000011001010111100101100101
xor1 is now: 011010000110100101110100001000000111010001101000011001010010000001100010011101010110110001101100001001110111001100100000011001010111100101100101

All good so far. I'd like to know what I can add to the end of this code to convert xor1 to bytes then to hex so that I can compare it to the result it should be. I've been trying to figure out how to use struct, binascii, and even bitstring, but I'm getting nowhere. Any and all suggestions greatly appreciated.

It would also be great if anyone could suggest how to make the code more efficient.

Thanks very much in advance!

You don't have to convert to bits here; you can XOR bytes just fine. When you iterate over a bytes object you get the individual values as integers in the range 0-255, and you can XOR those . Vice versa, you can create a new bytes object again from a sequence of integers.

Convert from hex to bytes with binascii.unhexlify() , back again with binascii.hexlify() :

from binascii import hexlify, unhexlify

bytes1, bytes2 = unhexlify(input1), unhexlify(input2)
xor_bytes = bytes([b1 ^ b2 for b1, b2 in zip(bytes1, bytes2)])

result = hexlify(xor_bytes).decode('ascii')

The decode is there to convert the bytes output of hexlify back to a string.

Demo:

>>> from binascii import hexlify, unhexlify
>>> input1 = "1c0111001f010100061a024b53535009181c"
>>> input2 = "686974207468652062756c6c277320657965"
>>> bytes1, bytes2 = unhexlify(input1), unhexlify(input2)
>>> xor_bytes = bytes([b1 ^ b2 for b1, b2 in zip(bytes1, bytes2)])
>>> xor_bytes
b"the kid don't play"
>>> hexlify(xor_bytes).decode('ascii')
'746865206b696420646f6e277420706c6179'

If all you need is to xor two hex strings:

>>> hex(int(input1, 16) ^ int(input2, 16))[2:]
'746865206b696420646f6e277420706c6179'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM