按位xor python

时间:2015-04-03 16:56:43

标签: python bitwise-operators xor

我正在尝试解决我必须解密文件的问题。但我发现了一个障碍。正如您在下面的代码中看到的,我需要在key和47之间执行按位xor。

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'

我在这里搜索了一些东西,但我无法得到它。谢谢。

1 个答案:

答案 0 :(得分:0)

因为你需要一个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)