I need some 开发者_C百科help in encrypting and decrypting a DataTable object.
Problem Scenario:
- Encrypting all the content of System.Data.DataTable object.
Required Features:
- To use Triple DES encryption logic
- Should convert all content or all cells for all columns and/or rows
Please assist.
My thoughts would be to convert to XML (DataSet.GetXML) and then do encryption on this XML Data. Have a look at the System.Security.Cryptography Namespace (TripleDES Class). Decrypting and converting your XML back to a DataSet is then trivial.
I am not sure that this is the best way to do this, but, it seems that would just be to iterate through the rows and columns (i.e. just use a double for loop, or foreach) and use this: http://msdn.microsoft.com/en-us/library/system.security.cryptography.tripledes.aspx to do the Triple DES. This code is NOT checked, but it should look like this:
String fin = cell1.ToString();
//Create variables to help with read and write.
byte[] bin = new byte[100]; //This is intermediate storage for the encryption.
long rdlen = 0; //This is the total number of bytes written.
long totlen = fin.Length; //This is the total length of the input file.
int len; //This is the number of bytes to be written at a time.
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
CryptoStream encStream = new CryptoStream(cell1, tdes.CreateEncryptor(tdesKey, tdesIV), CryptoStreamMode.Write);
Console.WriteLine("Encrypting...");
//Read from the input file, then encrypt and write to the output file.
while(rdlen < totlen)
{
len = fin.Read(bin, 0, 100);
encStream.Write(bin, 0, len);
rdlen = rdlen + len;
Console.WriteLine("{0} bytes processed", rdlen);
}
精彩评论