/// 验证传入字符串.
/// </summary>
/// <param name="text">传入的字符</param>
/// <returns></returns>
public bool ValidText(string text)
{
char[] ch = text.ToCharArray();
//是否通过验证的标示
bool isPass = true;
//特殊字符过滤
string str = "\\/\"[]:|<>+=;,?*@'";
for (int i = 0; i < ch.Length; i++)
{
if (str.IndexOf(ch[i]) >= 0)
{
isPass = false;
break;
}
}
//sql字符过滤 :可以改为SQLSERVER的关键字符
string sqlValid = "exec|insert |select |delete |update |count | chr | mid | master |truncate| char |declare|daxia123|<script|/script>";
string[] sql = sqlValid.Split('|');
for (int i = 0; i < sql.Length; i++)
{
if (text == sql[i])
{
isPass = false;
break;
}
}
return isPass;
}
===================================================================================================================
public static string getHtmlEncode2(string source)
{
string target = source;
target = target.Replace("<", "<");
target = target.Replace(">", ">");
target = target.Replace(" ", " ");
target = target.Replace("'", "'");
target = target.Replace("\"", "''");
target = target.Replace("\n", "<br>");
target = target.Replace("<p>", "<p>");
target = target.Replace("</p>", "</p>");
target = target.Replace("―", "-");
return target;
}
public static string getHtmlDecode(string sources)
{
string target = sources;
target = target.Replace("<", "<");
target = target.Replace(">", ">");
target = target.Replace(" ", " ");
target = target.Replace("'", "'");
target = target.Replace("''", "\"");
target = target.Replace("<br>", "\n");
target = target.Replace("<p>", "<p>");
target = target.Replace("</p>", "</p>");
target = target.Replace("“", "“");
target = target.Replace("”", "”");
target = target.Replace("-", "―");
return target;
}
================================================加密字符串=================================================
/// <summary>
/// 默认密钥向量
/// </summary>
private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
/// <summary>
/// URL传输参数加密Key
/// </summary>
public static string QueryStringKey = "wchdybfq";
/// <summary>
/// DES加密字符串
/// </summary>
/// <param name="encryptString">待加密的字符串</param>
/// <param name="encryptKey">加密密钥,要求为8位</param>
/// <returns>加密成功返回加密后的字符串,失败弹出消息返回登录页面</returns>
public static string EncryptDES(string encryptString, string encryptKey)
{
try
{
byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
byte[] rgbIV = Keys;
byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
cStream.Write(inputByteArray, 0, inputByteArray.Length);
cStream.FlushFinalBlock();
return Convert.ToBase64String(mStream.ToArray());
}
catch
{
MessageBox.Show("加密字符串出错!", "Login.aspx");
return encryptString;
}
}