public virtual void Throw(string message, string title, string links, string autojump, bool showback) { HttpContext.Current.Response.ContentType = "text/html"; HttpContext.Current.Response.AddHeader("Content-Type", "text/html"); StringBuilder builder = new StringBuilder(this.template); builder.Replace("{$Charset}", Encoding.UTF8.BodyName); builder.Replace("{$Message}", TextUtility.TextEncode(message)); builder.Replace("{$Title}", ((title == null) || (title == "")) ? "发生了系统错误, 错误信息已经被记录, 请联系管理员" : title); if ((links != null) && (links != "")) { string[] strArray = links.Split(new char[] { '|' }); for (int i = 0; i < strArray.Length; i++) { string[] strArray2 = strArray[i].Split(new char[] { ',' }); if (strArray2.Length > 1) { if (strArray2[1].Trim() == "RefHref") { strArray2[1] = RequestHelper.GetUrlReferrer(); } if ((strArray2[1] != string.Empty) && (strArray2[1] != null)) { string str = "<a href='" + strArray2[1] + "'"; if (strArray2.Length == 3) { str = str + " target='" + strArray2[2].Trim() + "'"; } if (strArray2[0].Trim() == "RefText") { strArray2[0] = TextUtility.TextEncode(RequestHelper.GetUrlReferrer()); } str = str + ">" + strArray2[0].Trim() + "</a>\r\n\t\t\t\t"; builder.Replace("{$Links}", str + "{$Links}"); } } } } if ((autojump != null) && (autojump != string.Empty)) { builder.Replace("{$AutoJump}", "<meta http-equiv='refresh' content='3; url=" + ((autojump == "back") ? "javascript:history.back()" : autojump) + "' />"); } else { builder.Replace("{$AutoJump}", "<!-- no jump -->"); } if (showback) { builder.Replace("{$Links}", "<a href='javascript:history.back()'>返回前一页</a>"); } else { builder.Replace("{$Links}", "<!-- no back -->"); } this.Echo(builder.ToString()); this.End(); }
/// <summary> /// 加密 /// </summary> /// <param name="plainText">待加密的文本</param> /// <param name="cipherkey">密钥</param> /// <returns>返回与此实例等效的加密文本</returns> public static string Encrypt(string plainText, string cipherkey) { cipherkey = TextUtility.CutLeft(cipherkey, 32); cipherkey = cipherkey.PadRight(32, ' '); ICryptoTransform transform = new RijndaelManaged { Key = Encoding.UTF8.GetBytes(cipherkey.Substring(0, 32)), IV = Keys }.CreateEncryptor(); byte[] bytes = Encoding.UTF8.GetBytes(plainText); return(Convert.ToBase64String(transform.TransformFinalBlock(bytes, 0, bytes.Length))); }
/// <summary> /// 加密 /// </summary> /// <param name="plainText">待加密的字节</param> /// <param name="cipherkey">密钥</param> /// <returns>返回与此实例等效的加密字节</returns> public static byte[] EncryptBuffer(byte[] plainText, string cipherkey) { cipherkey = TextUtility.CutLeft(cipherkey, 0x20); cipherkey = cipherkey.PadRight(0x20, ' '); RijndaelManaged managed = new RijndaelManaged { Key = Encoding.UTF8.GetBytes(cipherkey.Substring(0, 0x20)), IV = Keys }; return(managed.CreateEncryptor().TransformFinalBlock(plainText, 0, plainText.Length)); }
/// <summary> /// 适用于网页路径的获取和进程网页路径的获取 /// </summary> /// <param name="strPath"></param> /// <returns></returns> public static string GetPhysicsRealPath(string strPath) { if (string.IsNullOrEmpty(strPath)) { throw new Exception("strPath 不能为空!"); } if (HttpContext.Current != null) { return(HttpContext.Current.Server.MapPath(strPath)); } return(TextUtility.GetFullPath(strPath)); }
/// <summary> /// 获取一个目录的物理路径(适用于网页中获取) /// </summary> /// <param name="folderPath">目录路径</param> /// <returns></returns> public static string GetPhysicsPathByWebpage(string folderPath) { if (folderPath.IndexOf(@":\") > 0) { return(TextUtility.AddLast(folderPath, @"\")); } if (folderPath.StartsWith("~/")) { return(TextUtility.AddLast(HttpContext.Current.Server.MapPath(folderPath), @"\")); } string str2 = HttpContext.Current.Request.ApplicationPath + "/"; return(TextUtility.AddLast(HttpContext.Current.Server.MapPath(str2 + folderPath), @"\")); }
/// <summary> /// DES加密 /// </summary> /// <param name="encryptString">待加密的字符串</param> /// <param name="encryptKey">加密密钥,要求为8位</param> /// <returns>加密成功返回加密后的字符串,失败返回源串</returns> public static string Encrypt(string encryptString, string encryptKey) { encryptKey = TextUtility.CutLeft(encryptKey, 8); encryptKey = encryptKey.PadRight(8, ' '); byte[] bytes = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8)); byte[] keys = Keys; byte[] buffer = Encoding.UTF8.GetBytes(encryptString); DESCryptoServiceProvider provider = new DESCryptoServiceProvider(); MemoryStream stream = new MemoryStream(); CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(bytes, keys), CryptoStreamMode.Write); stream2.Write(buffer, 0, buffer.Length); stream2.FlushFinalBlock(); return(Convert.ToBase64String(stream.ToArray())); }
/// <summary> /// 写日志文件 /// </summary> /// <param name="logContent">日志内容</param> /// <param name="fileName">日志文件名</param> /// <returns>返回值true成功,false失败</returns> public static bool Write(string logContent, string fileName) { bool flag = true; if (string.IsNullOrEmpty(fileName)) { fileName = "AppError"; } try { string str = Path.Combine(APP_LOG_DIRECTORY, DateTime.Now.ToString("yyyyMMdd") + fileName + LOG_SUFFIX); FileInfo info = new FileInfo(str); if (info.Exists && (info.Length >= 0xc3500L)) { info.CopyTo(str.Replace(LOG_SUFFIX, TextUtility.CreateRandomNum(5) + LOG_SUFFIX)); File.Delete(str); } FileStream stream = new FileStream(str, FileMode.Append, FileAccess.Write, FileShare.Read); StreamWriter writer = new StreamWriter(stream, Encoding.UTF8); StringBuilder builder = new StringBuilder(); builder.AppendFormat("{0:yyyy'/'MM'/'dd' 'HH':'mm':'ss}", DateTime.Now); builder.Append("|"); if (WRITE_APP_LOG) { builder.Append(logContent.Replace("\r", "").Replace("\n", "<br />")); builder.Append("|"); builder.Append(RequestHelper.GetIP()); builder.Append("|"); builder.Append(RequestHelper.GetIP()); } else { builder.Append(logContent); } builder.Append("\r\n"); writer.Write(builder.ToString()); writer.Flush(); writer.Close(); writer.Dispose(); stream.Close(); stream.Dispose(); } catch { flag = false; } return(flag); }
/// <summary> /// 解密 /// </summary> /// <param name="cipherText">待解密的字节</param> /// <param name="cipherkey">密钥</param> /// <returns>返回与此实例等效的解密字节</returns> public static byte[] DecryptBuffer(byte[] cipherText, string cipherkey) { try { cipherkey = TextUtility.CutLeft(cipherkey, 32); cipherkey = cipherkey.PadRight(32, ' '); RijndaelManaged managed = new RijndaelManaged { Key = Encoding.UTF8.GetBytes(cipherkey), IV = Keys }; return(managed.CreateDecryptor().TransformFinalBlock(cipherText, 0, cipherText.Length)); } catch { return(null); } }
/// <summary> /// 解密 /// </summary> /// <param name="cipherText">待解密的文本</param> /// <param name="cipherkey">密钥</param> /// <returns>返回与此实例等效的解密文本</returns> public static string Decrypt(string cipherText, string cipherkey) { try { cipherkey = TextUtility.CutLeft(cipherkey, 32); cipherkey = cipherkey.PadRight(32, ' '); ICryptoTransform transform = new RijndaelManaged { Key = Encoding.UTF8.GetBytes(cipherkey), IV = Keys }.CreateDecryptor(); byte[] inputBuffer = Convert.FromBase64String(cipherText); byte[] bytes = transform.TransformFinalBlock(inputBuffer, 0, inputBuffer.Length); return(Encoding.UTF8.GetString(bytes)); } catch { return(""); } }
/// <summary> /// DES解密 /// </summary> /// <param name="decryptString">待解密的字符串</param> /// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param> /// <returns>解密成功返回解密后的字符串,失败返源串</returns> public static string Decrypt(string decryptString, string decryptKey) { try { decryptKey = TextUtility.CutLeft(decryptKey, 8); decryptKey = decryptKey.PadRight(8, ' '); byte[] bytes = Encoding.UTF8.GetBytes(decryptKey); byte[] keys = Keys; byte[] buffer = Convert.FromBase64String(decryptString); DESCryptoServiceProvider provider = new DESCryptoServiceProvider(); MemoryStream stream = new MemoryStream(); CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(bytes, keys), CryptoStreamMode.Write); stream2.Write(buffer, 0, buffer.Length); stream2.FlushFinalBlock(); return(Encoding.UTF8.GetString(stream.ToArray())); } catch { return(""); } }
/// <summary> /// 查找非 UTF8 编码的文件 /// </summary> /// <param name="directory"></param> /// <returns></returns> public static string[] SearchUTF8File(string directory) { StringBuilder builder = new StringBuilder(); FileInfo[] files = new DirectoryInfo(directory).GetFiles(); for (int i = 0; i < files.Length; i++) { if (files[i].Extension.ToLower().Equals(".htm")) { FileStream sbInputStream = new FileStream(files[i].FullName, FileMode.Open, FileAccess.Read); bool flag = IsUTF8(sbInputStream); sbInputStream.Close(); if (!flag) { builder.Append(files[i].FullName); builder.Append("\r\n"); } } } return(TextUtility.SplitStrArray(builder.ToString(), "\r\n")); }