Appendix

268 views
207 views

Published on

Published in: Technology, News & Politics
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
268
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Appendix

  1. 1. SENIOR PROJECT 2007-2008 (Appendix of the ekoSign project) Appendix Project team members Hüseyin Çakır, Mehmet Mesut Özışık, Yılmaz Kaya Abstract:This paper includes the codes of the some classes used in the project. Keywords:Encryption class, Decryption class, Signature class, verfySignature class. http://groups.google.com/group/digitalsignature digitalsignature@googlegroups.com PRINT DATE: 05/06/08 1
  2. 2. 1. Encryption Class using using using using using using using using using using using using System; System.Data; System.Configuration; System.Web; System.Web.Security; System.Web.UI; System.Web.UI.WebControls; System.Web.UI.WebControls.WebParts; System.Web.UI.HtmlControls; System.Xml; System.Security.Cryptography; System.Security.Cryptography.Xml; /// <summary> /// encryption: encrypts xml data with RSA algorithm, reference: http://msdn2.microsoft.com/en-us/library/ms229749(VS.80).aspx /// </summary> public class encryption { public void Encrypt(XmlDocument Doc, string ElementToEncrypt, RSA Alg, string KeyName) { // Check the arguments. if (Doc == null) throw new ArgumentNullException("Doc"); if (ElementToEncrypt == null) throw new ArgumentNullException("ElementToEncrypt"); if (Alg == null) throw new ArgumentNullException("Alg"); if (KeyName == null) throw new ArgumentNullException("KeyName"); XmlElement xmlElemt = Doc.GetElementsByTagName(ElementToEncrypt)[0] as XmlElement; EncryptedXml xmlEnc = new EncryptedXml(Doc); xmlEnc.AddKeyNameMapping(KeyName,Alg); EncryptedData encXml = xmlEnc.Encrypt(xmlElemt,KeyName); EncryptedXml.ReplaceElement(xmlElemt, encXml, false); } } 2
  3. 3. 2. Decryption Class using using using using using using using using using using using using System; System.Data; System.Configuration; System.Web; System.Web.Security; System.Web.UI; System.Web.UI.WebControls; System.Web.UI.WebControls.WebParts; System.Web.UI.HtmlControls; System.Xml; System.Security.Cryptography; System.Security.Cryptography.Xml; /// <summary> /// decryption: decrypts xml data with RSA algorithm, reference: http://msdn2.microsoft.com/en-us/library/ms229749(VS.80).aspx /// </summary> public class decryption { public void Decrypt(XmlDocument Doc, RSA Alg, string KeyName) { // Check the arguments. if (Doc == null) throw new ArgumentNullException("Doc"); if (Alg == null) throw new ArgumentNullException("Alg"); if (KeyName == null) throw new ArgumentNullException("KeyName"); // 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(KeyName, Alg); // Decrypt the element. exml.DecryptDocument(); } } 3
  4. 4. 3. Signature Class using using using using using using using using using using using using System; System.Data; System.Configuration; System.Web; System.Web.Security; System.Web.UI; System.Web.UI.WebControls; System.Web.UI.WebControls.WebParts; System.Web.UI.HtmlControls; System.Xml; System.Security.Cryptography; System.Security.Cryptography.Xml; /// <summary> /// signature class: Signs multiple Xml according to the reference.Uri /// </summary> public class signature { public void SignXml(XmlDocument Doc, RSA Key, int c) { // Check arguments. if (Doc == null) throw new ArgumentException("Doc"); if (Key == null) throw new ArgumentException("Key"); // Create a SignedXml object. SignedXml signedXml = new SignedXml(Doc); // Add the key to the SignedXml document. signedXml.SigningKey = Key; // Create a reference to be signed.<<Create a Reference object that describes what to sign.>> Reference reference = new Reference(); if (c == 1) { reference.Uri = "#c"; } else if (c == 2) { reference.Uri = "#s"; } else if (c == 3) { } reference.Uri = "#m"; 4
  5. 5. // Add an enveloped transformation to the reference. XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform(); reference.AddTransform(env); // Add the reference to the SignedXml object. signedXml.AddReference(reference); // Compute the signature. signedXml.ComputeSignature(); // Get the XML representation of the signature and save // it to an XmlElement object. XmlElement xmlDigitalSignature = signedXml.GetXml(); true)); // Append the element to the XML document. Doc.DocumentElement.AppendChild(Doc.ImportNode(xmlDigitalSignature, } } 5
  6. 6. 4. verifySignature Class using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Xml; using System.Security.Cryptography; using System.Security.Cryptography.Xml; /// <summary> /// verifySignature class: Verifies multiple Xml according to the reference.Uri /// </summary> public class verifySignature { // Verify the signature of an XML file against an asymmetric // algorithm and return the result. public Boolean VerifyXml(XmlDocument Doc, RSA Key,int i) { // Check arguments. if (Doc == null) throw new ArgumentException("Doc"); if (Key == null) throw new ArgumentException("Key"); // Create a new SignedXml object and pass it // the XML document class. SignedXml signedXml = new SignedXml(Doc); // Find the "Signature" node and create a new // XmlNodeList object. XmlNodeList nodeList = Doc.GetElementsByTagName("Signature"); // Throw an exception if no signature was found. if (nodeList.Count <= 0) { throw new CryptographicException("Verification failed: No Signature was found in the document."); } else{ // Load the first <signature> node. signedXml.LoadXml((XmlElement)nodeList[i]); // Check the signature and return the result. return signedXml.CheckSignature(Key); } } } 6

×