Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Appendix

328 views

Published on

Published in: Technology, News & Politics
  • Be the first to comment

  • Be the first to like this

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

×