[ Team LiB ]Recipe 5.7 Transmitting a DataSet Securely
Problem
You need to securely send a DataSet over a connection that is not secure.
Solution
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;
{0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F};
private Byte[] rijndaelIV = new Byte[]
{0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F};
// triple DES key and IV
private Byte[] tDESKey = new Byte[]
{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17};
private Byte[] tDESIV = new Byte[]
{0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F,
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37};
// . . .
[Serializable( )]
internal class EncryptedMessage
{
public byte[] Body; // RC2 encrypted
public byte[] Key; // RSA encrypted RC2 key
public byte[] IV; // RC2 initialization vector
} private void encryptButton_Click(object sender, System.EventArgs e)
{
DataSet ds = new DataSet( );
EncryptedMessage em = new EncryptedMessage( );
// RC2 symmetric algorithm to encode the DataSet
RC2CryptoServiceProvider rC2 = new RC2CryptoServiceProvider( );
rC2.KeySize = keySize;
// Generate RC2 Key and IV.
rC2.GenerateKey( );
rC2.GenerateIV( );
// Get the receiver's RSA public key.
RSACryptoServiceProvider rSA = new RSACryptoServiceProvider( );
rSA.ImportParameters(rSAReceiver.ExportParameters(false));
try
{
// Encrypt the RC2 key and IV with the receiver's RSA
// public key.
em.Key = rSA.Encrypt(rC2.Key, false);
em.IV = rSA.Encrypt(rC2.IV, false);
}
catch(CryptographicException ex)
{
MessageBox.Show(ex.Message, "Securing Transmission",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
Cursor.Current = Cursors.WaitCursor;
// Use the CryptoStream to write the encrypted DataSet to the
// MemoryStream.
MemoryStream ms = new MemoryStream( );
if (sfd.ShowDialog( ) == DialogResult.OK)
{
FileStream fsWrite = null;
try
{
fsWrite = new FileStream(sfd.FileName,
FileMode.Create, FileAccess.Write);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message,
"Securing Transmission",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
return;
}
Cursor.Current = Cursors.WaitCursor;
// Symmetric algorithms
byte[] key = null;
byte[] iV = null;
SymmetricAlgorithm sa = null;
if(dESRadioButton.Checked)