2017-06-13 108 views
1

使用.NET 4.6我儘量做到以下幾點:與非對稱密鑰解密XML失敗,InvalidCastException的.NET中

  • 負載從一個PFX文件解密的密鑰。
  • 解密XML文檔與從上面的文件中的私鑰。

我的代碼基於來自MSDN的How to: Decrypt XML Elements with Asymmetric Keys

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()拋出:

Object of type "System.Security.Cryptography.RSACryptoServiceProvider" cannot be cast to "System.Security.Cryptography.SymmetricAlgorithm". 

據我理解了MSDN例如,此代碼應該拿起從EncryptedKey元件XML並使用RSA密鑰解密密鑰。

我在這裏錯過了什麼?

下面是完整的堆棧跟蹤:

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:

<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> 
+0

如果可能的話,你可以在這行之後添加一個斷點:rsaKey = encryptionPfx.PrivateKey作爲RSACryptoServiceProvider;'確保rsaKey在此之後不爲空?這是因爲一些網上搜索後,似乎你的代碼是除了那些幾行完全正確的(通常它'的RSACryptoServiceProvider rsaKey =新的RSACryptoServiceProvider(cspParams);') –

+0

@KeyurPATEL的'rsaKey'不爲空此行之後,並根據調試器它包含​​從PFX文件加載的正確密鑰。 –

+0

請提供一個[mcve] - 你談論'GetDecryptionKey',但這不是你方法的一部分。 (或者,如果*是*你的方法,這將是值得這麼說的話,並顯示該行拋出異常......) –

回答

0

經過緊張的調試看來,如果加密的XML是不太正確的。如果我的KeyName元素移動到一個新的KeyInfo元素爲EncryptedKey個孩子,我的代碼工作。

<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>