Utils.cs
上传用户:xhd1221
上传日期:2022-05-28
资源大小:186k
文件大小:5k
- using System;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Web;
- using System.Security.Cryptography;
- namespace QQWinFarm
- {
- public class Utils
- {
- public static string ConvertUnicodeStringToChinese(string unicodeString)
- {
- if (string.IsNullOrEmpty(unicodeString))
- return string.Empty;
- string outStr = unicodeString;
- Regex re = new Regex("\\u[0123456789abcdef]{4}", RegexOptions.IgnoreCase);
- MatchCollection mc = re.Matches(unicodeString);
- foreach (Match ma in mc)
- {
- outStr = outStr.Replace(ma.Value, ConverUnicodeStringToChar(ma.Value).ToString());
- }
- return outStr;
- }
- private static char ConverUnicodeStringToChar(string str)
- {
- char outStr = Char.MinValue;
- outStr = (char)int.Parse(str.Remove(0, 2), System.Globalization.NumberStyles.HexNumber);
- return outStr;
- }
- /// <summary>
- /// md5加密
- /// </summary>
- /// <param name="input"></param>
- /// <returns></returns>
- public static string getMd5Hash2(string input)
- {
- byte[] buffer = MD5.Create().ComputeHash(Encoding.Default.GetBytes(input));
- StringBuilder builder = new StringBuilder();
- for (int i = 0; i < buffer.Length; i++)
- {
- builder.Append(buffer[i].ToString("x2"));
- }
- return builder.ToString();
- }
- /// <summary>
- /// 三次md5加密
- /// </summary>
- /// <param name="input"></param>
- /// <returns></returns>
- public static string getMd5Hash(string input)
- {
- MD5 md = MD5.Create();
- byte[] buffer = md.ComputeHash(Encoding.Default.GetBytes(input));
- buffer = md.ComputeHash(buffer);
- buffer = md.ComputeHash(buffer);
- StringBuilder builder = new StringBuilder();
- for (int i = 0; i < buffer.Length; i++)
- {
- builder.Append(buffer[i].ToString("x2"));
- }
- return builder.ToString();
- }
- //// <summary>
- /// 去除HTML标记
- /// </summary>
- /// <param name="NoHTML">包括HTML的源码 </param>
- /// <returns>已经去除后的文字</returns>
- public static string NoHTML(string Htmlstring)
- {
- //删除脚本
- Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
- //删除HTML
- Regex regex = new Regex("<.+?>", RegexOptions.IgnoreCase);
- Htmlstring = regex.Replace(Htmlstring, "");
- Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"([rn])[s]+", "", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", """, RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "xa1", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "xa2", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "xa3", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "xa9", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"&#(d+);", "", RegexOptions.IgnoreCase);
- Htmlstring.Replace("<", "");
- Htmlstring.Replace(">", "");
- Htmlstring.Replace("rn", "");
- //Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();
- return Htmlstring;
- }
- /// <summary>
- /// md5加密
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- //public string MD5(String str)
- //{
- // MD5 md5 = new MD5CryptoServiceProvider();
- // byte[] data = System.Text.Encoding.Default.GetBytes(str);
- // byte[] result = md5.ComputeHash(data);
- // String ret = "";
- // for (int i = 0; i < result.Length; i++)
- // ret += result[i].ToString("x").PadLeft(2, '0');
- // return ret;
- //}
- }
- }