PDA

Click to See Complete Forum and Search --> : C# - Symmetric Encryption Class


SimonVega
Mar 14th, 2003, 04:28 AM
Here is a class that encapsulates symmetric encryption using the TripleDES algorithm:

The module is written in C#, but as you know, you can interop with it from any .NET aware language. :cool:


using System;
using System.Text;
using System.IO;
using System.Security.Cryptography;
using Encryption;

namespace Encryption {

public class TripleDESEncryption {

public static string EncryptData(string data, out byte[] desKey, out byte[] desIV) {
MemoryStream output = new MemoryStream();
byte[] byteData = new UnicodeEncoding().GetBytes(data);

//Use the TripleDES symmetric encryption algorithm to encrypt our data. Without an IV, the
//same input block of plaintext will encrypt to same output block of ciphertext. IV guarantees
//output of two identical plaintext blocks are different.
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
CryptoStream crypt = new CryptoStream(output, des.CreateEncryptor(), CryptoStreamMode.Write);

//Assign our crypto-generated key and iv values to our output arguments
desKey = des.Key; desIV = des.IV;
crypt.Write(byteData, 0, byteData.Length);

crypt.Close(); output.Close();
return new UnicodeEncoding().GetString(output.ToArray());
}

public static string EncryptData(byte[] desKey, byte[] desIV, string data) {
MemoryStream output = new MemoryStream();
byte[] byteData = new UnicodeEncoding().GetBytes(data);

//Use the TripleDES symmetric encryption algorithm to encrypt our data. Without an IV, the
//same input block of plaintext will encrypt to same output block of ciphertext. IV guarantees
//output of two identical plaintext blocks are different.
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
CryptoStream crypt = new CryptoStream(output, des.CreateEncryptor(desKey, desIV), CryptoStreamMode.Write);
crypt.Write(byteData, 0, byteData.Length);

crypt.Close(); output.Close();
return new UnicodeEncoding().GetString(output.ToArray());
}

public static string DecryptData(string data, byte[] desKey, byte[] desIV) {
MemoryStream output = new MemoryStream();
byte[] byteData = new UnicodeEncoding().GetBytes(data);

//Use the TripleDES symmetric encryption algorithm to decrypt our data. In order for the ciphertext to be
//successfully decrypted, the exact same key and iv must be used when initially encryted.
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
CryptoStream crypt = new CryptoStream(output, des.CreateDecryptor(desKey, desIV), CryptoStreamMode.Write);
crypt.Write(byteData, 0, byteData.Length);

crypt.Close(); output.Close();
return new UnicodeEncoding().GetString(output.ToArray());
}
}
}

class CipherText {

static void Main() {
byte[] key; byte[] iv;
string password = "secret";
string cif = TripleDESEncryption.EncryptData(password, out key, out iv);
Console.WriteLine(TripleDESEncryption.DecryptData(cif, key, iv));

//NOTE: Key and IVector must be 16 bytes each
//byte[] key = UnicodeEncoding.Unicode.GetBytes("cornhle");
//string cif = TripleDESEncryption.EncryptData(key, key, password);
//Console.WriteLine(TripleDESEncryption.DecryptData(cif, key, key));
}
}