URL參數加密專用


//URL參數加密專用

        #region ========加密========
        private static Byte[] key;
        private static Byte[] IV = new Byte[] { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="Text"></param>
        /// <returns></returns>
        public static string UrlEncrypt(string strToEncrypt)
        {
            return UrlEncrypt(strToEncrypt, "#!12^0#@");
        }
        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="strToEncrypt">要加密的字符串</param>
        /// <param name="strEncryptKey">密鑰</param>
        /// <returns>加密后的字符串</returns>
        public static string UrlEncrypt(string strToEncrypt, string strEncryptKey)
        {
            if (!string.IsNullOrEmpty(strToEncrypt))
            {
                try
                {
                    key = Encoding.UTF8.GetBytes(strEncryptKey.Substring(0, 8));
                    Byte[] inputByteArray = Encoding.UTF8.GetBytes(strToEncrypt);
                    DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                    MemoryStream ms = new MemoryStream();
                    CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(key, IV), CryptoStreamMode.Write);
                    cs.Write(inputByteArray, 0, inputByteArray.Length);
                    cs.FlushFinalBlock();
                    return Convert.ToBase64String(ms.ToArray());
                }
                catch (Exception ex)
                {
                    //return ex.Message;
                    return null;
                }
            }
            else
            {
                return null;
            }
        }
        #endregion

        #region ========解密========
        public static string UrlDecrypt(string strToDecrypt)
        {
            return UrlDecrypt(strToDecrypt, "#!12^0#@");
        }
        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="strToDecrypt">要解密的字符串</param>
        /// <param name="strEncryptKey">密鑰,必須與加密的密鑰相同</param>
        /// <returns>解密后的字符串</returns>
        public static string UrlDecrypt(string strToDecrypt, string strEncryptKey)
        {
            if (!string.IsNullOrEmpty(strToDecrypt))
            {
                strToDecrypt = strToDecrypt.Replace(" ", "+");//如果去除此部分的代碼就會出現上面出現所說的情況,出錯或者解密出來的數據變成空值。
                try
                {
                    key = Encoding.UTF8.GetBytes(strEncryptKey.Substring(0, 8));
                    Byte[] inputByteArray = Convert.FromBase64String(strToDecrypt);
                    DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                    MemoryStream ms = new MemoryStream();
                    CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(key, IV), CryptoStreamMode.Write);
                    cs.Write(inputByteArray, 0, inputByteArray.Length);
                    cs.FlushFinalBlock();
                    return Encoding.UTF8.GetString(ms.ToArray());
                }
                catch (Exception ex)
                {
                    //return ex.Message;
                    return null;
                }
            }
            else
            {
                return null;
            }
        }
        #endregion

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM