0

我们收到一个加密的 XML 文档,其中一些内容使用对称密钥加密,对称密钥在 XML 中提供,使用我们证书的公钥加密。我试图用我们的私钥解密后一部分,但它一直抛出错误

这是我的代码,非常感谢帮助

public static void Decrypt(XmlDocument Doc, RSA privateKey, string KeyName)
{
      XmlElement xmlelement = Doc.GetElementsByTagName("bla")[0] as XmlElement;
      EncryptedKey encryptedKey = new EncryptedKey();
      encryptedKey.LoadXml(xmlelement);

       //How is the privateKey mapped to encryptedKey??
       //DecryptEncryptedKey throws a value null exception

       EncryptedXml exml = new EncryptedXml(Doc); 
       byte[] decrOut = exml.DecryptEncryptedKey(encryptedKey);            
}
4

1 回答 1

0

你必须预先加载 XmlDocument 和认证,即

XmlDocument doc 对象与您的 xml X509Certificate2 x509ServiceProvider(您的证书与私钥)

        // Go and get the encrypted key node
        XmlElement encryptedCipherValueElement = (XmlElement)doc.SelectSingleNode("/XPATH to CipherValue i.e. encrypted symmetric key");

        // These are the input bytes to be decrypted
        byte[] encryptedCipherBytes = Convert.FromBase64String(encryptedCipherValueElement.InnerText);

        // The RSA service provider is necessary as we can't just rely on IIS to decrypt stuff
        RSACryptoServiceProvider rsaServiceProvider = x509ServiceProvider.PrivateKey as RSACryptoServiceProvider;

        // We want to use PKCS1 v1.5 padding which corresponds to OEAP padding being false 
        // This is what other vendors appear to be using. This may become a parameter
        // in time
        const bool OeapPadding = false;
        byte[] decryptedCipherBytes = rsaServiceProvider.Decrypt(encryptedCipherBytes, OeapPadding);

        // We want to wipe out any lingering references to keys or algorithms as
        // soon as possible
        rsaServiceProvider.Clear();

        return decryptedCipherBytes;
于 2015-03-23T17:17:07.203 回答