byte operations (XOR) in python

后端 未结 2 892
孤城傲影
孤城傲影 2021-02-04 07:00
    #!/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         


        
2条回答
  •  温柔的废话
    2021-02-04 07:51

    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):
        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.

提交回复
热议问题