I'm trying to sign an XML Document using the following algorithm in C# :
When I try to sign it with a random RSA Key it works perfectly.
XmlDocument xmlDoc = new XmlDocument(); xmlDoc.PreserveWhitespace = false; xmlDoc.Load("hpbtest.xml"); RSA Key = new RSACryptoServiceProvider(2048); // Create a SignedXml object. PrefixedSignedXML signedXml = new PrefixedSignedXML(xmlDoc); // Add the key to the SignedXml document. signedXml.SigningKey = Key; signedXml.SignedInfo.SignatureMethod = "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"; // Create a reference to be signed. Reference reference = new Reference(); reference.Uri = "#xpointer(//*[@authenticate='true'])"; reference.DigestMethod = "http://www.w3.org/2001/04/xmlenc#sha256"; // Add an enveloped transformation to the reference. XmlDsigExcC14NTransform env = new XmlDsigExcC14NTransform(); env.Algorithm = "http://www.w3.org/TR/2001/REC-xml-c14n-20010315"; reference.AddTransform(env); // Add the reference to the SignedXml object. signedXml.AddReference(reference); // Compute the signature. signedXml.ComputeSignature("ds"); // Get the XML representation of the signature and save // it to an XmlElement object. XmlElement xmlDigitalSignature = signedXml.GetXml("ds"); // Append the element to the XML document. xmlDoc.DocumentElement.AppendChild(xmlDoc.ImportNode(xmlDigitalSignature, true)); xmlDoc.Save("hpbtest.xml"); But if i want to use an RSA Key from a Certificate :
XmlDocument xmlDoc = new XmlDocument(); xmlDoc.PreserveWhitespace = false; xmlDoc.Load("hpbtest.xml"); RSA Key = new GestionCertificat("CN=Bruno").getClePrivee();//Get the private key // Create a SignedXml object. PrefixedSignedXML signedXml = new PrefixedSignedXML(xmlDoc); // Add the key to the SignedXml document. signedXml.SigningKey = Key; signedXml.SignedInfo.SignatureMethod = "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"; // Create a reference to be signed. Reference reference = new Reference(); reference.Uri = "#xpointer(//*[@authenticate='true'])"; reference.DigestMethod = "http://www.w3.org/2001/04/xmlenc#sha256"; // Add an enveloped transformation to the reference. XmlDsigExcC14NTransform env = new XmlDsigExcC14NTransform(); env.Algorithm = "http://www.w3.org/TR/2001/REC-xml-c14n-20010315"; reference.AddTransform(env); // Add the reference to the SignedXml object. signedXml.AddReference(reference); // Compute the signature. signedXml.ComputeSignature("ds"); // Get the XML representation of the signature and save // it to an XmlElement object. XmlElement xmlDigitalSignature = signedXml.GetXml("ds"); // Append the element to the XML document. xmlDoc.DocumentElement.AppendChild(xmlDoc.ImportNode(xmlDigitalSignature, true)); xmlDoc.Save("hpbtest.xml"); I get this error :
Invalid specified algorithm
In these two examples, my RSA Keys have the same length (2048) and I have no idea why I get this error.
Thank you !
Thomas
No comments:
Post a Comment