从hex到ascii在python中

时间:2018-05-26 16:05:26

标签: python hex ascii python-2.x xor

嘿,我写了这个剧本:

import re

arrayofhex =[]
hexedarr=[]
string_input = raw_input()
key = "0x3c"
encodehex = string_input.encode("hex")

hexsplit = re.findall('..?', encodehex)


for letter in hexsplit:
     a = hex(int(letter,16))
     arrayofhex.append(a)

print arrayofhex

print "Xoring with Key 0x3c turns to:"

for hexedletter in arrayofhex:

    xor = hex(int(hexedletter,16) ^ int(key,16))
    hexedarr.append(xor)

print hexedarr

正如你所看到的那样,用户可以插入一些字符串然后代码使它成为一个十六进制和xor它的密钥是0x3c,我想获得xoring进程的ascii可以有人帮忙吗?

1 个答案:

答案 0 :(得分:0)

好吧,所以我做了一些修改,让它在这里工作的是完整的脚本,效果很好!!

import re

Array_of_hex =[]
Hexed_array=[]
Xord_array=[]

string_input = raw_input()
key = "0x3c"

Encode_to_hex = string_input.encode("hex")

Hex_split = re.findall('..?', Encode_to_hex)


for letter in Hex_split:
     a = hex(int(letter,16))
     Array_of_hex.append(a)

print Array_of_hex

print "Xoring with Key 0x3c turns to:"

for hexedletter in Array_of_hex:

    xor = hex(int(hexedletter,16) ^ int(key,16))
    Hexed_array.append(xor)

print Hexed_array


for hextoascii_letter in Hexed_array:
    toascii = chr(int(hextoascii_letter,16))
    Xord_array.append(toascii)


xord_string = "".join(Xord_array)
print "Xored Ascii:"
print xord_string