/// <summary> /// 地址,路径编码,排除 / // \ \\等编码 /// </summary> /// <param name="str"></param> /// <param name="encoding">编码类型</param> /// <returns></returns> public static string StrEncodeUrl(string str, string encoding) { if (str == null) { return(""); } char tempStr = ' '; string tempStr2 = ""; string returnStr = ""; char[] ary = str.ToCharArray(); for (int i = 0; i < str.Length; i++) { tempStr = ary[i]; //过滤特别字符 if (!EKGetString.IsExist(":,?,&,/,//,///,////,\\,\\\\,\\/,/\\", ',', tempStr.ToString())) { tempStr2 = EKRequest.StrEncode(tempStr.ToString(), encoding); } else { tempStr2 = tempStr.ToString(); } //新编码字符串 returnStr += tempStr2; } return(returnStr.Replace("+", "%20")); }
/// <summary> /// AES加密字符串 /// </summary> /// <param name="encryptString">待加密的字符串</param> /// <param name="encryptKey">加密密钥,要求为32位</param> /// <returns>加密成功返回加密后的字符串,失败返回源串</returns> public static string EncryptAES(string encryptString, string encryptKey) { encryptKey = EKGetString.SubString(encryptKey, 32, ""); encryptKey = encryptKey.PadRight(32, ' '); RijndaelManaged rijndaelProvider = new RijndaelManaged(); rijndaelProvider.Key = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 32)); rijndaelProvider.IV = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 32)); ICryptoTransform rijndaelEncrypt = rijndaelProvider.CreateEncryptor(); byte[] inputData = Encoding.UTF8.GetBytes(encryptString); byte[] encryptedData = rijndaelEncrypt.TransformFinalBlock(inputData, 0, inputData.Length); return(Convert.ToBase64String(encryptedData)); }
/// <summary> /// 多选控件选中值 /// </summary> /// <param name="cbl">多选控件</param> /// <param name="defaultVal">默认值</param> /// <returns></returns> public static string GetValueCheckBoxList(System.Web.UI.WebControls.CheckBoxList cbl, string defaultVal) { string valStr = ""; for (int i = 0; i < cbl.Items.Count; i++) { if (cbl.Items[i].Value == "" || !cbl.Items[i].Selected) { continue; } valStr += cbl.Items[i].Value + ","; } valStr = EKGetString.RemoveEnd(valStr, ","); if (valStr == "") { return(defaultVal); } return(valStr); }
/// <summary> /// AES解密字符串 /// </summary> /// <param name="decryptString">待解密的字符串</param> /// <param name="decryptKey">解密密钥,要求为32位,和加密密钥相同</param> /// <returns>解密成功返回解密后的字符串,失败返源串</returns> public static string DecryptAES(string decryptString, string decryptKey) { try { decryptKey = EKGetString.SubString(decryptKey, 32, ""); decryptKey = decryptKey.PadRight(32, ' '); RijndaelManaged rijndaelProvider = new RijndaelManaged(); rijndaelProvider.Key = Encoding.UTF8.GetBytes(decryptKey); rijndaelProvider.IV = Encoding.UTF8.GetBytes(decryptKey); ICryptoTransform rijndaelDecrypt = rijndaelProvider.CreateDecryptor(); byte[] inputData = Convert.FromBase64String(decryptString); byte[] decryptedData = rijndaelDecrypt.TransformFinalBlock(inputData, 0, inputData.Length); return(Encoding.UTF8.GetString(decryptedData)); } catch { return(decryptString); } }
/// <summary> /// 字符串中删除字符串。 /// </summary> /// <param name="str1">原始字符串 1,2,3,4,6</param> /// <param name="str2">删除的字符串 2,4</param> /// <param name="split">分格字符串如 ,</param> /// <returns></returns> public static string StringRemoveString(string str1, string str2, string split) { string[] ary = str2.Split(new string[] { split }, StringSplitOptions.None); for (int i = 0; i < ary.Length; i++) { //1000,1001,1002,1003 > 1000,,1002 > 1000,1002 str1 = str1.Replace(ary[i], "").Replace(split + split, split); } //去头多余 if (str1.IndexOf(split) == 0) { str1 = EKGetString.RemoveStart(str1, split); } //去尾多余 if (str1.LastIndexOf(split) + split.Length == str1.Length) { str1 = EKGetString.RemoveEnd(str1, split); } return(str1); }
/// <summary> /// 返回指定IP是否在指定的IP数组所限定的范围内, IP数组内的IP地址可以使用*表示该IP段任意, 例如192.168.1.* /// </summary> /// <param name="ip"></param> /// <param name="iparray"></param> /// <returns></returns> public static bool InIPArray(string ip, string[] iparray) { string[] userip = EKGetString.SplitString(ip, @"."); for (int ipIndex = 0; ipIndex < iparray.Length; ipIndex++) { string[] tmpip = EKGetString.SplitString(iparray[ipIndex], @"."); int r = 0; for (int i = 0; i < tmpip.Length; i++) { if (tmpip[i] == "*") { return(true); } if (userip.Length > i) { if (tmpip[i] == userip[i]) { r++; } else { break; } } else { break; } } if (r == 4) { return(true); } } return(false); }