using System;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace Security.Common
{
///
/// Encrypts and Decrypts strings
///
public sealed class EncryptionHelper
{
private TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
private UTF8Encoding utf8 = new UTF8Encoding();
///
/// The embeded encyption Key, default key
///
private byte[] _CryptoKey = { 8, 6, 6, 9, 3, 0, 9, 8, 6, 8, 5, 3,1, 9, 7, 8, 6, 4, 4, 4, 4, 1, 69, 69 };
///
/// The initialization Vector
///
private byte[] _InitVector = { 69, 6, 1, 8, 2, 3, 69, 1 };
///
/// Constructor
///
public EncryptionHelper()
{
//NOOP, do not forget to set your key in the
}
public EncryptionHelper(byte[] CryptoKey)
{
_CryptoKey = CryptoKey;
}
///For use on the web, like a Query string variable
public string UrlEncode(string input)
{
var output = input;
output = output.Replace("=","@");
output = output.Replace("+", "-");
output = output.Replace("/", "_");
return output;
}
///For use on the web, like a Query string variable
public string UrlDecode(string input)
{
var output = input;
output = output.Replace("@", "=");
output = output.Replace("-", "+");
output = output.Replace("_", "/");
return output;
}
///
/// Key to use during encryption and decryption
///
///
///
/// byte[] key Ex. { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24 };
///
///
public byte[] CryptoKey
{
get { return _CryptoKey; }
set { _CryptoKey = value; }
}
///
/// Initialization vector to use during encryption and decryption
///
///
///
/// byte[] iv Example { 8, 7, 6, 5, 4, 3, 2, 1 };
///
///
public byte[] InitializationVector
{
get { return _InitVector; }
set { _InitVector = value; }
}
///
/// Decrypt a string
///
///
/// Decrypted data as string
public string Decrypt(string text)
{
try
{
byte[] input = Convert.FromBase64String(text);
byte[] output = Transform(input, des.CreateDecryptor(this._CryptoKey, this._InitVector));
return utf8.GetString(output);
}
catch (Exception ex)
{
return text;
}
}
///
/// Encrypt a string
///
///
/// Encrypted data as string
public string Encrypt(string text)
{
byte[] input = utf8.GetBytes(text);
byte[] output = Transform(input, des.CreateEncryptor(this._CryptoKey, this._InitVector));
return Convert.ToBase64String(output);
}
///
/// Encrypt or Decrypt bytes.
///
///
/// This is used by the public methods
///
/// Data to be encrypted/decrypted
///
/// des.CreateEncryptor(this.keyValue, this.iVValue)
///
/// Byte data containing result of operation
private byte[] Transform(byte[] input, ICryptoTransform cryptoTransform)
{
// Create the necessary streams
var memory = new MemoryStream();
var stream = new CryptoStream(memory, cryptoTransform, CryptoStreamMode.Write);
// Transform the bytes as requested
stream.Write(input, 0, input.Length);
stream.FlushFinalBlock();
// Read the memory stream and convert it back into byte array
memory.Position = 0;
var result = new byte[memory.Length];
memory.Read(result, 0, result.Length);
// Clean up
memory.Close();
stream.Close();
// Return result
return result;
}
}
}
|