z *_* - 26
你拿了一个密钥,比如0101,然后你用它来对你的字符串进行异或(以二进制格式)来实现一个加密的字符串.
0101 XOR <-- key
1011 <---- original message
----
1110 <-- send message
Run Code Online (Sandbox Code Playgroud)
您将1110发送给您的接收器.然后,该接收器获取接收到的字符串并使用密钥对其进行异或,以获取原始消息:
1110 XOR <--- received message
0101 <-- key
----
1011 <--- original message
Run Code Online (Sandbox Code Playgroud)
Ale*_*own 11
XOR或'exclusive or'是一个2操作数逻辑操作,定义如下:
(a and b) or (not a and not b)
a b result
0 0 0
1 0 1
0 1 1
1 1 0
Run Code Online (Sandbox Code Playgroud)
关于加密的XOR的关键特征是它是可逆的,即在C = A XOR B的情况下,那么你可以使用A = C XOR B返回A.
因此,对于明文A流和相同长度B的密钥,您可以生成密码文本C,并将其发送给收件人.
收件人在他的保险箱中有B副本,可以做C XOR B并重新生成A.
XOR是一个逻辑运算,发音为exclusive或.它可以用来简单快速地加密消息.您可以在此处查看此操作的真值表:http://mathworld.wolfram.com/XOR.html
准伪代码实现(通过http://www.evanfosmark.com/2008/06/xor-encryption-with-python/):
#!/usr/bin/env python
from itertools import izip, cycle
def xor_crypt_string(data, key):
return ''.join(chr(ord(x) ^ ord(y)) for (x,y) in izip(data, cycle(key)))
my_data = "Hello. This is a secret message! How fun."
my_key= "firefly"
# Do the actual encryption
encrypted = xor_crypt_string(my_data, key=my_key)
print encrypted
print '---->'
# This will obtain the original data from the encrypted
original = xor_crypt_string(encrypted, key=my_key)
print original
Run Code Online (Sandbox Code Playgroud)
输出:
. BY2F
FRR
DF$IB
---->
Hello. This is a secret message! How fun.
Run Code Online (Sandbox Code Playgroud)