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

來源:互聯網
上載者:User
加密|字串 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() != "" , "Null 字元串" , "Null 字元串不需要加密") ;
#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


    }
}


相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。