#!/usr/bin/env python3
import binascii
var=binascii.a2b_qp(\"hello\")
key=binascii.a2b_qp(\"supersecretkey\")[:len(var)]
print(binascii.b2a_qp(var))
print(binasc
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):
key = key[:len(var)]
int_var = int.from_bytes(var, sys.byteorder)
int_key = int.from_bytes(key, sys.byteorder)
int_enc = int_var ^ int_key
return int_enc.to_bytes(len(var), sys.byteorder)
Simple tests:
assert encrypt1(b'hello', b'supersecretkey') == b'\x1b\x10\x1c\t\x1d'
assert encrypt2(b'hello', b'supersecretkey') == b'\x1b\x10\x1c\t\x1d'
Performance tests with var
and key
being 1000 bytes long:
$ python3 -m timeit \
-s "import test_xor;a=b'abcdefghij'*100;b=b'0123456789'*100" \
"test_xor.encrypt1(a, b)"
10000 loops, best of 3: 100 usec per loop
$ python3 -m timeit \
-s "import test_xor;a=b'abcdefghij'*100;b=b'0123456789'*100" \
"test_xor.encrypt2(a, b)"
100000 loops, best of 3: 5.1 usec per loop
The integer approach seems to be significantly faster.