使用System.Web.Security命名空间对ASP.NET中字符加密
2007-09-28 15:16
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls;
public partial class _5_09 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string s1 = "We are happy!"; Response.Write("字符串"+s1+"经过sha1加密后为:<br>"); string s2 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(s1, "sha1"); Response.Write(s2); Response.Write("<br>"); string s3 = "goodluck"; Response.Write("字符串"+s3+"经过md5加密后为:<br>"); string s4 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(s1, "md5"); Response.Write(s4); Response.Write("<br>");
} } 结果为:
字符串We are happy!经过sha1加密后为: CBAFFA21FEAE5252B4E9041581D3080B7D4AFAF5 字符串goodluck经过md5加密后为: D6C4AD84708D89951BDD8E8C609C0F69
//
using System.Security.Cryptography; using System.Text;
public static string MD5(string source) { byte[] data = Encoding.Unicode.GetBytes(source.ToCharArray()); MD5 md5 = new MD5CryptoServiceProvider(); byte[] result = md5.ComputeHash(data); string sResult = Encoding.Unicode.GetString(result); return sResult; }