|
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Schema;
namespace Core.Cryptography
{
/// <summary>
/// WrappedData contains a piece of data that has been encrypted. The clear text data was
/// encrypted using a temporary session key. This temporary session key is encrypyted with
/// the requestor's public key. This class provides a convienient way to pass encrypted
/// information along with the data needed to decrypt it by the requestor.
/// </summary>
public class WrappedData : System.Xml.Serialization.IXmlSerializable
{
private string _certificate;
private string _encryptedData;
private string _iv;
private string _encryptedKey;
/// <summary>
/// IV used in the symmetric key encryption
/// </summary>
public string IV
{
get { return _iv; }
set { _iv = value; }
}
/// <summary>
/// The encrypted temporary Symmetric key
/// </summary>
public string EncryptedKey
{
get { return _encryptedKey; }
set { _encryptedKey = value; }
}
/// <summary>
/// Certificate used to encrypt the data
/// </summary>
public string Certificate
{
get { return _certificate; }
set { _certificate = value; }
}
/// <summary>
/// The secret encrypted by the temporary Symmetric key
/// </summary>
public string EncryptedData
{
get { return _encryptedData; }
set { _encryptedData = value; }
}
// IXmlSerializable is implemented since it allows for the Wrapped Data to be easily
// serialzed to and from XML. This XML serialized format can be readily stored or
// transferred.
#region IXmlSerializable Members
private void SchemaValidation(object source, ValidationEventArgs args)
{
}
public System.Xml.Schema.XmlSchema GetSchema()
{
StringBuilder sb = new StringBuilder();
sb.Append("<?xml version='1.0' encoding='utf-8'?>");
sb.Append("<xs:schema id='XMLSchema1' targetNamespace='http://www.oswsolutions.com/WrappedData.xsd' elementFormDefault='qualified' xmlns='http://www.oswsolutions.com/WrappedData.xsd' xmlns:mstns='http://www.oswsolutions.com/WrappedData.xsd' xmlns:xs='http://www.w3.org/2001/XMLSchema'>");
sb.Append(" <xs:complexType name='WrappedData'>");
sb.Append(" <xs:sequence>");
sb.Append(" <xs:element name='Certificate' type='xs:string' />");
sb.Append(" <xs:element name='EncryptedKey' type='xs:string' />");
sb.Append(" <xs:element name='EncryptedData' type='xs:string' />");
sb.Append(" <xs:element name='IV' type='xs:string' />");
sb.Append(" </xs:sequence>");
sb.Append(" </xs:complexType>");
sb.Append(" <xs:element name='Data' type='WrappedData' />");
sb.Append("</xs:schema>");
System.IO.StringReader reader = new System.IO.StringReader(sb.ToString() );
XmlSchema schema = XmlSchema.Read(reader, new ValidationEventHandler( this.SchemaValidation) );
return schema;
}
public void ReadXml(System.Xml.XmlReader reader)
{
XPathDocument document = new XPathDocument(reader);
XPathNavigator navigator = document.CreateNavigator();
XPathNodeIterator iter = navigator.Select("/Data");
if (iter.MoveNext() == true)
{
if (iter.Current.MoveToChild("Certificate", string.Empty) == true)
this.Certificate = iter.Current.Value;
else
throw new ApplicationException("Invalid Wrapped Key: No certificate");
if (iter.Current.MoveToNext("EncryptedKey", string.Empty))
this.EncryptedKey = iter.Current.Value;
else
throw new ApplicationException("Invalid Wrapped Key: No EncryptedKey");
if (iter.Current.MoveToNext("EncryptedData", string.Empty) == true)
this.EncryptedData = iter.Current.Value;
else
throw new ApplicationException("Invalid Wrapped Key: No EncryptedData");
if (iter.Current.MoveToNext("IV", string.Empty) == true)
this.IV = iter.Current.Value;
else
throw new ApplicationException("Invalid Wrapped Key: No IV");
}
}
public void WriteXml(System.Xml.XmlWriter writer)
{
XmlDocument doc = new XmlDocument();
XmlElement data = doc.CreateElement("Data");
XmlElement cert = doc.CreateElement("Certificate");
XmlElement encData = doc.CreateElement("EncryptedData");
XmlElement encKey = doc.CreateElement("EncryptedKey");
XmlElement iv = doc.CreateElement("IV");
cert.InnerText = Certificate;
encData.InnerText = EncryptedData;
encKey.InnerText = EncryptedKey;
iv.InnerText = IV;
doc.AppendChild(data);
data.AppendChild(cert);
data.AppendChild(encKey);
data.AppendChild(encData);
data.AppendChild(iv);
doc.WriteTo(writer);
}
#endregion
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.