首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用非对称密钥解密XML的InvalidCastException在.NET中失败

使用非对称密钥解密XML的InvalidCastException在.NET中失败
EN

Stack Overflow用户
提问于 2017-06-13 08:10:48
回答 1查看 738关注 0票数 1

使用.NET 4.6,我尝试执行以下操作:

  • 加载用于从PFX文件解密的密钥。
  • 从上面的文件中用私钥解密XML文档。

我的代码是基于MSDN的如何:用非对称密钥解密XML元素的。

代码语言:javascript
复制
CspParameters cspParams = new CspParameters();
cspParams.KeyContainerName = "XML_ENC_RSA_KEY";

// Load PFX
X509Certificate2 encryptionPfx = new X509Certificate2(@"file.pfx", "test_password");
RSACryptoServiceProvider rsaKey = new RSACryptoServiceProvider();
rsaKey = encryptionPfx.PrivateKey as RSACryptoServiceProvider;

// Decrypt XML
EncryptedXml encryptedXml = new EncryptedXml(xmlDocument);
encryptedXml.AddKeyNameMapping("TESTKEY", rsaKey);
encryptedXml.DecryptDocument();

在运行这段代码时,我得到下面的InvalidCastException,它是由方法GetDecryptionKey()引发的

代码语言:javascript
复制
Object of type "System.Security.Cryptography.RSACryptoServiceProvider" cannot be cast to "System.Security.Cryptography.SymmetricAlgorithm".

就我所理解的MSDN示例而言,这段代码应该从XML中提取EncryptedKey元素并用RSA密钥对密钥进行解密。

我在这里错过了什么?

下面是完整的堆栈跟踪:

代码语言:javascript
复制
System.InvalidCastException wurde nicht behandelt.
  HResult=-2147467262
  Message=Das Objekt des Typs "System.Security.Cryptography.RSACryptoServiceProvider" kann nicht in Typ "System.Security.Cryptography.SymmetricAlgorithm" umgewandelt werden.
  Source=System.Security
  StackTrace:
       bei System.Security.Cryptography.Xml.EncryptedXml.GetDecryptionKey(EncryptedData encryptedData, String symmetricAlgorithmUri)
       bei System.Security.Cryptography.Xml.EncryptedXml.DecryptDocument()
       bei XML_Encryption_Tools.XmlDecryption.DecryptDocument(XmlDocument xmlDocument) in C:\Users\user\Documents\Visual Studio 2015\Projects\XML Encryption Tools\XML Encryption Tools\XmlDecryption.cs:Zeile 38.
       bei XML_Encryption_Tools.Program.Main(String[] args) in C:\Users\user\Documents\Visual Studio 2015\Projects\XML Encryption Tools\XML Encryption Tools\Program.cs:Zeile 17.
       bei System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       bei System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       bei Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       bei System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       bei System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       bei System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

这是加密的XML:

代码语言:javascript
复制
<xenc:EncryptedData xmlns:xenc="http://www.w3.org/2001/04/xmlenc#" Id="G051f21d6-44b8-4ca8-abcd-bcec94a81ffa" Type="http://www.w3.org/2001/04/xmlenc#Element">
    <xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes256-cbc"/>
    <dsig:KeyInfo xmlns:dsig="http://www.w3.org/2000/09/xmldsig#">
      <dsig:KeyName>TESTKEY</dsig:KeyName>
        <xenc:EncryptedKey Id="EK65c1fdc2-a757-4482-8238-1d19c5d05006">
            <xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p">
                <dsig:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
            </xenc:EncryptionMethod>
            <xenc:CipherData>
                <xenc:CipherValue>Here is cipherValue_1</xenc:CipherValue>
            </xenc:CipherData>
        </xenc:EncryptedKey>
    </dsig:KeyInfo>
    <xenc:CipherData>
        <xenc:CipherValue>Here is cipherValue_2</xenc:CipherValue>
    </xenc:CipherData>
</xenc:EncryptedData>
EN

回答 1

Stack Overflow用户

发布于 2017-06-15 21:05:29

经过密集的调试,似乎加密的XML并不完全正确。如果我将KeyName元素作为EncryptedKey的子元素移动到一个新的KeyInfo元素,那么我的代码就能工作。

代码语言:javascript
复制
<xenc:EncryptedData xmlns:xenc="http://www.w3.org/2001/04/xmlenc#" Id="G051f21d6-44b8-4ca8-abcd-bcec94a81ffa" Type="http://www.w3.org/2001/04/xmlenc#Element">
    <xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes256-cbc"/>
    <dsig:KeyInfo xmlns:dsig="http://www.w3.org/2000/09/xmldsig#">
    <!-- This KeyName is found but not associated with the encrypted key. -->      
    <dsig:KeyName>TESTKEY</dsig:KeyName>
        <xenc:EncryptedKey Id="EK65c1fdc2-a757-4482-8238-1d19c5d05006">
            <xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p">
            <!-- If I place the KeyInfo and KeyName here, my code works. -->
            <dsig:KeyInfo xmlns:dsig="http://www.w3.org/2000/09/xmldsig#">
                <dsig:KeyName>TESTKEY</dsig:KeyName>
            </dsig:KeyInfo>
                <dsig:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
            </xenc:EncryptionMethod>
            <xenc:CipherData>
                <xenc:CipherValue>Here is cipherValue_1</xenc:CipherValue>
            </xenc:CipherData>
        </xenc:EncryptedKey>
    </dsig:KeyInfo>
    <xenc:CipherData>
        <xenc:CipherValue>Here is cipherValue_2</xenc:CipherValue>
    </xenc:CipherData>
</xenc:EncryptedData>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44515786

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档