Previous Tip  |  Next Tip  |  Index (recent)   |  Design Tips   | [Bill's Home]

239. Symmetric Encryption in .NET

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

Other related .NET articles I've written include:

- Design Tip 298. [.NET] HMAC-SHA1.
- Design Tip 243. [.NET] Base-64 or Hex hash values.
- Design Tip 242. [.NET] Digital Certificates.
- Design Tip 241. [.NET] Public-key Encryption.
- Design Tip 240. [.NET] Diffie-Hellman Method.
- Design Tip 239. [.NET] Symmetric Encryption (Private-key).
- Design Tip 238. [.NET] Obfuscation Part II.
- Design Tip 237. [.NET] Obfuscation Part I
- Design Tip 236. [.NET] Data packet capture (filters: IP, TCP, and so on).
- Design Tip 235. [.NET] Data packet capture.
- Design Tip 234. [.NET] Interface to network adapter.
- Design Tip 232. [.NET] Creating an SSH client.
- Design Tip 231. [.NET] Creating an SNMP client.
- Design Tip 216. [.NET] Client/server communications.
- Design Tip 210. [XML/.NET] XML and .NET.
- Design Tip 207. [.NET] Treeviews for interest.
- Design Tip 206. [.NET/Design] Design, evaluate, design, .....
- Design Tip 205. [.NET] Treeviews.
- Design Tip 203. [.NET] Replacing menus with Treeviews.
- Design Tip 202. [.NET/Flash] .NET and Flash - the perfect pair.