按位异或 python

Bitwise xor python

我正在尝试解决必须解密文件的问题。但是我发现了一个障碍。正如您在下面的代码中看到的,我需要在键和数字 47 之间进行按位异或运算。

from Crypto.Cipher import AES
import base64

l1 = open("./2015_03_13_mohamed.said.benmousa.puerta_trasera.enc", "rb");
iv = l1.read(16)
enc = l1.read()
l1.close()

#key = xor beetwen kiv(47) and IV

key = iv
for i in range(len(key)):
    key[i] = key[i] ^ 47

obj = AES.new(key,AES.MODE_CBC, iv)
obj1 = obj.decrypt(enc)

l = open("./ej3.html", "wb").write(obj1)

当我尝试这样做时,出现以下错误:

TypeError: unsupported operand type(s) for ^: 'str' and 'int'

我在这里搜索了一些东西,但找不到。谢谢。

因为你需要一个整数

ascii_code_for_a = ord('a') == 97 #convert a character to an ascii integer

int2chr = chr(97) == 'a'  #convert an ascii code back to a character

就这样吧

key = list(iv) # first convert it to a list ... strings are unmutable
for i in range(len(key)):
    key[i] = chr(ord(key[i]) ^ 47)