密码术十六进制,二进制文件和ascii python

时间:2015-01-05 17:57:53

标签: python encryption cryptography

我是密码学的新手,当谈到xor时,我真的很困惑。 鉴于ascii中的文本和十六进制中的密码,我如何才能使它们具有相同的格式? 我目前的代码是:

import binascii
string = b'09e1c5f70a65ac519458e7e53f36'
binary = binascii.unhexlify(string)
#This make the hex string to raw bytes

我的问题是如何才能将ascii字符串设置为原始字节,以便我可以xor? 或者如果不可能我该怎么办xor?

1 个答案:

答案 0 :(得分:2)

ascii只是一个字节字符串

XOR_WITH = 0x12

def xor_encode(byte):
    if isinstance(byte,basestring):
        byte = ord(byte) #convert to ascii integer value
    byte = byte ^ XOR_WITH #encode
    return chr(byte) # convert it back to a string and return

encoded_string = "".join(map(xor_encode,"Test String"))

也许你在寻找什么