首页 新闻 搜索 专区 学院

.NET加密URL参数会包含出特殊字符串吗?

0
悬赏园豆:10 [已解决问题] 解决于 2013-07-03 13:58
/// <summary>
        /// 加密字符串
        /// </summary>
        /// <param name="a_strString"></param>
        /// <returns></returns>
        public static string Encrypt3DES(string a_strString)
        {
            TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();
            DES.Key = ASCIIEncoding.ASCII.GetBytes("#s^un2ye31<cn%|aoXpR,+vh");
            DES.Mode = CipherMode.ECB;
            ICryptoTransform DESEncrypt = DES.CreateEncryptor();
            byte[] Buffer = ASCIIEncoding.ASCII.GetBytes(a_strString);
            return Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
        }

如上方法,我想加密URL参数,但怕加密后字符串包含 &,=,?,这些关键字符 ,请问会出现这种情况吗?

 

如果会,请各位大婶们提供个 能正常加密的helper类或方法,谢了

在日码农的主页 在日码农 | 菜鸟二级 | 园豆:292
提问于:2013-07-03 11:16
< >
分享
最佳答案
0

如果怕出现,那加密后再进行UrlEncode

收获园豆:10
dudu | 高人七级 |园豆:37242 | 2013-07-03 11:27
其他回答(1)
0

不会的,你用aes,des 都没问题,base64 会有=/+

 static void Main(string[] args)
        {
            string key = "1678122asdasdasdasdasdasdasdasd345678";
            string encryptRs = AESEncrypt2("www.cnblogs.com", key);
            Console.WriteLine(string.Format("Encrypt Result:{0}", encryptRs));
            string decryptRs = AESDecrypt2(encryptRs, key);
            Console.WriteLine(string.Format("Decrypt Result:{0}", decryptRs));
        }



        /// <summary>
        /// aes  2.0 返回16 进制
        /// </summary>
        /// <param name="toEncrypt"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string AESEncrypt2(string toEncrypt, string key)
        {
            key = key.Substring(0, 32);
            key=key.PadRight(32,'@');
            byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);
            byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);

            RijndaelManaged rDel = new RijndaelManaged();
            rDel.Key = keyArray;
            rDel.Mode = CipherMode.ECB;
            rDel.Padding = PaddingMode.PKCS7;

            ICryptoTransform cTransform = rDel.CreateEncryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

            StringBuilder sb = new StringBuilder();
            foreach (byte b in resultArray)
            {
                sb.AppendFormat("{0:X2}", b);
            }
            // return Convert.ToBase64String(resultArray, 0, resultArray.Length);
            return sb.ToString();
        }

        /// <summary>
        /// aes解密 2.0  由16进制解密
        /// </summary>
        /// <param name="toDecrypt"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string AESDecrypt2(string toDecrypt, string key)
        {
            key = key.Substring(0, 32);
            key = key.PadRight(32, '@');
            byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);
            //  byte[] toEncryptArray = Convert.FromBase64String(toDecrypt);

            int halfInputLength = toDecrypt.Length / 2;
            byte[] toEncryptArray = new byte[halfInputLength];
            for (int x = 0; x < halfInputLength; x++)
            {
                int i = (Convert.ToInt32(toDecrypt.Substring(x * 2, 2), 16));
                toEncryptArray[x] = (byte)i;
            }

            RijndaelManaged rDel = new RijndaelManaged();
            rDel.Key = keyArray;
            rDel.Mode = CipherMode.ECB;
            rDel.Padding = PaddingMode.PKCS7;

            ICryptoTransform cTransform = rDel.CreateDecryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

            return UTF8Encoding.UTF8.GetString(resultArray);
        }
世界万物 | 园豆:276 (菜鸟二级) | 2013-07-03 12:02

这个会有 / 这个符号,在伪静态中有会出现问题。

支持(0) 反对(0) yzy | 园豆:317 (菜鸟二级) | 2015-02-02 13:07
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册