2009年4月25日 星期六

加密的類別

using System;
using System.Security ;
using System.Security.Cryptography ;
using System.Diagnostics ;
using System.Web ;
using System.Text ;

namespace Bigeagle.Util
{
/// <summary>
/// 一個加密類
/// <br>Author: Bigeagle@163.net</br>
/// <br>Date: 2001/09/25</br>
/// <br>History: 2001/10/25 finished</br>
/// </summary>
/// <remarks>
/// 封裝常用的加密算法
/// </remarks>
public class Cryptography
{

/// <summary>
/// md5加密指定字符串
/// </summary>
/// <param name="a_strValue">要加密的字符串</param>
/// <returns>加密後的字符串</returns>
public static string EncryptMD5String(string a_strValue)
{
#if DEBUG
Debug.Assert(a_strValue.Trim() != "" , "空字符串" , "空字符串不需要加密") ;
#endif//DEBUG

//轉換成bytearray
Byte[] hba = ((HashAlgorithm) CryptoConfig.CreateFromName("MD5")).
ComputeHash(StringToByteArray(a_strValue));

return ByteArrayToString(hba) ;
}

/// <summary>
/// 判斷兩個加密字符串是否相同
/// </summary>
/// <param name="a_str1"></param>
/// <param name="a_str2"></param>
/// <returns>如果相同返回真</returns>
public static bool IsSame(string a_str1 , string a_str2)
{
Byte[] b1 = StringToByteArray(a_str1) ;
Byte[] b2 = StringToByteArray(a_str2) ;
if(b1.Length != b2.Length)
{
return false ;
}

for(int i = 0 ; i < b1.Length ; i ++)
{
if(b1[i] != b2[i])
{
return false ;
}
}

return true ;
}

/// <summary>
/// 轉換string到Byte樹組
/// </summary>
/// <param name="s">要轉換的字符串</param>
/// <returns>轉換的Byte數組</returns>
public static Byte[] StringToByteArray(String s)
{
/*
Char[] ca = s.ToCharArray();
Byte[] ba = new Byte[ca.Length];
for(int i=0; i<ba.Length; i++) ba[i] = (Byte)ca[i];
return ba;*/

return Encoding.UTF8.GetBytes(s) ;
}

/// <summary>
/// 轉換Byte數組到字符串
/// </summary>
/// <param name="a_arrByte">Byte數組</param>
/// <returns>字符串</returns>
public static string ByteArrayToString(Byte[] a_arrByte)
{
/*
//char[] ca = new char[a_arrByte.Length] ;
for(int i = 0 ; i < a_arrByte.Length ; i ++)
{
result += (char)a_arrByte[i] ;
}*/

return Encoding.UTF8.GetString(a_arrByte) ;
}


/// <summary>
/// 3des加密字符串
/// </summary>
/// <param name="a_strString">要加密的字符串</param>
/// <param name="a_strKey">密鑰</param>
/// <returns>加密後並經base64編碼的字符串</returns>
/// <remarks>靜態方法,採用默認ascii編碼</remarks>
public static string Encrypt3DES(string a_strString, string a_strKey)
{
TripleDESCryptoServiceProvider DES = new
TripleDESCryptoServiceProvider();
MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();

DES.Key = hashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(a_strKey));
DES.Mode = CipherMode.ECB;

ICryptoTransform DESEncrypt = DES.CreateEncryptor();

byte[] Buffer = ASCIIEncoding.ASCII.GetBytes(a_strString);
return Convert.ToBase64String(DESEncrypt.TransformFinalBlock
(Buffer, 0, Buffer.Length));
}//end method

/// <summary>
/// 3des加密字符串
/// </summary>
/// <param name="a_strString">要加密的字符串</param>
/// <param name="a_strKey">密鑰</param>
/// <param name="encoding">編碼方式</param>
/// <returns>加密後並經base63編碼的字符串</returns>
/// <remarks>重載,指定編碼方式</remarks>
public static string Encrypt3DES(string a_strString, string a_strKey , Encoding encoding)
{
TripleDESCryptoServiceProvider DES = new
TripleDESCryptoServiceProvider();
MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();

DES.Key = hashMD5.ComputeHash(encoding.GetBytes(a_strKey));
DES.Mode = CipherMode.ECB;

ICryptoTransform DESEncrypt = DES.CreateEncryptor();

byte[] Buffer = encoding.GetBytes(a_strString);
return Convert.ToBase64String(DESEncrypt.TransformFinalBlock
(Buffer, 0, Buffer.Length));
}


/// <summary>
/// 3des解密字符串
/// </summary>
/// <param name="a_strString">要解密的字符串</param>
/// <param name="a_strKey">密鑰</param>
/// <returns>解密後的字符串</returns>
/// <exception cref="">密鑰錯誤</exception>
/// <remarks>靜態方法,採用默認ascii編碼</remarks>
public static string Decrypt3DES(string a_strString, string a_strKey)
{
TripleDESCryptoServiceProvider DES = new
TripleDESCryptoServiceProvider();
MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();

DES.Key = hashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(a_strKey));
DES.Mode = CipherMode.ECB;

ICryptoTransform DESDecrypt = DES.CreateDecryptor();

string result = "";
try
{
byte[] Buffer = Convert.FromBase64String(a_strString);
result = ASCIIEncoding.ASCII.GetString(DESDecrypt.TransformFinalBlock
(Buffer, 0, Buffer.Length));
}
catch(Exception e)
{
#if DEBUG
Console.WriteLine("錯誤:{0}" , e) ;
#endif//DEBUG
throw(new Exception("Invalid Key or input string is not a valid base64 string" , e)) ;
}

return result ;
}//end method

/// <summary>
/// 3des解密字符串
/// </summary>
/// <param name="a_strString">要解密的字符串</param>
/// <param name="a_strKey">密鑰</param>
/// <param name="encoding">編碼方式</param>
/// <returns>解密後的字符串</returns>
/// <exception cref="">密鑰錯誤</exception>
/// <remarks>靜態方法,指定編碼方式</remarks>
public static string Decrypt3DES(string a_strString, string a_strKey , Encoding encoding)
{
TripleDESCryptoServiceProvider DES = new
TripleDESCryptoServiceProvider();
MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();

DES.Key = hashMD5.ComputeHash(encoding.GetBytes(a_strKey));
DES.Mode = CipherMode.ECB;

ICryptoTransform DESDecrypt = DES.CreateDecryptor();

string result = "";
try
{
byte[] Buffer = Convert.FromBase64String(a_strString);
result = encoding.GetString(DESDecrypt.TransformFinalBlock
(Buffer, 0, Buffer.Length));
}
catch(Exception e)
{
#if DEBUG
Console.WriteLine("錯誤:{0}" , e) ;
#endif//DEBUG
throw(new Exception("Invalid Key or input string is not a valid base64 string" , e)) ;
}

return result ;
}//end method


}
}

沒有留言:

張貼留言