0

我正在使用 Python 中的 XOR 加密模型,到目前为止,除了数字和标点符号外,一切都很好。

任何数字或标点符号都会给你一个无效的二进制字符串,但是,至少在英文字母表中的任何基本字母都可以。

我在这里做错了什么?我已经将其追溯到这种方法:

def IMpassEnc(self,password):
    binaryList = ""
    for i in password:
        if i == " ":
            binaryList += "00100000"  #adding binary for space
        else:
            tmp = bin(int(binascii.hexlify(i),16)) #Binary Conversion
            newtemp = tmp.replace('b','')  
            binaryList += newtemp
    return binaryList
4

1 回答 1

2

您需要生成 8 位宽的二进制表示;bin()你。

产生这些结果的更好方法是使用format(value, '08b'); 这会产生 8 个字符宽的二进制表示,用 0 填充。此外;ord()获取给定字符的整数代码点将是一种更直接的方法:

>>> format(ord(' '), '08b')
'00100000'
>>> format(ord('a'), '08b')
'01100001'
>>> format(ord('!'), '08b')
'00100001'

或者,使用''.join()

def IMpassEnc(self, password):
    return ''.join(format(ord(c), '08b') for c in password)
于 2013-11-06T15:20:19.560 回答