prep for encrypting xml
This commit is contained in:
parent
7f8879a19b
commit
3bed5e7210
@ -72,6 +72,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Reference Include="System" />
|
<Reference Include="System" />
|
||||||
<Reference Include="System.Core" />
|
<Reference Include="System.Core" />
|
||||||
|
<Reference Include="System.Security" />
|
||||||
<Reference Include="System.Xml.Linq" />
|
<Reference Include="System.Xml.Linq" />
|
||||||
<Reference Include="System.Data.DataSetExtensions" />
|
<Reference Include="System.Data.DataSetExtensions" />
|
||||||
<Reference Include="Microsoft.CSharp" />
|
<Reference Include="Microsoft.CSharp" />
|
||||||
@ -175,6 +176,7 @@
|
|||||||
<DependentUpon>RTFWindow.cs</DependentUpon>
|
<DependentUpon>RTFWindow.cs</DependentUpon>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="SchoolworkClass.cs" />
|
<Compile Include="SchoolworkClass.cs" />
|
||||||
|
<Compile Include="TrippleDESDocumentEncryption.cs" />
|
||||||
<Compile Include="VLANConfig.cs">
|
<Compile Include="VLANConfig.cs">
|
||||||
<SubType>Form</SubType>
|
<SubType>Form</SubType>
|
||||||
</Compile>
|
</Compile>
|
||||||
|
122
EduNetworkBuilder/TrippleDESDocumentEncryption.cs
Normal file
122
EduNetworkBuilder/TrippleDESDocumentEncryption.cs
Normal file
@ -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
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Copied from https://msdn.microsoft.com/en-us/library/system.security.cryptography.xml.encryptedxml(v=vs.110).aspx
|
||||||
|
/// </summary>
|
||||||
|
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);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user