日期:2008-07-30  浏览次数:20399 次

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 ++)
            {