python - 无法使用 Python 中的密钥解密文件
问题描述
我有一个正在尝试解密的视频文件。密钥存储在文件中。由于某些原因,它不起作用并给我这个错误"TypeError: Object type <class 'str'> cannot be passed to C code"
我写的 DecryptFile 函数需要 3 个参数
- 输入文件名(“input.ts”)
- 输出文件名(“output.ts”)
- 解密密钥(“k.kjs”)。
我想要它做的是使用提供的密钥解密文件并使用我给出的输出名称保存它。我正在使用 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")
解决方案
根据[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)
# ...
推荐阅读
- groovy - 如何使用 Groovy 评估
- python - 如何在字符串(单元格)数组中查找字符,Python
- tensorflow - 减少固定 TF 估计器的损失
- codeigniter - 使用多语言支持在 Codeigniter 中创建用户友好的 URL
- aws-amplify - 尝试通过 API Gateway 从 AWS 检索数据时收到错误“getTokens 不支持在签名时检索令牌”
- java - 是否可以使用二进制序列化对象作为数据格式来创建 presto 表?
- c# - 带有 api-key 的 ASPNET Core API
- json - jq 可以检查逗号分隔的值数组的每个元素以检查该值是否存在于 JSON 中?
- flutter - 在多条路线上共享集团,而不是在我的整个应用程序中
- sql - 通过加入 XML 最大化查询性能