首页 > 解决方案 > 无法使用 Python 中的密钥解密文件

问题描述

我有一个正在尝试解密的视频文件。密钥存储在文件中。由于某些原因,它不起作用并给我这个错误"TypeError: Object type <class 'str'> cannot be passed to C code"

我写的 DecryptFile 函数需要 3 个参数

我想要它做的是使用提供的密钥解密文件并使用我给出的输出名称保存它。我正在使用 Python 3.7.1

from Crypto.Cipher import AES
import os

def DecryptFile(infile,outfile,keyfile):
    data = open(infile,"rb").read()
    key = open(keyfile,"rb").read()
    print(type(data))
    iv = '\x00'*15 + chr(1)
    aes_crypter = AES.new(key,  AES.MODE_CBC,  iv)
    a = aes_crypter.decrypt(data)
    with open(outfile, 'wb') as out_file:
        out_file.write(a)



DecryptFile("input.ts","output.ts","k.kjs")

标签: pythonpycryptodome

解决方案


根据[ReadTheDocs.PyCryptodome]: AES - Crypto.Cipher.AES.new( key, mode, *args, **kwargs )iv应该是:

  • 字节类型

要克服此错误,请修改 2 行代码:

# ...
iv = b'\x00' * 15 + b'\x01'
aes_crypter = AES.new(key, AES.MODE_CBC, iv=iv)
# ...

推荐阅读