按位XOR Python [英] Bitwise xor python

查看:73
本文介绍了按位XOR Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to solve a problem where I have to decrypt a file. But I found an obstacle. As you can see in the below code, I need to do a bitwise xor between key and number 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)

当我尝试出现以下错误时:

When I try that I get the following error:

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

我在这里搜索了东西,但找不到.谢谢.

I searched things here but I can't get it. Thank you.

推荐答案

因为您需要一个整数

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)

这篇关于按位XOR Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆