轉自:https://stackoverflow.com/questions/3283984/decode-hex-string-in-python-3
import codecs decode_hex = codecs.getdecoder("hex_codec") # for an array msgs = [decode_hex(msg)[0] for msg in msgs] # for a string string = decode_hex(string)[0]
該方法py2,py3通用。
應用
Python2
from Crypto.Cipher import AES aes = AES.new('323ae8281ce4492246c63d968011bfd3'.decode('hex'), AES.MODE_ECB) aes.decrypt('aaad9c5376ee2f20175cbec0a91b47d3e5956f2948468fe9deb61564642018f6320b63df16502fcd408ac7cdb8a78751'.decode('hex'))
Python 2/3
from Crypto.Cipher import AES import codecs decode_hex = codecs.getdecoder("hex_codec") aes = AES.new(decode_hex('323ae8281ce4492246c63d968011bfd3')[0], AES.MODE_ECB) aes.decrypt(decode_hex('aaad9c5376ee2f20175cbec0a91b47d3e5956f2948468fe9deb61564642018f6320b63df16502fcd408ac7cdb8a78751')[0])
import codecs decode_hex = codecs.getdecoder("hex_codec") encode_hex = codecs.getencoder("hex_codec") s = decode_hex('323ae8281ce4492246c63d968011bfd3')[0] print (s) s = encode_hex(s)[0] print (s)