问题描述
我需要使用Python 3使用xor加密/解密文件,我有一个在Python 2中可以正常工作的代码,但是当尝试将其适用于Python 3时,会给我一些我无法解决的错误。 / p>
此代码在Python 2.7中正常工作:
from itertools import cycle
def xore(data,key):
return''。(zip(ord) (data,cycle(key)))
with open('inputfile.jpg','rb')as encryption,open('outputfile.jpg','wb')as decrypt:
decrypt.write(xore(encrypt.read(),'anykey'))
错误当尝试在python 3中运行不变:
追溯(最近的最后一次调用):
文件ask.py ,第8行,< module>
decrypt.write(xore(encrypt.read(),'anykey'))
文件ask.py,第5行,xore
return''.join(chr(ord (a,b)中的zip(数据,循环(key))中的(a)^ ord(b))
文件ask.py,第5行,< genexpr>
TypeError:ord()预期的字符串(),(b))for(a,b)in zip(data,cycle(key)的长度为1,但int发现
请是否有人可以解释并帮助我将此代码适应Python 3
a
已经是一个 int
,所以您需要删除对 ord(a)
的调用:
<$ p $ (数据,关键字):
return''。(zip,(key))中的(a,b) ))
如果没有$ $ c $,您将无法将加入的字符串写入outfile c> .encode(utf-8)这不会给你任何可用的输出,所以不知道你实际想要实现什么。
你可以看到你在Python中找到一个int字符串的索引:
n [1]:s = b foo
在[2]中:s [0]
输出[2]:102#f
其中python2你得到str / char:
在[1]中:s = bfoo
在[2 ]:s [0]
Out [2]:'f'
迭代或调用下一个还给你整数值:
在[12]中:it = iter(s)
在[13 ]:next(it)
出[13]:102
在[14]中:对于ele中的ele:
print(ele)
....:
102
111
111
在python2中,您只需获取每个字符。所以在你的代码中迭代从 encrypt.read()
返回的字节对象,你得到整数值,所以 ord(some_int) code>明显失败。
有一个彻底的解释,它解释了python2和python3之间的区别,其代码片段是:
I need to encrypt / decrypt a file using xor with Python 3, I have a code that works fine in Python 2, but when trying to adapt it to Python 3, gives me some errors that I can't solve.
This code works fine in Python 2.7:
from itertools import cycle
def xore(data, key):
return ''.join(chr(ord(a) ^ ord(b)) for (a, b) in zip(data, cycle(key)))
with open('inputfile.jpg', 'rb') as encry, open('outputfile.jpg', 'wb') as decry:
decry.write(xore(encry.read(), 'anykey'))
The error when trying to run unchanged in python 3:
Traceback (most recent call last):
File "ask.py", line 8, in <module>
decry.write(xore(encry.read(), 'anykey'))
File "ask.py", line 5, in xore
return ''.join(chr(ord(a) ^ ord(b)) for (a, b) in zip(data, cycle(key)))
File "ask.py", line 5, in <genexpr>
return ''.join(chr(ord(a) ^ ord(b)) for (a, b) in zip(data, cycle(key)))
TypeError: ord() expected string of length 1, but int found
Please if someone could explain and help me adapt this code to Python 3.
解决方案 a
is already an int
so you would need to remove the call to ord(a)
:
def xore(data, key):
return ''.join(chr(a ^ ord(b)) for (a, b) in zip(data, cycle(key)))
You will not be able to write the joined string to your outfile without a .encode("utf-8")
which will not give you any usable output so not sure what you are actually trying to achieve.
You can see when you index a byte string you get an int in python3:
n [1]: s = b"foo"
In [2]: s[0]
Out[2]: 102 # f
Where in python2 you get the str/char:
In [1]: s = b"foo"
In [2]: s[0]
Out[2]: 'f'
iterating or calling next also gives you the integer value:
In [12]: it = iter(s)
In [13]: next(it)
Out[13]: 102
In [14]: for ele in s:
print(ele)
....:
102
111
111
In python2 you would just get each character. So in your code as you iterate over the bytes objects returned from encry.read()
you are getting the integer values so ord(some_int)
obviously fails.
There is a thorough explanation text-versus-binary-data which explains the differences between python2 and python3 a snippet of which is:
这篇关于使用Python对文件进行Xor加密/解密3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!