title: Decrypting XML with asymmetric keys fails with InvalidCastException in .NET
body: <p>Using .NET 4.6 I try to do the following:</p>
  
  <ul>
  <li>Load the key for decryption from a PFX file.</li>
  <li>Decrypt an XML document with the private key from the file above.</li>
  </ul>
  
  <p>My code is based on <a href="https://msdn.microsoft.com/de-de/library/ms229919(v=vs.110).aspx" rel="nofollow noreferrer">How to: Decrypt XML Elements with Asymmetric Keys</a> from MSDN.</p>
  
  <pre><code>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();
  </code></pre>
  
  <p>When running this code I get the following <code>InvalidCastException</code> which is thrown by the method <code>GetDecryptionKey()</code>:</p>
  
  <pre><code>Object of type "System.Security.Cryptography.RSACryptoServiceProvider" cannot be cast to "System.Security.Cryptography.SymmetricAlgorithm".
  </code></pre>
  
  <p>As far as I have understood the MSDN example, this code should pick up the <code>EncryptedKey</code> element from the XML and decrypt the key with the RSA key.</p>
  
  <p>What am I missing here?</p>
  
  <p>Here is the complete stack trace:</p>
  
  <pre><code>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: 
  </code></pre>
  
  <p>This is the encrypted XML:</p>
  
  <pre><code>&lt;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"&gt;
      &lt;xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes256-cbc"/&gt;
      &lt;dsig:KeyInfo xmlns:dsig="http://www.w3.org/2000/09/xmldsig#"&gt;
        &lt;dsig:KeyName&gt;TESTKEY&lt;/dsig:KeyName&gt;
          &lt;xenc:EncryptedKey Id="EK65c1fdc2-a757-4482-8238-1d19c5d05006"&gt;
              &lt;xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p"&gt;
                  &lt;dsig:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/&gt;
              &lt;/xenc:EncryptionMethod&gt;
              &lt;xenc:CipherData&gt;
                  &lt;xenc:CipherValue&gt;Here is cipherValue_1&lt;/xenc:CipherValue&gt;
              &lt;/xenc:CipherData&gt;
          &lt;/xenc:EncryptedKey&gt;
      &lt;/dsig:KeyInfo&gt;
      &lt;xenc:CipherData&gt;
          &lt;xenc:CipherValue&gt;Here is cipherValue_2&lt;/xenc:CipherValue&gt;
      &lt;/xenc:CipherData&gt;
  &lt;/xenc:EncryptedData&gt;
  </code></pre>