C# EnvelopedCms解密不适用于Azure密钥库

C# EnvelopedCms解密不适用于Azure密钥库,c#,.net,encryption,pkcs#7,azure-keyvault,C#,.net,Encryption,Pkcs#7,Azure Keyvault,我已经为此苦苦挣扎了好几天了,RFC2315有点难以理解 我正在尝试实现我自己版本的EnvelopedCms.Decrypt(),这样我就可以使用Azure密钥库的证书操作来以正确的方式解开密钥和/或解密PKCS#7消息(CMS对象)。我使用.Net中的EnevelopedCms对消息进行解码,然后尝试解密信封中的内容 这就是我试图做的 public static async Task<byte[]> DecryptCustom(string certificateId, strin

我已经为此苦苦挣扎了好几天了,RFC2315有点难以理解

我正在尝试实现我自己版本的
EnvelopedCms.Decrypt()
,这样我就可以使用Azure密钥库的证书操作来以正确的方式
解开密钥和/或
解密PKCS#7消息(CMS对象)。我使用.Net中的EnevelopedCms对消息进行
解码
,然后尝试
解密
信封中的
内容

这就是我试图做的

public static async Task<byte[]> DecryptCustom(string certificateId, string encryptedBase64Content)
{
    var bytes = Convert.FromBase64String(encryptedBase64Content);
    var contentInfo = new ContentInfo(bytes);
    var envelopedCms = new EnvelopedCms(contentInfo);
    envelopedCms.Decode(bytes);
    // envelopedCms.Decrypt()  <-- no go. Can't extract certificate from Key Vault

    // My (naive) attempt to decrypt CMS content using Azure Key Vault certificates
    byte[] decryptedContent;
    using (var client = new KeyVaultClient(GetKeyVaultToken))
    {
        var decryptionresult = await client.DecryptAsync(GetKeyUrl(certificateId), "RSA1_5", envelopedCms.ContentInfo.Content);
        decryptedContent = decryptionresult.Result;
    }
    return decryptedContent;
}
公共静态异步任务解密自定义(字符串certificateId、字符串encryptedBase64Content)
{
var bytes=Convert.FromBase64String(encryptedBase64Content);
var contentInfo=新contentInfo(字节);
var envelopedCms=新的envelopedCms(内容信息);
包络CMS.解码(字节);

//envelopedCms.Decrypt()CMS信封内容使用会话密钥进行加密,并且在传输之前使用每个收件人(可能有许多)公钥对该密钥进行加密

您需要提取收件人的加密会话密钥,并使用密钥库中存储的私钥将其打开。我现在不在Visual Studio附近,但以下是伪代码:

// Extract the first (and often only) receiver's encrypted session key
var key = envelopedCms.Receivers[0].EncryptionKey; 
// Unwrap the sessionKey using the receiver's private key stored in key vault:
var sessionKey = (await keyVaultClient.Unwrap(uri, "certificatename", key)).Result;

最后,使用sessionKey,您可以解密信封内容(ContentInfo.Content)。加密类型在信封的加密算法属性中指定。

谢谢!这帮助我解决了这个问题。我现在可以在Azure密钥库中使用私有证书解密CMS对象了!