python Crypto AES密码解码类型

时间:2016-01-22 22:22:51

标签: python python-3.x pycrypto

我在使用AES_CBC密码解码功能方面遇到了一些问题。当我假设(通过文件打开(x,' wb'))它的返回是字节时,python抱怨在write()函数中获取字符串。当我假设(open(x,' w'))它的返回是字符串时,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)

结果

TypeError: a bytes-like object is required, not 'str'

将文件打开语句更改为

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)的字符串, 但是我的输出文件是一串字节字符串,如

b", 'Shutdown', 'R"b"eboot', 'SetPowe"b"rSave'} (string "

有什么想法?我在这做错了什么?仅供参考使用CryptoPP在Fedora PC上加密文件。当我在CryptoPP中解码它时,一切正常,但这是我第一次使用pycrypto。

0 个答案:

没有答案