Created
August 27, 2012 13:01
-
-
Save srikanthps/3488237 to your computer and use it in GitHub Desktop.
C# Code to Digitally Sign Using a .P12 file (its really a hack, for reference purposes only!!!)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Security.Cryptography; | |
using System.Security.Cryptography.X509Certificates; | |
using System.Security.Cryptography.Xml; | |
using System.Xml; | |
namespace DigitalSignature | |
{ | |
public class Program | |
{ | |
public static void Main(String[] args) | |
{ | |
try | |
{ | |
// Create a new XML document. | |
XmlDocument xmlDoc = new XmlDocument(); | |
X509Certificate2 uidCert = new X509Certificate2("C:\\Users\\m1007055\\Desktop\\public-may2012.p12", "public", X509KeyStorageFlags.DefaultKeySet); | |
// Load an XML file into the XmlDocument object. | |
xmlDoc.Load("C:\\test.xml"); | |
xmlDoc.PreserveWhitespace = true; | |
// Sign the XML document. | |
SignXml(xmlDoc, uidCert); | |
Console.WriteLine("XML file signed."); | |
// Save the document. | |
xmlDoc.Save("C:\\test-signed.xml"); | |
} | |
catch (Exception e) | |
{ | |
Console.WriteLine(e.Message); | |
} | |
finally | |
{ | |
System.Console.ReadLine(); | |
} | |
} | |
// Sign an XML file. | |
// This document cannot be verified unless the verifying | |
// code has the key with which it was signed. | |
public static void SignXml(XmlDocument xmlDoc, X509Certificate2 uidCert) | |
{ | |
RSACryptoServiceProvider rsaKey = (RSACryptoServiceProvider)uidCert.PrivateKey; | |
// Check arguments. | |
if (xmlDoc == null) | |
throw new ArgumentException("xmlDoc"); | |
if (rsaKey == null) | |
throw new ArgumentException("Key"); | |
// Create a SignedXml object. | |
SignedXml signedXml = new SignedXml(xmlDoc); | |
// Add the key to the SignedXml document. | |
signedXml.SigningKey = rsaKey; | |
// Create a reference to be signed. | |
Reference reference = new Reference(); | |
reference.Uri = ""; | |
// Add an enveloped transformation to the reference. | |
XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform(); | |
reference.AddTransform(env); | |
// Add the reference to the SignedXml object. | |
signedXml.AddReference(reference); | |
// Add an RSAKeyValue KeyInfo (optional; helps recipient find key to validate). | |
KeyInfo keyInfo = new KeyInfo(); | |
KeyInfoX509Data clause = new KeyInfoX509Data(); | |
clause.AddSubjectName(uidCert.Subject); | |
clause.AddCertificate(uidCert); | |
keyInfo.AddClause(clause); | |
signedXml.KeyInfo = keyInfo; | |
// Compute the signature. | |
signedXml.ComputeSignature(); | |
// Get the XML representation of the signature and save | |
// it to an XmlElement object. | |
XmlElement xmlDigitalSignature = signedXml.GetXml(); | |
System.Console.WriteLine(signedXml.GetXml().InnerXml); | |
// Append the element to the XML document. | |
xmlDoc.DocumentElement.AppendChild(xmlDoc.ImportNode(xmlDigitalSignature, true)); | |
} | |
} | |
} |
hi, i have a question about the prefix example that the example generate but i need ds:signature how can i add this prefix en la xml that the example generate
Need to install this from the Microsoft nuget
System.Security.Cryptography.Xml
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
where shoul i Get SignedXml class/ dll?