Tuesday, September 2, 2008

Example for Encryption and Decryption with C#.

This example describes how a simple Encryption and Decryption can be written and how those functions can be used from client application.

First we will create a singleton Utility class which will contain Encryption and Decryption functions. For Cryptography operation two values will be required which are “Key” and “IV”. By default I have set those values and also I expose properties for both, so that you can use these values when you will use this class as per your demand.


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

namespace ZAQ.Util.CCryptography
{
public class CCryptography
{
protected string sKey = "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456";
protected string sIV = "ABCDEFGHIJKL";

protected byte[] bKey;
protected byte[] bIV;

private CCryptography()
{
bKey = Convert.FromBase64String(sKey);
bIV = Convert.FromBase64String(sIV);
}

public static CCryptography Instance
{
get
{
return UIUtilityCreator.CreatorInstance;
}
}

public string EncryptString(string Value)
{
UTF8Encoding utf8encoder = new UTF8Encoding();
byte[] inputBytes = utf8encoder.GetBytes(Value);

TripleDESCryptoServiceProvider tdesProvider = new TripleDESCryptoServiceProvider();
ICryptoTransform cryptoTransform = tdesProvider.CreateEncryptor(bKey, bIV);

MemoryStream encryptedStream = new MemoryStream();
CryptoStream cryptStream = new CryptoStream(encryptedStream, cryptoTransform, CryptoStreamMode.Write);
cryptStream.Write(inputBytes, 0, inputBytes.Length);
cryptStream.FlushFinalBlock();
encryptedStream.Position = 0;

Byte[] bResult = new Byte[encryptedStream.ToArray().Length];
encryptedStream.Read(bResult, 0, encryptedStream.ToArray().Length);
cryptStream.Close();
return Convert.ToBase64String(bResult);
}

public string DecryptString(string Value)
{
byte[] inputBytes = Convert.FromBase64String(Value);
TripleDESCryptoServiceProvider tdesProvider = new TripleDESCryptoServiceProvider();
ICryptoTransform cryptoTransform = tdesProvider.CreateDecryptor(bKey, bIV);

MemoryStream decryptedStream = new MemoryStream();
CryptoStream cryptStream = new CryptoStream(decryptedStream, cryptoTransform, CryptoStreamMode.Write);

cryptStream.Write(inputBytes, 0, inputBytes.Length);
cryptStream.FlushFinalBlock();
decryptedStream.Position = 0;

byte[] bResult = new byte[decryptedStream.ToArray().Length];
decryptedStream.Read(bResult, 0, decryptedStream.ToArray().Length);
cryptStream.Close();

UTF8Encoding myutf = new UTF8Encoding();
return myutf.GetString(bResult).ToString();
}

public string Key
{
set
{
sKey = value;
}
}

public string IV
{
set
{
sIV = value;
}
}

private sealed class UIUtilityCreator
{
// Retrieve a single instance of a Singleton
private static readonly CCryptography _instance = new CCryptography();

public static CCryptography CreatorInstance
{
get { return _instance; }
}
}
}
}


Now here is the sample code to use this class.

class Program
{
static void Main(string[] args)
{
CCryptography oCryptography = CCryptography.Instance;

string sEncrypt = "Test";
string sEncrypted ="";
string sDecrypted = "";
sEncrypted = oCryptography.EncryptString(sEncrypt);
sDecrypted = oCryptography.DecryptString(sEncrypted);

Console.WriteLine("Text to be Encrypted: " + sEncrypt + "\nEncrypted Text: " + sEncrypted + "\nDecrypted Text: " + sDecrypted);
Console.ReadLine();
}
}

No comments: