# encryption – byte operations (XOR) in python

## Comparison of two python3 solutions

The first one is based on zip:

``````    def encrypt1(var, key):
return bytes(a ^ b for a, b in zip(var, key))
``````

The second one uses int.from_bytes and int.to_bytes:

``````    def encrypt2(var, key, byteorder=sys.byteorder):
key, var = key[:len(var)], var[:len(key)]
int_var = int.from_bytes(var, byteorder)
int_key = int.from_bytes(key, byteorder)
int_enc = int_var ^ int_key
return int_enc.to_bytes(len(var), byteorder)
``````

Simple tests:

``````assert encrypt1(bhello, bsupersecretkey) == bx1bx10x1ctx1d
assert encrypt2(bhello, bsupersecretkey) == bx1bx10x1ctx1d
``````

Performance tests with `var` and `key` being 1000 bytes long:

``````\$ python3 -m timeit
-s import test_xor;a=babcdefghij*100;b=b0123456789*100
test_xor.encrypt1(a, b)
10000 loops, best of 3: 100 usec per loop

\$ python3 -m timeit
-s import test_xor;a=babcdefghij*100;b=b0123456789*100
test_xor.encrypt2(a, b)
100000 loops, best of 3: 5.1 usec per loop
``````

The integer approach seems to be significantly faster.

It looks like what you need to do is XOR each of the characters in the message with the corresponding character in the key. However, to do that you need a bit of interconversion using `ord` and `chr`, because you can only xor numbers, not strings:

``````>>> encrypted = [ chr(ord(a) ^ ord(b)) for (a,b) in zip(var, key) ]
>>> encrypted
[x1b, x10, x1c, t, x1d]

>>> decrypted = [ chr(ord(a) ^ ord(b)) for (a,b) in zip(encrypted, key) ]
>>> decrypted
[h, e, l, l, o]

>>> .join(decrypted)
hello
``````

Note that `binascii.a2b_qp(hello)` just converts a string to another string (though possibly with different encoding).

Your approach, and my code above, will only work if the key is at least as long as the message. However, you can easily repeat the key if required using `itertools.cycle`:

``````>>> from itertools import cycle
>>> var=hello
>>> key=xy

>>> encrypted = [ chr(ord(a) ^ ord(b)) for (a,b) in zip(var, cycle(key)) ]
>>> encrypted
[x10, x1c, x14, x15, x17]

>>> decrypted = [ chr(ord(a) ^ ord(b)) for (a,b) in zip(encrypted, cycle(key)) ]
>>> .join(decrypted)
hello
``````

To address the issue of unicode/multi-byte characters (raised in the comments below), one can convert the string (and key) to bytes, zip these together, then perform the XOR, something like:

``````>>> var=uhellou2764
>>> var
hello❤

>>> encrypted = [ a ^ b for (a,b) in zip(bytes(var, utf-8),cycle(bytes(key, utf-8))) ]
>>> encrypted
[27, 16, 28, 9, 29, 145, 248, 199]

>>> decrypted = [ a ^ b for (a,b) in zip(bytes(encrypted), cycle(bytes(key, utf-8))) ]
>>> decrypted
[104, 101, 108, 108, 111, 226, 157, 164]

>>> bytes(decrypted)
bhelloxe2x9dxa4

>>> bytes(decrypted).decode()
hello❤
``````

#### encryption – byte operations (XOR) in python

You can use Numpy to perform faster

``````import numpy as np
def encrypt(var, key):
a = np.frombuffer(var, dtype = np.uint8)
b = np.frombuffer(key, dtype = np.uint8)
return (a^b).tobytes()
``````