public class TripleDES
{
#region Private Members
// privateKey and initVector default for TripleDES
private byte []privateKey = {};
private byte []initVector = {};
// These members will be used to
perform encryption and decryption.
private ICryptoTransform
encryptor = null;
private ICryptoTransform
decryptor = null;
#endregion
public TripleDES()
{
// Create a Provider for TripleDES
Encryption.
TripleDESCryptoServiceProvider tdesProvider
=
new TripleDESCryptoServiceProvider();
// Initialize the encryptor we
are going to use.
encryptor = tdesProvider.CreateEncryptor(
privateKey,
initVector);
// Initialize the decryptor we
are going to use.
decryptor = tdesProvider.CreateDecryptor(
privateKey,
initVector); }
#region Encryption Functions
/// <summary>
/// Encrypts
a string value generating a base64-encoded string.
/// </summary>
/// <param name="plainText">
/// Plain Text string to be encrypted.
/// </param>
/// <returns>
/// Cipher text formated
as a base64-encoded string.
/// </returns>
public string Encrypt(string
plainText)
{
return
Encrypt(Encoding.UTF8.GetBytes(plainText));
}
/// <summary>
/// Encrypts
a byte array generating a base64-encoded string.
/// </summary>
/// <param name="plainTextBytes">
/// Plain text bytes to be encrypted.
/// </param>
/// <returns>Cipher text formated as a base64-encoded string.
/// </returns>
public string Encrypt(byte[]
plainTextBytes)
{
return
Convert.ToBase64String(
EncryptToBytes(plainTextBytes));
}
/// <summary>
/// Encrypts
a string value generating a byte array of cipher text.
/// </summary>
/// <param name="plainText">Plain
text string to be encrypted.
/// </param>
/// <returns>Cipher text
formatted as a byte array.</returns>
public byte[] EncryptToBytes(string plainText)
{
return EncryptToBytes(Encoding.UTF8.GetBytes(plainText));
}
/// <summary>
/// Encrypts
a byte array generating a byte array of cipher text.
/// </summary>
/// <param name="plainTextBytes">Plain
text bytes to be encrypted.
/// </param>
/// <returns>Cipher text
formatted as a byte array.</returns>
public byte[] EncryptToBytes(byte[] plainTextBytes)
{
// Encryption will be performed using memory stream.
MemoryStream encryptedStream = new MemoryStream();
lock (this)
{
// Init encrypting stream.
CryptoStream cryptStream = new CryptoStream(
encryptedStream,
encryptor,
CryptoStreamMode.Write);
// Start encrypting data.
cryptStream.Write(
plainTextBytes,
0,
plainTextBytes.Length);
// Finish the encryption operation.
cryptStream.FlushFinalBlock();
// Move encrypted data from memory to array.
byte[] result = encryptedStream.ToArray();
// Close memory streams.
encryptedStream.Close();
cryptStream.Close();
// Return encrypted data.
return result;
}
}
#endregion
#region Decryption routines
/// <summary>
/// Decrypts
a base64-encoded cipher text value
/// generating a string result.
/// </summary>
/// <param name="cipherText">
/// Base64-encoded cipher
text string to be decrypted.
/// </param>
/// <returns>
/// Decrypted
string value.
/// </returns>
public string Decrypt(string
cipherText)
{
return
Decrypt(Convert.FromBase64String(cipherText));
}
/// <summary>
/// Decrypts a byte array
containing cipher text value and
/// generates a string result.
/// </summary>
/// <param name="cipherTextBytes">
/// Byte
array containing encrypted data.
/// </param>
/// <returns>
/// Decrypted
string value.
/// </returns>
public string Decrypt(byte[]
cipherTextBytes)
{
return Encoding.UTF8.GetString(
DecryptToBytes(cipherTextBytes));
}
/// <summary>
/// Decrypts a
base64-encoded cipher text value and generates
/// a byte array of plain text data.
/// </summary>
/// <param name="cipherText">
/// Base64-encoded cipher
text string to be decrypted.
/// </param>
/// <returns>
/// Byte
array containing decrypted value.
/// </returns>
public byte[] DecryptToBytes(string cipherText)
{
return DecryptToBytes(Convert.FromBase64String(cipherText));
}
/// <summary>
/// Decrypts a
base64-encoded cipher text value and generates
/// a byte array of plain text data.
/// </summary>
/// <param name="cipherTextBytes">
/// Byte
array containing encrypted data.
/// </param>
/// <returns>
/// Byte
array containing decrypted value.
/// </returns>
public byte[] DecryptToBytes(byte[] cipherTextBytes)
{
byte[] decryptedBytes =
null;
byte[] plainTextBytes =
null;
int decryptedByteCount =
0;
MemoryStream memoryStream = new MemoryStream(cipherTextBytes);
// Since we do not know how big
decrypted value will be, use
// the same size as cipher text. Cipher text is always longer
// than plain text (in block cipher encryption), so we will
// just use the number of decrypted data byte after we know
// how big it is.
decryptedBytes = new byte[cipherTextBytes.Length];
// Thread Safe
lock (this)
{
// Initialize decryption stream
CryptoStream cryptoStream = new CryptoStream(
memoryStream,
decryptor,
CryptoStreamMode.Read);
// Decrypting data and get the
count of plain text.
decryptedByteCount = cryptoStream.Read(
decryptedBytes,
0,
decryptedBytes.Length);
// Release Memory.
cryptoStream.Close();
memoryStream.Close();
}
// Allocate the byte array to hold
the original plain text.
plainTextBytes = new byte[decryptedByteCount];
// Copy original plain text value.
Array.Copy(decryptedBytes, 0, plainTextBytes,
0,
decryptedByteCount);
// Return original plain text value.
return plainTextBytes;
}
#endregion
}