首页 > 开发 > 综合 > 正文

常用加密字符串的class,转自OSLeague啊,作者:bigeagle

2024-07-21 02:24:31
字体:
来源:转载
供稿:网友
using system;
using system.security ;
using system.security.cryptography ;
using system.diagnostics ;
using system.web ;
using system.text ;

namespace bigeagle.util
{
    /// <summary>
    /// 一个加密类
    /// <br>author:  [email protected]</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


    }
}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表