All articles(网络文学目录) All Pictures(图片目录) All Softwares(软件目录)

 
用ASP.NET加密口令的程序_[Asp.Net教程]

Writer: 归海一刀 Article type: Programming skills(编程技巧) Time: 2014/1/30 1:57:08 Browse times: 322 Comment times: 0

用ASP.NET加密口令的程序_[Asp.Net教程]


Head photo

Go homepage
Upload pictures
Write articles

用ASP.NET加密口令的程序_[Asp.Net教程]

















给大家提供一个简单实用,但是老套的方法,就是口令加密。在此我们使用ASP.NET技术对口令加密。简单的讲,就是将用户提供的口令加密之后,然后让它和存放于系统中的数据比较,如果相同,则通过验证。
在ASP中,并未提供加密的对象,我们只能使用外部的对象来进行加密。现在好了,在ASP.NET中提供了加密的解决方法。在名字空间
System.Web.Security中包含了类FormsAuthentication,其中有一个方法HashPasswordForStoringInConfigFile。这个方法可以将用户提供
的字符变成乱码,然后存储起来,甚至可以 存储在cookies中。
HashPasswordForStoringInConfigFile方法使用起来很简单,它支持"SHA1"和"MD5"加密算法。
下面的代码简单的演示了关于其用法:
<%@ Page language="c#" %>
<%@ Import Namespace="System.Web.Security" %>







Original Clear Text Password:



ID="Button1" />



Encrypted Password In SHA1:



Encrypted Password In MD5:








正如你所看到的这样简单易用。我们可以把这段加密程序封装在一个函数里便于重复的使用。代码如下:
public string EncryptPassword(string
PasswordString,string PasswordFormat )
{
if (PasswordFormat="SHA1"){

EncryptPassword=FormsAuthortication.HashPasswordForStoringIn
ConfigFile(PasswordString ,"SHA1");
}
elseif (PasswordFormat="MD5")
{
EncryptPassword=FormsAuthortication.HashPasswordForStoringIn
ConfigFile(PasswordString ,"MD5");
}
else
{
EncryptPassword="";
}

我们可以在数据库中添加一个字段,使用insert将加密的口令作为一个string存入数据库中。当用户登陆的时候,就可以将用户输入的口令加密结果和数据库中的正确结果比较,通过这种办法来验证口令的正确性了。






























There are 0 records,
Comment:
Must be registered users to comment(必须是注册用户才能发表评论)

Disclaimer Privacy Policy About us Site Map
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.