So I've done this code(this is just the part where it transforms the string into hex), and when I try to decrypt I get this message: Non-hexadecimal digit found
Here is string to hex code:
def password (pslen):
alphabet = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+}{:|?><,./;'\[]"
pw_length = pslen
mypw = ""
for i in range(pw_length):
next_index = random.randrange(len(alphabet))
mypw = mypw + alphabet[next_index]
print ("Password: ",mypw)
parolaBin = bin(int.from_bytes(mypw.encode(), 'big'))
parolaHex = hex(int(parolaBin, 2))
return parolaHex
And here it is hex to string code
pw = binascii.unhexlify(pw)
The program reads pw from a file.
The program it's a lot bigger but this is the part that got me stuck and won't work.
The method that I used to convert string to hexadecimal worked but the hex to string didn't.
Can someone help me please?
You are converting an integer to hex with the
hex()
function:This includes a
0x
prefix, which is not a hexadecimal value. This is whatbinascii.unhexlify()
is complaining about:Simply remove that prefix, or don't generate it in the first place by using
format(integer, 'x')
instead:Your method of generating a random password in hexadecimal is... way too verbose. You could just generate random hex digits:
and get the same result with less work. At any rate, converting an integer to binary notation first then converting it back to an integer:
is certainly doing nothing more than keeping your CPU warm at night. You could have gone straight to hex: