XOR Python 文本加密/解密
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20557999/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XOR Python Text Encryption/Decryption
提问by Caddiana Runes
I know there is a built in xor operator that can be imported in Python. I'm trying to execute the xor encryption/decryption. So far I have:
我知道有一个可以在 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))
This:
这个:
> for letter in message:
print xor(bool(cipher), bool(keydecrypt))
is where my python starts to go wrong.
是我的 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
What I'm messing up on is trying to get these two binary(key and encrypted) to be compared and give true(1) or false(being 0). The xor should then give me a resulting 1 and 0 binary list from comparing the two. Any input?
我搞砸的是试图比较这两个二进制文件(密钥和加密)并给出真(1)或假(为 0)。然后,异或应该通过比较两者给我一个结果 1 和 0 二进制列表。任何输入?
回答by George
somecode = 'asdfln3j34tnonfdkjnflksdfnla'
message = 'this is my message'
def str_xor(s1, s2):
return "".join([chr(ord(c1) ^ ord(c2)) for (c1,c2) in zip(s1,s2)])
encoded = str_xor(message, somecode)
# encoded == '\x15\x1b\r\x15L\x07@J^MT\x03\n\x1d\x15\x05\x0c\x0f'
decoded = str_xor(encoded, somecode)
# decoded == 'this is my message'
This is a naive / minimalistic implementation without error checking. Here len(somecode) >= len(message) is required.
这是一个没有错误检查的天真/简约的实现。这里需要 len(somecode) >= len(message) 。
回答by jfs
Here's a variation of the code example from XOR Cipher Wikipedia article:
这是来自XOR Cipher Wikipedia 文章的代码示例的变体:
def xor(data, key):
return bytearray(a^b for a, b in zip(*map(bytearray, [data, key])))
Example (Python 2):
示例(Python 2):
>>> one_time_pad = 'shared secret'
>>> plaintext = 'unencrypted'
>>> ciphertext = xor(plaintext, one_time_pad)
>>> ciphertext
bytearray(b'\x06\x06\x04\x1c\x06\x16Y\x03\x11\x06\x16')
>>> decrypted = xor(ciphertext, one_time_pad)
>>> decrypted
bytearray(b'unencrypted')
>>> plaintext == str(decrypted)
True
回答by wookie
Code below works both ways and does not need length checking as cycle is used.
下面的代码可以双向工作,并且在使用循环时不需要长度检查。
from itertools import cycle, izip
cryptedMessage = ''.join(chr(ord(c)^ord(k)) for c,k in izip(message, cycle(key)))

