Symmetric encryption using a shared private key is the most used encryption method, as it is relatively fast (compared with public-key encryption), and relatively easy to implement (Figure 1). The .NET environment provides a number of cryptography classes, including symmetric encryption methods. A good way to interface to these methods is to use a code wrapper, which provides a simple method of accessing these classes [Click here]. It provides encryption algorithms such as DES, 3DES and BlowFish, and also hash algorithms such as MD5 and SHA. The following is a simple example using the 3DES algorithm. The solution and demo of this example is at: [Click here to download of the solution]
using System;
using XCrypt;
// Program uses XCrypt library from http://www.codeproject.com/csharp/xcrypt.asp
namespace encryption
{
class MyEncryption
{
static void Main ( string [] args)
{
XCryptEngine xe = new XCryptEngine();
xe.InitializeEngine(XCryptEngine.AlgorithmType.TripleDES);
// Other algorithms are:
// xe.InitializeEngine(XCryptEngine.AlgorithmType.BlowFish);
// xe.InitializeEngine(XCryptEngine.AlgorithmType.Twofish);
// xe.InitializeEngine(XCryptEngine.AlgorithmType.DES);
// xe.InitializeEngine(XCryptEngine.AlgorithmType.MD5);
// xe.InitializeEngine(XCryptEngine.AlgorithmType.RC2);
// xe.InitializeEngine(XCryptEngine.AlgorithmType.Rijndael);
// xe.InitializeEngine(XCryptEngine.AlgorithmType.SHA);
// xe.InitializeEngine(XCryptEngine.AlgorithmType.SHA256);
// xe.InitializeEngine(XCryptEngine.AlgorithmType.SHA384);
// xe.InitializeEngine(XCryptEngine.AlgorithmType.SHA512);
xe.Key = "MyKey"; // Define the public key
Console.WriteLine("Enter string to encrypt:");
string inText = Console.ReadLine();
string encText = xe.Encrypt(inText);
string decText = xe.Decrypt(encText);
Console.WriteLine("Input: {0}\r\nEncr: {1}\r\nDecr: {2}", inText,encText,decText);
Console.ReadLine();
}
}
}
A sample run shows:
Enter string to encrypt:
test
Input: test
Encr: uVZLHJ3Wr8s=
Decr: test
By changing the method to SHA gives (for Base-64):
Enter string to hash:
test
Input: test
Hash: qUqP5cyxm6YcTAhz05Hph5gvu9M=/~bill/encrypti

Figure 1: Symmetric encryption