I am trying to create a license file and I need it encrypted.
I have License object and List<License>licenses. I am needing to encrypt the stream prior to saving it to the xml file so that it can not be easily read.
I have found this post: MSDN Code: Writing Class Data to an XML File (Visual C#)
public class Book
{
public string title;
static void Main()
{
Book introToVCS = new Book();
introToVCS.title = "Intro to Visual CSharp";
System.Xml.Serialization.XmlSerializer writer =
new System.Xml.Serialization.XmlSerializer(introToVCS.GetType());
System.IO.StreamWriter file =
new System.IO.StreamWriter("c:\\IntroToVCS.xml");
writer.Serialize(file, introToVCS);
file.Close();
}
}
And this post: CodeProject: Using CryptoStream in C#
Writing the xml file:
FileStream stream = new FileStream(�C:\\test.txt�,
FileMode.OpenOrCreate,FileAccess.Write);
DESCryptoServiceProvider cryptic = new DESCryptoServiceProvider();
cryptic.Key = ASCIIEncoding.ASCII.GetBytes(�ABCDEFGH�);
cryptic.IV = ASCIIEncoding.ASCII.GetBytes(�ABCDEFGH�);
CryptoStream crStream = new CryptoStream(stream,
cryptic.CreateEncryptor(),CryptoStreamMode.Write);
byte[] data = ASCIIEncoding.ASCII.GetBytes(�Hello World!�);
crStream.Write(data,0,data.Length);
crStream.Close();
stream.Close();
Reading the xml file:
FileStream stream = new FileStream(�C:\\test.txt�,
FileMode.Open,FileAccess.Read);
DESCryptoServiceProvider cryptic = new DESCryptoServiceProvider();
cryptic.Key = ASCIIEncoding.ASCII.GetBytes(�ABCDEFGH�);
cryptic.IV = ASCIIEncoding.ASCII.GetBytes(�ABCDEFGH�);
CryptoStream crStream = new CryptoStream(stream,
cryptic.CreateDecryptor(),CryptoStreamMode.Read);
StreamReader reader = new StreamReader(crStream);
string data = reader.ReadToEnd();
reader.Close();
stream.Close();
I am having a hard time trying to combine the two. Can anyone help me out here?
Actually, you should consider using the EncryptedXml class. Rather than encrypting the XML itself, you encrypt the XML contents.
Encryption can entail different approaches for cryptographic strength, key bases, etc. Follow the examples in the MSDN documentation. This is not a short implementation, but it works very well.