0

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?

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.