using System;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Security;
public sealed class SecurityHelper
{
/// <summary>
/// DES解密。
/// </summary>
/// <param name="pToDecrypt">要解密的字符串</param>
/// <param name="sKey">8位密钥</param>
/// <returns>已解密的字符串。</returns>
public static string DESDecrypt(stringpToDecrypt, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
//把字符串放入byte数组
int len = 0;
len = pToDecrypt.Length / 2 - 1;
byte[] inputByteArray = newbyte[len + 1];
int x = 0;
int i = 0;
for (x = 0; x <= len; x++)
{
i = Convert.ToInt32(pToDecrypt.Substring(x* 2, 2), 16);
inputByteArray[x] = Convert.ToByte(i);
}
//建立加密对象的密钥和偏移量,此值重要,不能修改
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
CryptoStream cs = newCryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
return Encoding.Default.GetString(ms.ToArray());
}
/// <summary>
/// DES加密函数
/// </summary>
/// <param name="pToEncrypt">要加密的字符串</param>
/// <param name="sKey">8位密钥</param>
/// <returns>加密后的字符串</returns>
public static string DESEncrypt(stringpToEncrypt, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = null;
inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
//建立加密对象的密钥和偏移量
//原文使用ASCIIEncoding.ASCII方法的GetBytes方法
//使得输入密码必须输入英文文本
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
//写二进制数组到加密流
//(把内存流中的内容全部写入)
System.IO.MemoryStream ms = new System.IO.MemoryStream();
CryptoStream cs = newCryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
//写二进制数组到加密流
//(把内存流中的内容全部写入)
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
//建立输出字符串
StringBuilder ret = newStringBuilder();
byte b = 0;
foreach (byteb_loopVariable in ms.ToArray())
{
b = b_loopVariable;
ret.AppendFormat("{0:X2}",b);
}
return ret.ToString();
}
/// <summary>
/// 处理为合法Windows文件名
/// </summary>
/// <param name="strFileName">源文件名字符串</param>
/// <returns>合法的Windows文件名</returns>
public static string FileNameFilter(stringstrFileName)
{
if (!string.IsNullOrEmpty(strFileName))
{
strFileName.Replace("<","");
strFileName.Replace(">","");
strFileName.Replace("\\", "");
strFileName.Replace("/", "");
strFileName.Replace(":", "");
strFileName.Replace("*", "");
strFileName.Replace("?", "");
strFileName.Replace("|", "");
strFileName.Replace("\"","");
return strFileName;
}
else
{
return "";
}
}
/// <summary>
/// HTML及脚本过滤器
/// </summary>
/// <param name="htmlCode">待处理字符串</param>
/// <returns>处理后的字符串</returns>
public static string HtmlFilter(stringhtmlCode)
{
if (!string.IsNullOrEmpty(htmlCode))
{
//过滤脚本
htmlCode = Regex.Replace(htmlCode, "<script[^>]*?>.*?</script>","", RegexOptions.IgnoreCase);
//过滤HTML
htmlCode = Regex.Replace(htmlCode, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
htmlCode = Regex.Replace(htmlCode, "([\\r\\n])[\\s]+", "", RegexOptions.IgnoreCase);
htmlCode = Regex.Replace(htmlCode, "<!--.*?-->", "", RegexOptions.IgnoreCase);
htmlCode = Regex.Replace(htmlCode, "&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
htmlCode.Replace("<", "");
htmlCode.Replace(">", "");
htmlCode.Replace("'", "‘");
htmlCode.Replace("\\r\\n", "");
htmlCode.Replace("" + (char)13 + ""+ "" + (char)10+ "", "");
htmlCode = HttpContext.Current.Server.HtmlEncode(htmlCode).Trim();
return htmlCode;
}
else
{
return "";
}
}
/// <summary>
/// 产生随机密码
/// </summary>
/// <param name="length">密码长度</param>
/// <returns></returns>
public static string GetRandomPassword(intlength)
{
var chars = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()".ToCharArray();
var password = string.Empty;
var random = new Random();
for (var i = 0; i< length; i++)
{
var x = random.Next(1, chars.Length);
if (!password.Contains(chars.GetValue(x).ToString()))
{
password += chars.GetValue(x);
}
else
{
i--;
}
}
return password;
}
}
此类是关于加密的处理类,转载