擅长:python、mysql、java
<p>下面是<a href="http://en.wikipedia.org/wiki/XOR_cipher" rel="noreferrer">XOR Cipher Wikipedia article</a>中代码示例的变体:</p>
<pre><code>def xor(data, key):
return bytearray(a^b for a, b in zip(*map(bytearray, [data, key])))
</code></pre>
<p>示例(Python 2):</p>
<pre><code>>>> 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
</code></pre>