/// <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类或方法,谢了
如果怕出现,那加密后再进行UrlEncode
不会的,你用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); }
这个会有 / 这个符号,在伪静态中有会出现问题。