• 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
    

      

  • 相关阅读:
    网络编程基础【day10】:多线程效果演示(二)
    网络编程基础【day10】:进程与线程介绍(一 )
    第四模块:网络编程基础
    第三模块:面向对象(目录)
    网络编程基础【day09】:堡垒机前戏(十一)
    网络编程基础【day09】:socketserver进阶(十)
    网络编程基础【day09】:socketserver使用(九)
    网络编程基础【day09】:socket解决粘包问题之MD5(八)
    网络编程基础【day09】:socket接收大数据(五)
    网络编程基础【day09】:socket实现文件发送(六)
  • 原文地址:https://www.cnblogs.com/sntetwt/p/2744737.html
Copyright © 2020-2023  润新知