Guest User

Untitled

a guest
Dec 11th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. using System.Security.Cryptography;
  7. using System.Security.Cryptography.X509Certificates;
  8. using System.Security.Cryptography.Xml;
  9.  
  10. using System.Xml;
  11.  
  12. namespace DigitalSignature
  13. {
  14. public class Program
  15. {
  16. public static void Main(String[] args)
  17. {
  18. try
  19. {
  20. // Create a new XML document.
  21. XmlDocument xmlDoc = new XmlDocument();
  22.  
  23. X509Certificate2 uidCert = new X509Certificate2("C:\\Users\\m1007055\\Desktop\\public-may2012.p12", "public", X509KeyStorageFlags.DefaultKeySet);
  24.  
  25.  
  26. // Load an XML file into the XmlDocument object.
  27. xmlDoc.Load("C:\\test.xml");
  28. xmlDoc.PreserveWhitespace = true;
  29.  
  30. // Sign the XML document.
  31. SignXml(xmlDoc, uidCert);
  32.  
  33. Console.WriteLine("XML file signed.");
  34.  
  35. // Save the document.
  36. xmlDoc.Save("C:\\test-signed.xml");
  37.  
  38. }
  39. catch (Exception e)
  40. {
  41. Console.WriteLine(e.Message);
  42. }
  43. finally
  44. {
  45. System.Console.ReadLine();
  46. }
  47. }
  48.  
  49.  
  50. // Sign an XML file.
  51. // This document cannot be verified unless the verifying
  52. // code has the key with which it was signed.
  53. public static void SignXml(XmlDocument xmlDoc, X509Certificate2 uidCert)
  54. {
  55.  
  56. RSACryptoServiceProvider rsaKey = (RSACryptoServiceProvider)uidCert.PrivateKey;
  57.  
  58.  
  59. // Check arguments.
  60. if (xmlDoc == null)
  61. throw new ArgumentException("xmlDoc");
  62. if (rsaKey == null)
  63. throw new ArgumentException("Key");
  64.  
  65. // Create a SignedXml object.
  66. SignedXml signedXml = new SignedXml(xmlDoc);
  67.  
  68. // Add the key to the SignedXml document.
  69. signedXml.SigningKey = rsaKey;
  70.  
  71.  
  72. // Create a reference to be signed.
  73. Reference reference = new Reference();
  74. reference.Uri = "";
  75.  
  76. // Add an enveloped transformation to the reference.
  77. XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform();
  78. reference.AddTransform(env);
  79.  
  80. // Add the reference to the SignedXml object.
  81. signedXml.AddReference(reference);
  82.  
  83.  
  84. // Add an RSAKeyValue KeyInfo (optional; helps recipient find key to validate).
  85. KeyInfo keyInfo = new KeyInfo();
  86.  
  87. KeyInfoX509Data clause = new KeyInfoX509Data();
  88. clause.AddSubjectName(uidCert.Subject);
  89. clause.AddCertificate(uidCert);
  90. keyInfo.AddClause(clause);
  91. signedXml.KeyInfo = keyInfo;
  92.  
  93. // Compute the signature.
  94. signedXml.ComputeSignature();
  95.  
  96. // Get the XML representation of the signature and save
  97. // it to an XmlElement object.
  98. XmlElement xmlDigitalSignature = signedXml.GetXml();
  99.  
  100. System.Console.WriteLine(signedXml.GetXml().InnerXml);
  101.  
  102. // Append the element to the XML document.
  103. xmlDoc.DocumentElement.AppendChild(xmlDoc.ImportNode(xmlDigitalSignature, true));
  104.  
  105.  
  106. }
  107.  
  108. }
  109.  
  110. }
Add Comment
Please, Sign In to add comment