Skip to content

Instantly share code, notes, and snippets.

@srikanthps
Created August 27, 2012 13:01
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save srikanthps/3488237 to your computer and use it in GitHub Desktop.
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!!!)
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));
}
}
}
@amitsingh308
Copy link

where shoul i Get SignedXml class/ dll?

@NomedigasYisus
Copy link

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

@wilari932
Copy link

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