我在使用AES_CBC密码解码函数时遇到了一个小问题。当我假设(通过一个打开的文件(x,'wb'))返回的是字节时,python会抱怨在write()函数中获取字符串。当我假设(open(x,'w')的返回是string时,python会抱怨在write()函数中获取字节。有人能解释一下这个问题吗?(Python 3.5.1供Win7参考)
def decrypt(in_file, out_file, key,iv):
bs = AES.block_size
cipher = AES.new(key,AES.MODE_CBC,iv)
next_chunk = ''
finished = False
while not finished:
try:
chunk, next_chunk = next_chunk, cipher.decrypt(in_file.read(bs))
except ValueError:
finished = True
out_file.write(chunk)
with open(in_filename, 'rb') as in_file, open(out_filename, 'wb') as out_file:
decrypt(in_file, out_file, key, iv)
结果
^{pr2}$将文件打开语句更改为
with open(in_filename, 'rb') as in_file, open(out_filename, 'w') as out_file:
decrypt(in_file, out_file, key, iv)
结果
TypeError: write() argument must be str, not bytes
我只能用str(chunk)将chunk转换为string, 但是我的输出文件是一堆字节字符串
b", 'Shutdown', 'R"b"eboot', 'SetPowe"b"rSave'} (string "
有什么想法吗?我做错什么了?仅供参考,文件是在Fedora PC上使用CryptoPP加密的。当我在CryptoPP中解码时,一切正常,但这是我第一次使用pycrypto。在
目前没有回答
相关问题 更多 >
编程相关推荐