diff --git a/EduNetworkBuilder/EduNetworkBuilder.csproj b/EduNetworkBuilder/EduNetworkBuilder.csproj index 5864e97..f018215 100644 --- a/EduNetworkBuilder/EduNetworkBuilder.csproj +++ b/EduNetworkBuilder/EduNetworkBuilder.csproj @@ -72,6 +72,7 @@ + @@ -175,6 +176,7 @@ RTFWindow.cs + Form diff --git a/EduNetworkBuilder/TrippleDESDocumentEncryption.cs b/EduNetworkBuilder/TrippleDESDocumentEncryption.cs new file mode 100644 index 0000000..2544d54 --- /dev/null +++ b/EduNetworkBuilder/TrippleDESDocumentEncryption.cs @@ -0,0 +1,122 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Security.Cryptography; +using System.Security.Cryptography.Xml; +using System.Xml; + +namespace EduNetworkBuilder +{ + /// + /// Copied from https://msdn.microsoft.com/en-us/library/system.security.cryptography.xml.encryptedxml(v=vs.110).aspx + /// + class TrippleDESDocumentEncryption + { + protected XmlDocument docValue; + protected TripleDES algValue; + + public TrippleDESDocumentEncryption(XmlDocument Doc, TripleDES Key) + { + if (Doc != null) + { + docValue = Doc; + } + else + { + throw new ArgumentNullException("Doc"); + } + + if (Key != null) + { + + algValue = Key; + } + else + { + throw new ArgumentNullException("Key"); + } + } + + public XmlDocument Doc { set { docValue = value; } get { return docValue; } } + public TripleDES Alg { set { algValue = value; } get { return algValue; } } + + public void Clear() + { + if (algValue != null) + { + algValue.Clear(); + } + else + { + throw new Exception("No TripleDES key was found to clear."); + } + } + + public void Encrypt(string Element) + { + // Find the element by name and create a new + // XmlElement object. + XmlElement inputElement = docValue.GetElementsByTagName(Element)[0] as XmlElement; + + // If the element was not found, throw an exception. + if (inputElement == null) + { + throw new Exception("The element was not found."); + } + + // Create a new EncryptedXml object. + EncryptedXml exml = new EncryptedXml(docValue); + + // Encrypt the element using the symmetric key. + byte[] rgbOutput = exml.EncryptData(inputElement, algValue, false); + + // Create an EncryptedData object and populate it. + EncryptedData ed = new EncryptedData(); + + // Specify the namespace URI for XML encryption elements. + ed.Type = EncryptedXml.XmlEncElementUrl; + + // Specify the namespace URI for the TrippleDES algorithm. + ed.EncryptionMethod = new EncryptionMethod(EncryptedXml.XmlEncTripleDESUrl); + + // Create a CipherData element. + ed.CipherData = new CipherData(); + + // Set the CipherData element to the value of the encrypted XML element. + ed.CipherData.CipherValue = rgbOutput; + + // Replace the plaintext XML elemnt with an EncryptedData element. + EncryptedXml.ReplaceElement(inputElement, ed, false); + } + + public void Decrypt() + { + + // XmlElement object. + XmlElement encryptedElement = docValue.GetElementsByTagName("EncryptedData")[0] as XmlElement; + + // If the EncryptedData element was not found, throw an exception. + if (encryptedElement == null) + { + throw new Exception("The EncryptedData element was not found."); + } + + // Create an EncryptedData object and populate it. + EncryptedData ed = new EncryptedData(); + ed.LoadXml(encryptedElement); + + // Create a new EncryptedXml object. + EncryptedXml exml = new EncryptedXml(); + + // Decrypt the element using the symmetric key. + byte[] rgbOutput = exml.DecryptData(ed, algValue); + + // Replace the encryptedData element with the plaintext XML elemnt. + exml.ReplaceData(encryptedElement, rgbOutput); + + } + + } +}