using System; using System.Web; using System.Text; using System.Security.Cryptography; using CallCenter.Utility; namespace CallCenterApi.Common { public class CommonHelper { public static string EncryptAES(string data, string key) { return EncryptHelper.EncryptAES(data, key); } public static string DecryptAES(string data, string key) { return data + "" == "" ? "" : EncryptHelper.DecryptAES(data.Replace(" ", "+"), key); } /// /// MD5函数 /// /// 原始字符串 /// MD5结果 public static string MD5(string str) { byte[] b = Encoding.UTF8.GetBytes(str); b = new MD5CryptoServiceProvider().ComputeHash(b); string ret = ""; for (int i = 0; i < b.Length; i++) ret += b[i].ToString("x").PadLeft(2, '0'); return ret; } public static string escape(string value) { if (value == null) return ""; return Microsoft.JScript.GlobalObject.escape(value); } public static string unescape(string value) { if (value == null) return ""; return Microsoft.JScript.GlobalObject.unescape(value); } public static string toString(object value) { if (value == null) { return ""; } else { return value.ToString(); } } public static int ToInt32(object value) { string str = toString(value); int result = 0; if (int.TryParse(str, out result) == false) { result = 0; } return result; } public static long ToInt64(object value) { string str = toString(value); long result = 0; if (long.TryParse(str, out result) == false) { result = 0; } return result; } /// /// 是否登录 /// /// public static bool IsLogin(string Uid, string PassToken) { if (string.IsNullOrEmpty(Uid) || string.IsNullOrEmpty(PassToken) || Uid == "0" || PassToken == "0") { return false; } if (PassToken.Length <= 37) { return false; } string salt = PassToken.Substring(32); if (MD5(Uid + salt + "0fc7b78cfa1cb49f5d265e261dc70675") == PassToken.Substring(0, 32)) { return true; } else { return false; } } public static bool IsLogin() { var uid = HttpContext.Current.Request["uid"]; var PassToken = HttpContext.Current.Request["PassToken"]; return CommonHelper.IsLogin(uid, PassToken); } public static string getSourceIP() { string SourceIP = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; /* 获得用户 IP 地址 */ if (string.IsNullOrEmpty(SourceIP)) { SourceIP = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; /* 兼容已有程序 */ } if (SourceIP != null) { if (SourceIP.IndexOf(";") != -1) { SourceIP = SourceIP.Split(';')[0]; if (SourceIP.IndexOf(",") != -1) { SourceIP = SourceIP.Split(',')[0]; } } SourceIP = SourceIP.Trim(); } return SourceIP; } /// /// 字符串数组转十进制数组 /// /// 字符串数组 /// 十进制数组 public static decimal[] ToDecimalArray(string[] strArray) { decimal[] decArray = new decimal[strArray.Length]; for (int i = 0; i < strArray.Length; i++) { decArray[i] = Convert.ToDecimal(strArray[i]); } return decArray; } /// /// 全局静态随机数生成器 /// public static Random ranNumber = new Random(); public static string DirtyFilter(string text) { if (text == null) return ""; string dirtyWords = ""; if (HttpContext.Current.Cache["dirtywords"] == null) { string path = HttpContext.Current.Server.MapPath("/Txt/dirtywords.txt"); if (System.IO.File.Exists(path)) dirtyWords = System.IO.File.ReadAllText(path, Encoding.UTF8); HttpContext.Current.Cache.Add("dirtywords", dirtyWords, null, DateTime.Now.AddDays(1), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.High, null); } dirtyWords = HttpContext.Current.Cache["dirtywords"].ToString(); dirtyWords = dirtyWords.Trim('\r', '\n', ' '); if (dirtyWords != "") { text = System.Text.RegularExpressions.Regex.Replace(text, dirtyWords, "*", System.Text.RegularExpressions.RegexOptions.IgnoreCase); } return text; } public static string DirtyFilterBroadCast(string text) { if (text == null) return ""; string dirtyWords = ""; string path = HttpContext.Current.Server.MapPath("/Txt/boradcast.txt"); if (System.IO.File.Exists(path)) dirtyWords = System.IO.File.ReadAllText(path, Encoding.UTF8); if (dirtyWords != "") { text = System.Text.RegularExpressions.Regex.Replace(text, dirtyWords, "**", System.Text.RegularExpressions.RegexOptions.IgnoreCase); } return text; } /// /// GB2312转化为UTF-8 /// /// /// public static byte[] gb2312toutf8(string str) { Encoding utf8 = Encoding.GetEncoding("UTF-8"); Encoding gb2312 = Encoding.GetEncoding("GB2312"); byte[] gb = gb2312.GetBytes(str); gb = Encoding.Convert(gb2312, utf8, gb); //return utf8.GetString(gb); return gb; } /// /// UTF-8转化为GB2312 /// /// /// public static byte[] utf8togb2312(string text) { Encoding utf8 = Encoding.GetEncoding("UTF-8"); Encoding gb2312 = Encoding.GetEncoding("GB2312"); byte[] bs = utf8.GetBytes(text); bs = Encoding.Convert(utf8, gb2312, bs); //return gb2312.GetString(bs); return bs; } } }