5.7. Transmitting a DataSet Securely


You need to securely send a DataSet over a connection that is not secure.


Encrypt and decrypt the DataSet using the .NET cryptographic services, and serialize and save the encrypted DataSet to a stream (such as a file or network stream).

The sample code contains two event handlers:

Encrypt Button.Click

The first Button.Click creates a DataSet and encrypts it using the algorithm specified by the user and writes the encrypted DataSet to a file.

Decrypt Button.Click

The second Button.Click decrypts a file containing a DataSet previously encrypted using an algorithm specified by the user and uses the file to recreate the DataSet previously encrypted.

The C# code is shown in Example 5-7.

Example 5-7. File: SecureTransmissionForm.cs

// Namespaces, variables, and constants using System; using System.Configuration; using System.Windows.Forms; using System.Xml; using System.IO; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; using System.Security.Cryptography; using System.Data; using System.Data.SqlClient; // Table name constants private const String ORDERS_TABLE = "Orders"; private const String ORDERDETAILS_TABLE = "OrderDetails"; // Relation name constants private const String ORDERS_ORDERDETAILS_RELATION = "Orders_OrderDetails_Relation"; // Field name constants private const String ORDERID_FIELD = "OrderID"; private RSACryptoServiceProvider rSAReceiver; private const int keySize = 128; // DES key ...

Get ADO.NET Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.