ClassLibrary .
. Encrypted
System.Security.Cryptography;
public class TripleDes
{
readonly byte[] _key = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24 };
readonly byte[] _iv = { 8, 7, 6, 5, 4, 3, 2, 1 };
private readonly TripleDESCryptoServiceProvider _mDes = new TripleDESCryptoServiceProvider();
private readonly UTF8Encoding _mUtf8 = new UTF8Encoding();
private readonly byte[] _mKey;
private readonly byte[] _mIv;
public TripleDes()
{
_mKey = _key;
_mIv = _iv;
}
public TripleDes(byte[] key, byte[] iv)
{
_mKey = key;
_mIv = iv;
}
public byte[] Encrypt(byte[] input)
{
return Transform(input, _mDes.CreateEncryptor(_mKey, _mIv));
}
public byte[] Decrypt(byte[] input)
{
return Transform(input, _mDes.CreateDecryptor(_mKey, _mIv));
}
public string Encrypt(string text)
{
byte[] input = _mUtf8.GetBytes(text);
byte[] output = Transform(input, _mDes.CreateEncryptor(_mKey, _mIv));
return Convert.ToBase64String(output);
}
public string Decrypt(string text)
{
byte[] input = Convert.FromBase64String(text);
byte[] output = Transform(input, _mDes.CreateDecryptor(_mKey, _mIv));
return _mUtf8.GetString(output);
}
private static byte[] Transform(byte[] input, ICryptoTransform cryptoTransform)
{
using (MemoryStream memStream = new MemoryStream())
{
using (CryptoStream cryptStream = new CryptoStream(memStream, cryptoTransform, CryptoStreamMode.Write))
{
cryptStream.Write(input, 0, input.Length);
cryptStream.FlushFinalBlock();
memStream.Position = 0;
byte[] result = memStream.ToArray();
memStream.Close();
cryptStream.Close();
return result;
}
}
}
}