Click here for information on how to use code snippets in VS.NET (2005+)
Literal: ID: strDESBinFileType: System.StringToolTip: The file that is to store the serialized jagged array of both the DES' Key and IV byte arrays.Default: "DES.bin"Literal: ID: strDataSetBinFileType: System.StringToolTip: The file that is to store the encrypted DataSet instance.Default: "DataSet.bin"Literal: ID: strDataSetSchemaFileType: System.StringToolTip: The file that is to store the DataSet instance's schema.Default: "DataSet.xsd"
ID: strDESBinFileType: System.StringToolTip: The file that is to store the serialized jagged array of both the DES' Key and IV byte arrays.Default: "DES.bin"
ID: strDataSetBinFileType: System.StringToolTip: The file that is to store the encrypted DataSet instance.Default: "DataSet.bin"
ID: strDataSetSchemaFileType: System.StringToolTip: The file that is to store the DataSet instance's schema.Default: "DataSet.xsd"
// NOTE: This code snippet is designed for VS.NET 2005 or later. // Special reference(s) for this code include: System.Security.dll. // Special namespace usage includes: // using System.IO, System.Security.Cryptography; // using System.Runtime.Serialization; // using System.Runtime.Serialization.Formatters.Binary; private void EncryptDataSet(DataSet ds) { // Create the DES encryption provider: DES des = new DESCryptoServiceProvider(); // Serialize the DES provider's key and IV to disk for decryption later: using (StreamWriter sw = new StreamWriter("DES.bin")) { BinaryFormatter bf = new BinaryFormatter(); byte[][] stuff = new byte[2][]; stuff[0] = des.Key; stuff[1] = des.IV; bf.Serialize(sw.BaseStream, stuff); } using (FileStream fs = new FileStream("DataSet.bin", FileMode.OpenOrCreate, FileAccess.ReadWrite)) { using (CryptoStream cs = new CryptoStream(fs, des.CreateEncryptor(), CryptoStreamMode.Write)) { // Encrypt the DataSet to the file: using (StreamWriter sw = new StreamWriter(cs)) { sw.Write(ds.GetXml()); } } } // Now write the DataSet schema to disk: ds.WriteXmlSchema($strDataSetSchemaFile$); } private DataSet DecryptDataSet() { DataSet ds = new DataSet(); // Setup the DES encryption provider: DES des = new DESCryptoServiceProvider(); // Deserialize the DES provider's key and IV from disk: using (StreamReader sr = new StreamReader("DES.bin")) { BinaryFormatter bf = new BinaryFormatter(); byte[][] stuff = (byte[][])bf.Deserialize(sr.BaseStream); des.Key = stuff[0]; des.IV = stuff[1]; } // Decrypt the Encrypted DataSet: using (FileStream fs = new FileStream("DataSet.bin", FileMode.Open, FileAccess.Read)) { // Decrypt the DataSet and store it into an instance: using (CryptoStream cs = new CryptoStream(fs, des.CreateDecryptor(), CryptoStreamMode.Read)) { ds.ReadXml(cs); } } // Now read the DataSet schema from disk: ds.ReadXmlSchema($strDataSetSchemaFile$); return ds; }
XML View:
Encrypting and Decrypting DataSets encdecdataset Provides two methods for encrypting and decrypting data from a single instance of a DataSet. Calling EncryptDataSet() will cause three files to be generated: 1) a serialized jagged byte array holding the DES key and vector, 2) the encrypted DataSet instance's data and 3) the DataSet instance's schema. Calling DecryptDataSet() will read the preceding three files: 1) it will deserialize the DES key and vector, 2) decrypt and load into the DataSet instance the data and 3) load into the DataSet instance the schema. Note that these two methods will work with one instance of a DataSet. If you call EncryptDataSet() with several instances, you may encounter problems. Therefore, the preceding methods are designed to deal with one instance of a DataSet. If you require encryption and decryption for several DataSet instances then you should pass the methods unique file names for each one accordingly. Please note that this code is the most basic employed. You should implement try-catch blocks as well as conditional checks for the existence of the various files being read and written to. Also, be aware that if you intend to decrypt the DataSet, you must have present all three of the files discussed above. So, for example, if you plan on encrypting the DataSet on computer A and decrypting it on computer B, make sure the files generated are copied to computer B before decryption occurs (ensuring that the paths to the files are properly set as well). strDESBinFile System.String The file that is to store the serialized jagged array of both the DES' Key and IV byte arrays. "DES.bin" strDataSetBinFile System.String The file that is to store the encrypted DataSet instance. "DataSet.bin" strDataSetSchemaFile System.String The file that is to store the DataSet instance's schema. "DataSet.xsd"