在 Python 中使用 OpenSSL 解密 CMS

Nic*_*ick 5 python openssl pkcs#7

我目前正在尝试使用 Python 解密一些 CMS 加密文本。我一直无法找到可以在 OpenSSL 中使用 CMS 实现的库(尝试过 M2Crypto、PyOpenSSL、PyCrypto)。

发送给我的消息包含使用以下 Java 加密的数据:

public static byte[] cmsEncrypt(byte[] data, Certificate cert) throws NoSuchAlgorithmException, NoSuchProviderException, CMSException, IOException {
    CMSEnvelopedDataGenerator gen = new CMSEnvelopedDataGenerator();
    gen.addKeyTransRecipient((X509Certificate) cert);
    CMSProcessable cmsData = new CMSProcessableByteArray(data);
    CMSEnvelopedData enveloped = gen.generate(cmsData, CMSEnvelopedDataGenerator.AES128_CBC, 128, "BC");
    return enveloped.getEncoded();
}
Run Code Online (Sandbox Code Playgroud)

这个 Java 包含一些已弃用的方法,不幸的是我无法控制它们。我可以使用 Python OpenSSL 模块来解密此 CMS 加密数据吗?截至目前,我正在使用 bash OpenSSL 命令使用以下 Python 进行解密:

from subprocess import call
decrypt = call(['openssl', 'cms', '-decrypt', '-binary', '-inkey', 'key.pem', '-in', 'message.msg'])
Run Code Online (Sandbox Code Playgroud)

我更愿意完全在 Python 中完成此操作,而不必使用 shell OpenSSL 命令。