[英]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))
如果有人能啟發我如何做到這一點,我會很高興。 對整個數據類型轉換來說非常新,所以是的……閱讀 python wiki 並不像我想要的那樣清楚。
第一個基於zip :
def encrypt1(var, key):
return bytes(a ^ b for a, b in zip(var, key))
第二個使用int.from_bytes和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)
簡單測試:
assert encrypt1(b'hello', b'supersecretkey') == b'\x1b\x10\x1c\t\x1d'
assert encrypt2(b'hello', b'supersecretkey') == b'\x1b\x10\x1c\t\x1d'
var
和key
性能測試長度為1000字節:
$ 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
整數方法似乎明顯更快。
看起來你需要做的是將消息中的每個字符與密鑰中的相應字符進行異或。 但是,要做到這一點,你需要使用ord
和chr
進行一些相互轉換,因為你只能編號而不是字符串:
>>> 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'
請注意, binascii.a2b_qp("hello")
只是將字符串轉換為另一個字符串(盡管可能使用不同的編碼)。
您的方法和我上面的代碼只有在密鑰至少與消息一樣長時才會起作用。 但是,如果需要,您可以使用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'
為了解決unicode /多字節字符的問題(在下面的注釋中提出),可以將字符串(和鍵)轉換為字節,將它們壓縮在一起,然后執行XOR,如:
>>> var=u"hello\u2764"
>>> 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)
b'hello\xe2\x9d\xa4'
>>> bytes(decrypted).decode()
'hello❤'
您可以使用 Numpy 更快地執行
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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.