我知道有一个内置的xor操作符可以在Python中导入。我正在尝试执行xor加密/解密。到目前为止,我已经:
def xor_attmpt():
message = raw_input("Enter message to be ciphered: ")
cipher = []
for i in message:
cipher.append(bin(ord(i))[2::])#add the conversion of the letters/characters
#in your message from ascii to binary withoout the 0b in the front to your ciphered message list
cipher = "".join(cipher)
privvyKey = raw_input("Enter the private key: ")
keydecrypt = []
for j in privvyKey:
keydecrypt.append(bin(ord(j))[2::]) #same
keydecrypt = "".join(keydecrypt )#same
print "key is '{0}'" .format(keydecrypt) #substitute values in string
print "encrypted text is '{0}'" .format(cipher)
from operator import xor
for letter in message:
print xor(bool(cipher), bool(keydecrypt))
这个:
^{pr2}$是我的python开始出错的地方。在
The ouput looks like this
Enter message to be ciphered: hello
Enter the private key: \@154>
key is '10111001000000110001110101110100111110'
encrypted text is '11010001100101110110011011001101111'
False
False
False
False
False
我要搞砸的是试图比较这两个二进制(密钥和加密的)并给出true(1)或false(0)。然后,xor应该给我一个通过比较二者得到的1和0二进制列表。有什么意见吗?在
bool()
构造函数将任何值转换为True
或{bool(cipher)
和{True
和{忘记转换为0和1的字符串,您实际需要做的只是异或调用
ord()
得到的字符代码,然后转换回带有chr()
的字符。另外,您不需要导入函数,Python有一个功能完善的异或运算符^
。在这样的事情应该有用:
你犯了一些错误:
cipher
和keydecrypt
不是二进制的,它们是包含0和1个字符的字符串。在xor
函数,您已经有了xor运算符^
for
应该做什么。在下面是一个关于如何改进代码的示例:
如果
key
小于message
,也可以这样做。在相关问题 更多 >
编程相关推荐