using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Cryptography;
using System.Text;
namespace CommonObj.common
{
public sealed class clsCustomEncryptionbk
{
//private static string key = "sfdjf48mdfdf3054";
//private static string key = "sfdjf48mdfdf3054123";
public clsCustomEncryptionbk()
{
}
///
/// Mã hóa dựa trên thuật toán mã hóa 1 chiều MD5,
/// Sau khi mã hóa, sử dụng thuật toán encode Base64 để lưu dưới dạng Text
///
/// Chuỗi cần mã hóa
///
public static string EncryptUseMD5(string plainText)
{
string encrypted = null;
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
byte[] pwdhash = hashmd5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(plainText));
encrypted = Convert.ToBase64String(pwdhash);
return encrypted;
}
///
/// Mã hóa dựa trên thuật toán mã hóa phức hợp, có thể giải mã sử dụng hàm Decrypt,
/// Sau khi mã hóa, sử dụng thuật toán encode Base64 để lưu dưới dạng Text
///
/// Chuỗi cần mã hóa
///
public static string Encrypt(string plainText,string key)
{
string encrypted = null;
try
{
byte[] inputBytes = ASCIIEncoding.ASCII.GetBytes(plainText);
byte[] pwdhash = null;
MD5CryptoServiceProvider hashmd5;
//generate an MD5 hash from the password.
//a hash is a one way encryption meaning once you generate
//the hash, you cant derive the password back from it.
hashmd5 = new MD5CryptoServiceProvider();
pwdhash = hashmd5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(key));
hashmd5 = null;
// Create a new TripleDES service provider
TripleDESCryptoServiceProvider tdesProvider = new TripleDESCryptoServiceProvider();
tdesProvider.Key = pwdhash;
tdesProvider.Mode = CipherMode.ECB;
encrypted = Convert.ToBase64String(
tdesProvider.CreateEncryptor().TransformFinalBlock(inputBytes, 0, inputBytes.Length));
}
catch (Exception e)
{
string str = e.Message;
throw;
}
return encrypted;
}
///
/// Thực hiện giải mã một xâu ký tự
///
/// Chuỗi đã mã hóa
///
public static string Decrypt(string encryptedString,string key)
{
string decyprted = null;
byte[] inputBytes = null;
try
{
inputBytes = Convert.FromBase64String(encryptedString);
byte[] pwdhash = null;
MD5CryptoServiceProvider hashmd5;
//generate an MD5 hash from the password.
//a hash is a one way encryption meaning once you generate
//the hash, you cant derive the password back from it.
hashmd5 = new MD5CryptoServiceProvider();
pwdhash = hashmd5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(key));
hashmd5 = null;
// Create a new TripleDES service provider
TripleDESCryptoServiceProvider tdesProvider = new TripleDESCryptoServiceProvider();
tdesProvider.Key = pwdhash;
tdesProvider.Mode = CipherMode.ECB;
decyprted = ASCIIEncoding.ASCII.GetString(
tdesProvider.CreateDecryptor().TransformFinalBlock(inputBytes, 0, inputBytes.Length));
}
catch
{
decyprted = "";
}
return decyprted;
}
}
}