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