Encrypting Data table object

Answered Encrypting Data table object

  • Tuesday, March 13, 2012 3:51 PM
     
     

    Hello All,

    Can any one please help in finding the best method to encrypt and decry-pt a data table object in c# windows forms.  It would be really great, if i can get the code snippet also.

    - I wish, i could use the symmetric method of encryption for this.

    Regards

    Rithesh Krishnan


    RK

All Replies

  • Tuesday, March 13, 2012 5:48 PM
     
     Answered Has Code

    You can use DataTable.ReadXml method to convert datable into xml and then you can encrypt it and save it in file

    public static void EncryptFile(DataTable dataTable, string destinationFilePath)
    {
    	XmlTextWriter writer = new XmlTextWriter(destinationFilePath, Encoding.UTF8);
    	UnicodeEncoding a = new UnicodeEncoding();
    	byte[] key = a.GetBytes(mKey);
    	RijndaelManaged rm = new RijndaelManaged();
    	CryptoStream strm = new CryptoStream(writer.BaseStream, rm.CreateEncryptor(key, key), CryptoStreamMode.Write);
    
    	dataTable.WriteXml(strm);
    	strm.Close();
    }
    
    
    public static DataSet DecryptFile(string sourceFilePath)
    {
    	DataSet dt = new DataSet();
    	FileStream fs = new FileStream(sourceFilePath, FileMode.Open);
    	StreamReader sr = new StreamReader(fs);
    	UnicodeEncoding encoding = new UnicodeEncoding();
    	byte[] key = encoding.GetBytes(mKey);
    	RijndaelManaged rm = new RijndaelManaged();
    	CryptoStream strm = new CryptoStream(fs, rm.CreateDecryptor(key, key), CryptoStreamMode.Read);
    
    	dt.ReadXml(strm);
    	sr.Close();
    	fs.Close();
    	return dt;
    }
    

    You can use following code to Encrypt datable

    DataTable dt = new DataTable("TEST");
    
    dt.Columns.Add("FileName");
    dt.Rows.Add("GAURAV");
    dt.Rows.Add("KHANNA");
    EncryptFile(dt, "C:\\ABC.txt");
    

    And following code to decrypt it

    DataSet ds= DecryptFile("C:\\ABC.txt")
    


    Gaurav Khanna | Microsoft VB.NET MVP

  • Wednesday, March 14, 2012 6:58 AM
     
     

    Hi Gaurav,

    Could you please help about the parameter 'mkey' used in the snippet.

    byte[] key = encoding.GetBytes(mKey);

    - Just also would like to add, i wish to add a secret key encryption for this. How do i implement in the above code.

    Regards

    Rithesh



    RK

  • Wednesday, March 14, 2012 1:35 PM
     
      Has Code

    You can have a secret text in mKey variable in above code. It should be same in encryption and decryption code

    For example,

    mKey = "12345678";


    Gaurav Khanna | Microsoft VB.NET MVP

  • Thursday, March 15, 2012 2:56 PM
    Moderator
     
     Answered
    Hi Rithesh,
    How is it going with Gaurav’s suggestions?
    If you want to decrypt it with Asymmetric Keys, you might read How to: Encrypt XML Elements with Asymmetric Keys.
    If you want to decrypt it with Symmetric Keys, you might read How to: Encrypt XML Elements with Symmetric Keys.
    If you have anything unclear, please feel free to let us know.
    Best Regards,


    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us

  • Wednesday, March 21, 2012 4:01 PM
     
     

    Hello Gaurav, Bob,

    First of all, my apologies for late reply. I couldn't access my net due to some technical issues.

    Gaurav, i implemented the snippet which you have kindly given. Thanks a ton for that.

    Bob, The links which you had given provided me a greater insight to the possibilities in cryptography.  Thanks again.

    Just a doubt, since i am working with the data table objects, just curious whether we can generate MD5 hash code of a data grid view row (having columns of multiple data type).

    Regards

    Rithesh



    RK