Click here to Skip to main content
6,595,854 members and growing! (18,512 online)
Email Password   helpLost your password?
Desktop Development » Grid & Data Controls » DataSets, DataGrids etc     Intermediate

Encrypt and decrypt DataSets to XML files

By Koenraad

This class lib allows encrypting DataSets to XML files and reading them back. A Win Form app is included as a showcase.
C#, XML, Windows, .NET 1.1, ADO.NET, VS.NET2003, DBA, Dev
Posted:16 Apr 2005
Updated:23 Apr 2005
Views:49,147
Bookmarked:39 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
19 votes for this article.
Popularity: 3.07 Rating: 2.40 out of 5
9 votes, 47.4%
1
2 votes, 10.5%
2
1 vote, 5.3%
3
3 votes, 15.8%
4
4 votes, 21.1%
5

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:

{
   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

About the Author

Koenraad


Member

Location: Canada Canada

Other popular Grid & Data Controls articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 15 of 15 (Total in Forum: 15) (Refresh)FirstPrevNext
GeneralMore elegant way Pinmembersuper-e-0:00 21 Sep '09  
Generalbug fix PinmemberRonchen7775:28 7 Jul '09  
GeneralMade one change in XmlEncryptor Pinmembershedao11:08 21 Oct '08  
NewsTwo other related encryption articles in CodeProject ... PinmemberTony Selke7:49 27 Sep '07  
GeneralCannot decrypt in VB.Net Pinmemberstmarcus20:24 28 Dec '06  
AnswerRe: Cannot decrypt in VB.Net Pinmemberstmarcus9:19 29 Dec '06  
QuestionData Type in the Decrypted File PinmemberLuis Mora15:27 16 Dec '06  
AnswerRe: Data Type in the Decrypted File PinmemberKoenraad12:38 8 Jan '07  
GeneralVB.NET and the null result PinmemberLuis Mora14:09 16 Dec '06  
GeneralRe: VB.NET and the null result PinmemberKoenraad12:34 8 Jan '07  
Generalbetter ways Pinmemberediazc9:14 3 Aug '05  
Generalencryption explanation PinmemberAshley van Gerven22:28 24 Apr '05  
GeneralRe: encryption explanation PinsussKoenraad Blot2:02 25 Apr '05  
GeneralDataset Encryption PinsussDale Sides15:53 24 Apr '05  
GeneralRe: Dataset Encryption PinmemberKoenraad2:07 25 Apr '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 23 Apr 2005
Editor: Smitha Vijayan
Copyright 2005 by Koenraad
Everything else Copyright © CodeProject, 1999-2009
Web15 | Advertise on the Code Project