using System;
using System.Security.Cryptography;  
using System.Text;
namespace Maticsoft.Common.DEncrypt
{
	/// <summary>
	/// Encrypt µÄÕªÒªËµÃ÷¡£
    /// Copyright (C) Maticsoft
	/// </summary>
	public class DEncrypt
	{
		/// <summary>
		/// ¹¹Ôì·½·¨
		/// </summary>
		public DEncrypt()  
		{  
		} 

		#region Ê¹ÓÃ È±Ê¡ÃÜÔ¿×Ö·û´® ¼ÓÃÜ/½âÃÜstring

		/// <summary>
		/// Ê¹ÓÃÈ±Ê¡ÃÜÔ¿×Ö·û´®¼ÓÃÜstring
		/// </summary>
		/// <param name="original">Ã÷ÎÄ</param>
		/// <returns>ÃÜÎÄ</returns>
		public static string Encrypt(string original)
		{
			return Encrypt(original,"MATICSOFT");
		}
		/// <summary>
		/// Ê¹ÓÃÈ±Ê¡ÃÜÔ¿×Ö·û´®½âÃÜstring
		/// </summary>
		/// <param name="original">ÃÜÎÄ</param>
		/// <returns>Ã÷ÎÄ</returns>
		public static string Decrypt(string original)
		{
			return Decrypt(original,"MATICSOFT",System.Text.Encoding.Default);
		}

		#endregion

		#region Ê¹ÓÃ ¸ø¶¨ÃÜÔ¿×Ö·û´® ¼ÓÃÜ/½âÃÜstring
		/// <summary>
		/// Ê¹ÓÃ¸ø¶¨ÃÜÔ¿×Ö·û´®¼ÓÃÜstring
		/// </summary>
		/// <param name="original">Ô­Ê¼ÎÄ×Ö</param>
		/// <param name="key">ÃÜÔ¿</param>
		/// <param name="encoding">×Ö·û±àÂë·½°¸</param>
		/// <returns>ÃÜÎÄ</returns>
		public static string Encrypt(string original, string key)  
		{  
			byte[] buff = System.Text.Encoding.Default.GetBytes(original);  
			byte[] kb = System.Text.Encoding.Default.GetBytes(key);
			return Convert.ToBase64String(Encrypt(buff,kb));      
		}
		/// <summary>
		/// Ê¹ÓÃ¸ø¶¨ÃÜÔ¿×Ö·û´®½âÃÜstring
		/// </summary>
		/// <param name="original">ÃÜÎÄ</param>
		/// <param name="key">ÃÜÔ¿</param>
		/// <returns>Ã÷ÎÄ</returns>
		public static string Decrypt(string original, string key)
		{
			return Decrypt(original,key,System.Text.Encoding.Default);
		}

		/// <summary>
		/// Ê¹ÓÃ¸ø¶¨ÃÜÔ¿×Ö·û´®½âÃÜstring,·µ»ØÖ¸¶¨±àÂë·½Ê½Ã÷ÎÄ
		/// </summary>
		/// <param name="encrypted">ÃÜÎÄ</param>
		/// <param name="key">ÃÜÔ¿</param>
		/// <param name="encoding">×Ö·û±àÂë·½°¸</param>
		/// <returns>Ã÷ÎÄ</returns>
		public static string Decrypt(string encrypted, string key,Encoding encoding)  
		{       
			byte[] buff = Convert.FromBase64String(encrypted);  
			byte[] kb = System.Text.Encoding.Default.GetBytes(key);
			return encoding.GetString(Decrypt(buff,kb));      
		}  
		#endregion

		#region Ê¹ÓÃ È±Ê¡ÃÜÔ¿×Ö·û´® ¼ÓÃÜ/½âÃÜ/byte[]
		/// <summary>
		/// Ê¹ÓÃÈ±Ê¡ÃÜÔ¿×Ö·û´®½âÃÜbyte[]
		/// </summary>
		/// <param name="encrypted">ÃÜÎÄ</param>
		/// <param name="key">ÃÜÔ¿</param>
		/// <returns>Ã÷ÎÄ</returns>
		public static byte[] Decrypt(byte[] encrypted)  
		{  
			byte[] key = System.Text.Encoding.Default.GetBytes("MATICSOFT"); 
			return Decrypt(encrypted,key);     
		}
		/// <summary>
		/// Ê¹ÓÃÈ±Ê¡ÃÜÔ¿×Ö·û´®¼ÓÃÜ
		/// </summary>
		/// <param name="original">Ô­Ê¼Êý¾Ý</param>
		/// <param name="key">ÃÜÔ¿</param>
		/// <returns>ÃÜÎÄ</returns>
		public static byte[] Encrypt(byte[] original)  
		{  
			byte[] key = System.Text.Encoding.Default.GetBytes("MATICSOFT"); 
			return Encrypt(original,key);     
		}  
		#endregion

		#region  Ê¹ÓÃ ¸ø¶¨ÃÜÔ¿ ¼ÓÃÜ/½âÃÜ/byte[]

		/// <summary>
		/// Éú³ÉMD5ÕªÒª
		/// </summary>
		/// <param name="original">Êý¾ÝÔ´</param>
		/// <returns>ÕªÒª</returns>
		public static byte[] MakeMD5(byte[] original)
		{
			MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();   
			byte[] keyhash = hashmd5.ComputeHash(original);       
			hashmd5 = null;  
			return keyhash;
		}


		/// <summary>
		/// Ê¹ÓÃ¸ø¶¨ÃÜÔ¿¼ÓÃÜ
		/// </summary>
		/// <param name="original">Ã÷ÎÄ</param>
		/// <param name="key">ÃÜÔ¿</param>
		/// <returns>ÃÜÎÄ</returns>
		public static byte[] Encrypt(byte[] original, byte[] key)  
		{  
			TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();       
			des.Key =  MakeMD5(key);
			des.Mode = CipherMode.ECB;  
     
			return des.CreateEncryptor().TransformFinalBlock(original, 0, original.Length);     
		}  

		/// <summary>
		/// Ê¹ÓÃ¸ø¶¨ÃÜÔ¿½âÃÜÊý¾Ý
		/// </summary>
		/// <param name="encrypted">ÃÜÎÄ</param>
		/// <param name="key">ÃÜÔ¿</param>
		/// <returns>Ã÷ÎÄ</returns>
		public static byte[] Decrypt(byte[] encrypted, byte[] key)  
		{  
			TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();  
			des.Key =  MakeMD5(key);    
			des.Mode = CipherMode.ECB;  

			return des.CreateDecryptor().TransformFinalBlock(encrypted, 0, encrypted.Length);
		}  
  
		#endregion

		

		
	}
}
