Python2使用encode('hex')


轉自: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)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM