Click here to Skip to main content
15,891,708 members
Articles / Programming Languages / XML
Article

Encrypt and decrypt DataSets to XML files

Rate me:
Please Sign up or sign in to vote.
2.54/5 (23 votes)
23 Apr 20052 min read 111K   2.6K   57   17
This class lib allows encrypting DataSets to XML files and reading them back. A Win Form app is included as a showcase.

Introduction

XML is an easy, portable way to save DataSets within the .NET Framework. All methods to handle the XML are contained within .NET, so the end user will not need proprietary data handling software or server to read in the data.

I recently needed the ability to save encrypted DataSets to track a student's learning progress. Obviously, the XML format was too accessible to save this sort of information which needed to be kept from tampering, so I needed encryption, which comes standard with .NET. However, I was still facing a few challenges:

  1. I wanted to be able to accomplish all encryption and decryption with just a few calls to a special "crypto" class library.
  2. While decrypting a file in .NET is very easy, I wanted to avoid having temporary unencrypted files written to disk in the process.
  3. The start of every XML file is quite standard, so it can potentially be used to "guess" the encryption key and crack the code.
  4. Finally, I needed a mechanism to recognize encrypted files so the DataSet would not load erroneous data.

I set out to develop a class library (XMLEncryptor) to encrypt and decrypt DataSets using a username and password. The challenges above were handled as follows:

The XMLEncryptor class is instantiated using two strings, a user name and password. The constructor creates the encryption key and initialization vector, as well as a 16-byte signature.

The signature, which is written as a file header without encryption, is used by the XMLEncryptor to identify files as having been encrypted using its methods. Also, the signature serves as a mask for to push the standard XML header to the 17th byte in the file, which should offer some level of protection against "guessing" the XML header (see 3 above).

The XMLEncryptor exposes two public functions, one for reading in the encrypted XML file and one for writing the DataSet back to a file:

  • public DataSet ReadEncryptedXML(string fileName)
  • public void WriteEncryptedXML(DataSet dataset, string encFileName)

The ReadEncryptedXML function will return a DataSet if all goes as intended. The encrypted XML data is decrypted into a MemoryStream, which is subsequently used to load a DataSet which can then be returned to the caller without having intermediary files written to disk. If an error is encountered during reading, decryption etc., the ReadEncryptedXML function returns null.

The WriteEncryptedXML function takes the DataSet and writes it to a file using the same encryption key and IV as was used for the decryption. Once again, no temporary files are involved.

A typical use of the XMLEncryptor would be as follows:

C#
{
   XMLEncryptor XMLenc = new XMLEncryptor("myname", "mypassword");
   // Load encrypted file into DataSet
   DataSet myDataSet = XMLenc.ReadEncryptedXML("myfile.enc");
   // test for successful read
   if (myDataSet == null)
   {
      // Do something
      return;
   }
   // write back the dataset
   XMLEnc.WriteEncryptedXML(myDataSet, "myfile.enc");
}

Limitations of the application are:

  1. The handling of the username and password is left up to the parent application.
  2. Files of more than 2 GB cannot be handled. However, since the process involves in-memory handling of data, practical size limits may be smaller.

That's all!

Enjoy.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 1 Pin
BhagwatiPal1-Apr-11 0:54
BhagwatiPal1-Apr-11 0:54 
GeneralRe: My vote of 1 Pin
d347hm4n18-Nov-11 0:38
d347hm4n18-Nov-11 0:38 
GeneralMore elegant way Pin
super-e-20-Sep-09 23:00
super-e-20-Sep-09 23:00 
I think a much easier and elegant way to handle this is to use the WriteXml and ReadXml methods of the dataset class, passing in as an argument a CryptoStream object (System.Security.Cryptography namespace). CryptoStream derives from Stream class so, the process is straightforward and pratically transparent to the dataset class.

source would look like:

public void Encrypt(DataSet ds, string filename)
{
    outFile = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write);
    encryptStream = new CryptoStream(outFile, encryptor, CryptoStreamMode.Write);
    ds.WriteXml(encryptStream);
    encryptStream.Close();
    outFile.Close();

}

public DataSet Decrypt(string filename)
{
    DataSet result = new DataSet();
    inFile = new FileStream(filename, FileMode.Open, FileAccess.Read);
    decryptStream = new CryptoStream(inFile, decryptor, CryptoStreamMode.Read);
    result.ReadXml(decryptStream);
    decryptStream.Close();
    inFile.Close();
    return result;
}



encrypt and decryptor objects are defined as:
ICryptoTransform encryptor;
ICryptoTransform decryptor;

and can be obtained in different ways, depending on the programmer's choices. I suggest the fully managed RijndaelManaged.CreateEncryptor() and RijndaelManaged.CreateDecryptor() methods for symmetric encryption.

Hope this helps someone.
Generalbug fix Pin
Ronchen7777-Jul-09 4:28
Ronchen7777-Jul-09 4:28 
GeneralMade one change in XmlEncryptor Pin
shedao21-Oct-08 10:08
shedao21-Oct-08 10:08 
NewsTwo other related encryption articles in CodeProject ... Pin
Tony Selke27-Sep-07 6:49
Tony Selke27-Sep-07 6:49 
GeneralCannot decrypt in VB.Net Pin
marcusts28-Dec-06 19:24
marcusts28-Dec-06 19:24 
AnswerRe: Cannot decrypt in VB.Net Pin
marcusts29-Dec-06 8:19
marcusts29-Dec-06 8:19 
QuestionData Type in the Decrypted File Pin
Luis Mora16-Dec-06 14:27
Luis Mora16-Dec-06 14:27 
AnswerRe: Data Type in the Decrypted File Pin
Koenraad8-Jan-07 11:38
Koenraad8-Jan-07 11:38 
GeneralVB.NET and the null result Pin
Luis Mora16-Dec-06 13:09
Luis Mora16-Dec-06 13:09 
GeneralRe: VB.NET and the null result Pin
Koenraad8-Jan-07 11:34
Koenraad8-Jan-07 11:34 
Generalbetter ways Pin
ediazc3-Aug-05 8:14
ediazc3-Aug-05 8:14 
Generalencryption explanation Pin
Ashley van Gerven24-Apr-05 21:28
Ashley van Gerven24-Apr-05 21:28 
GeneralRe: encryption explanation Pin
Koenraad25-Apr-05 1:02
Koenraad25-Apr-05 1:02 
GeneralDataset Encryption Pin
sides_dale24-Apr-05 14:53
sides_dale24-Apr-05 14:53 
GeneralRe: Dataset Encryption Pin
Koenraad25-Apr-05 1:07
Koenraad25-Apr-05 1:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.