using System;
using System.Security ;
using System.Security.Cryptography ;
using System.Diagnostics ;
using System.Web ;
using System.Text ;
namespace Bigeagle.Util
{
///
/// 一个加密类
///
Author: Bigeagle@163.net
///
Date: 2001/09/25
///
History: 2001/10/25 finished
///
///
/// 封装常用的加密算法
///
public class Cryptography
{
///
/// md5加密指定字符串
///
/// 要加密的字符串
///
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) ;
}
///
/// 判断两个加密字符串是否相同
///
///
///
///
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 ;
}
///
/// 转换string到Byte树组
///
/// 要转换的字符串
///
public static Byte[] StringToByteArray(String s)
{
/*
Char[] ca = s.ToCharArray();
Byte[] ba = new Byte[ca.Length];
for(int i=0; i return ba;*/ return Encoding.UTF8.GetBytes(s) ; } /// /// 转换Byte数组到字符串 /// /// Byte数组 /// 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) ; } /// /// 3des加密字符串 /// /// 要加密的字符串 /// 密钥 /// /// 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 /// /// 3des加密字符串 /// /// 要加密的字符串 /// 密钥 /// 编码方式 /// /// 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)); } /// /// 3des解密字符串 /// /// 要解密的字符串 /// 密钥 /// /// /// 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 /// /// 3des解密字符串 /// /// 要解密的字符串 /// 密钥 /// 编码方式 /// /// /// 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 } }

