Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

byte operations (XOR) in python

    #!/usr/bin/env python3

import binascii


var=binascii.a2b_qp("hello")
key=binascii.a2b_qp("supersecretkey")[:len(var)]

print(binascii.b2a_qp(var))
print(binascii.b2a_qp(key))


#here i want to do an XOR operation on the bytes in var and key and place them in 'encryption': encryption=var XOR key

print(binascii.b2a_qp(encrypted))

If someone could enlighten me on how I could accomplish this I would be very happy. Very new to the whole data-type conversions so yeah...reading through the python wiki is not as clear as I would like :(.

like image 989
Jcov Avatar asked Apr 02 '15 08:04

Jcov


People also ask

Can you XOR bytes Python?

You can use single bytes or multiple byte keys for XOR, and we will use looping to test keys. Here's the XOR truth table: 0 ^ 0 = 0. 0 ^ 1 = 1.

What is XOR operation in Python?

In Python, XOR is a bitwise operator that is also known as Exclusive OR. It is a logical operator which outputs 1 when either of the operands is 1 (one is 1 and the other one is 0), but both are not 1, and both are not 0. The symbol for XOR in Python is '^' and in mathematics, its symbol is '⊕'.

How bitwise XOR operator works in Python?

XOR in Python is also known as “exclusive or” that compares two binary numbers bitwise. If both bits are the same, the XOR operator outputs 0. If both bits are different, the XOR operator outputs 1. The Bitwise XOR sets the input bits to 1 if either, but not both, of the analogous bits in the two operands is 1.

Can you Bitshift in Python?

Bitwise Right Shift OperatorPython right shift operator is exactly the opposite of the left shift operator. Then left side operand bits are moved towards the right side for the given number of times. In simple terms, the right side bits are removed.


1 Answers

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(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.

like image 193
Vincent Avatar answered Sep 30 '22 00:09

Vincent