0
I need to decrypt this XML:
<?xml version="1.0" encoding="UTF-8"?>
<xenc:EncryptedData
xmlns:xenc="http://www.w3.org/2001/04/xmlenc#"
Type="http://www.w3.org/2001/04/xmlenc#Element">
<xenc:EncryptionMethod
Algorithm="http://www.w3.org/2001/04/xmlenc#aes256-cbc" />
<ds:KeyInfo xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
<ds:X509Data>
<ds:X509Certificate></ds:X509Certificate>
</ds:X509Data>
<xenc:EncryptedKey
xmlns:xenc="http://www.w3.org/2001/04/xmlenc#">
<xenc:EncryptionMethod
Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" />
<xenc:CipherData>
<xenc:CipherValue></xenc:CipherValue>
</xenc:CipherData>
</xenc:EncryptedKey>
</ds:KeyInfo>
<xenc:CipherData>
<xenc:CipherValue></xenc:CipherValue>
</xenc:CipherData>
This is my project I’m running:
using System;
using System.Xml;
using System.Security.Cryptography;
using System.Security.Cryptography.Xml;
using System.Security.Cryptography.X509Certificates;
class Program
{
static void Main(string[] args)
{
// Create an XmlDocument object.
XmlDocument xmlDoc = new XmlDocument();
string arquivoXML = "C:\\Teste-Desc\\1-23042015-0703-ACE58971ACE59070.xml";
string thumbPrintCertificado = "BA71F3AA888E0197D945D2A0CDE21C7E694CE432";
// Load an XML file into the XmlDocument object.
try
{
xmlDoc.PreserveWhitespace = true;
xmlDoc.Load(arquivoXML);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
// Create a new TripleDES key.
TripleDESCryptoServiceProvider tDESkey = new TripleDESCryptoServiceProvider();
X509Certificate2 cert = BuscaConfiguracaoCertificado(thumbPrintCertificado);
RSACryptoServiceProvider rsaKey = (RSACryptoServiceProvider)cert.PrivateKey;
try
{
Decrypt(xmlDoc, rsaKey);
// Display the encrypted XML to the console.
Console.WriteLine();
Console.WriteLine("Decrypted XML:");
Console.WriteLine();
Console.WriteLine(xmlDoc.OuterXml);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
// Clear the TripleDES key.
tDESkey.Clear();
}
}
public static void Encrypt(XmlDocument Doc, string ElementToEncrypt, TripleDESCryptoServiceProvider Alg)
{
////////////////////////////////////////////////
// Find the specified element in the XmlDocument
// object and create a new XmlElemnt object.
////////////////////////////////////////////////
XmlElement elementToEncrypt = Doc.GetElementsByTagName(ElementToEncrypt)[0] as XmlElement;
// Throw an XmlException if the element was not found.
if (elementToEncrypt == null)
{
throw new XmlException("The specified element was not found");
}
//////////////////////////////////////////////////
// Create a new instance of the EncryptedXml class
// and use it to encrypt the XmlElement with the
// symmetric key.
//////////////////////////////////////////////////
EncryptedXml eXml = new EncryptedXml();
byte[] encryptedElement = eXml.EncryptData(elementToEncrypt, Alg, false);
////////////////////////////////////////////////
// Construct an EncryptedData object and populate
// it with the desired encryption information.
////////////////////////////////////////////////
EncryptedData edElement = new EncryptedData();
edElement.Type = EncryptedXml.XmlEncElementUrl;
// Create an EncryptionMethod element so that the
// receiver knows which algorithm to use for decryption.
// Determine what kind of algorithm is being used and
// supply the appropriate URL to the EncryptionMethod element.
edElement.EncryptionMethod = new EncryptionMethod(EncryptedXml.XmlEncTripleDESUrl);
// Add the encrypted element data to the
// EncryptedData object.
edElement.CipherData.CipherValue = encryptedElement;
////////////////////////////////////////////////////
// Replace the element from the original XmlDocument
// object with the EncryptedData element.
////////////////////////////////////////////////////
EncryptedXml.ReplaceElement(elementToEncrypt, edElement, false);
}
public static void Decrypt(XmlDocument Doc, RSA Alg)
{
// Check the arguments.
if (Doc == null)
throw new ArgumentNullException("Doc");
if (Alg == null)
throw new ArgumentNullException("Alg");
// Create a new EncryptedXml object.
EncryptedXml exml = new EncryptedXml(Doc);
// Add a key-name mapping.
// This method can only decrypt documents
// that present the specified key name.
exml.AddKeyNameMapping("rsaKey", Alg);
// Decrypt the element.
exml.DecryptDocument();
}
public static X509Certificate2 BuscaConfiguracaoCertificado(string sDigitalThumbPrint)
{
X509Certificate2 x509Cert = null;
X509Store store = new X509Store("MY", StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
X509Certificate2Collection collection = (X509Certificate2Collection)store.Certificates;
X509Certificate2Collection collection1 = null;
collection1 = (X509Certificate2Collection)collection.Find(X509FindType.FindByThumbprint, sDigitalThumbPrint, false);
for (int i = 0; i < collection1.Count; i++)
{
x509Cert = collection1[i];
}
return x509Cert;
}
}
But when it comes down to it:
exml.DecryptDocument();
It generates the following exception: Unable to recover decryption key.
Second manual of the TJMG I must use the private key of the digital certificate of the customer...
I don’t know what else to do...
What is the exception class? (and preferably, the stack trace, to let us know exactly which method launched it) I don’t know about XML encryption, but if I know more about the exception I can see if I can find something to help you determine the cause of the error. By the way, you has the private key of the customer’s digital certificate, don’t you? Who encrypted this document?
– mgibsonbr
The TJMG who generates the XML, he uses the public key of the client certificate to encrypt it, now I have to use the private to decrypt... About how it works... http://selos.tjmg.jus.br/developer/solicitaca-stamps.html The exception and the dot I put on the last lines..
– Renan Borges
"The exception and the point I put in the last lines.." where? I don’t see, the question only says "It generates the following exception: It is not possible to recover the decryption key." I understand it’s that line of code that doesn’t work, but to know where the exception is originated, only with the stack trace. The exception class also helps when searching for a solution. (by the way, the exception text is the same, in Portuguese?)
– mgibsonbr
I just noticed here that your XML specifies the encryption algorithm as AES256-CBC, but you are trying to decipher it using 3DES...
– mgibsonbr