选择显示字体大小

使用asp.net加密口令

     当我们在网站上建立数据库时,保护用户的信息安全是非常必要的。多数用户不愿意让别人知道自己的信息,同时网管也不想因为安全问题而丢失网站的信誉。无论对于谁,安全问题都是非常重要的。
  为了解决这个问题,我给大家提供一个简单实用,但是老套的方法,就是口令加密。在此我们使用asp.net技术对口令加密。简单的讲,就是将用户提供的口令加密之后,然后让它和存放于系统中的数据比较,如果相同,则通过验证。
  在asp中,并未提供加密的对象,我们只能使用外部的对象来进行加密。现在好了,在asp.net中提供了加密的解决方法。在名字空间system.web.security中包含了类formsauthentication,其中有一个方法hashpasswordforstoringinconfigfile。这个方法可以将用户提供的字符变成乱码,然后存储起来,甚至可以 存储在cookies中。
  hashpasswordforstoringinconfigfile方法使用起来很简单,它支持"sha1"和"md5"加密算法。
  下面的代码简单的演示了关于其用法:
  <%@ page language="c#" %>
  <%@ import namespace="system.web.security" %>
  <html>
   <head>
   <script language="c#" runat="server">
   public void encryptstring(object sender, eventargs e)
   {
   sha1.text = formsauthentication.hashpasswordforstoringinconfigfile(txtpassword.text,"sha1");
   md5.text =formsauthentication.hashpasswordforstoringinconfigfile(txtpassword.text, "md5") ;
   }
   </script>
   </head>
   <body>
   <form runat="server" id="form1">
   <p>
   <b>original clear text password: </b>
   <br>
   <asp:textbox id="txtpassword" runat="server" />
   <asp:button runat="server" text="encrypt string" onclick="encryptstring" id="button1" />
   </p>
   <p>
   <b>encrypted password in sha1: </b>
   <asp:label id="sha1" runat="server" />
   </p>
   <p>
   <b>encrypted password in md5: </b>
   <asp:label id="md5" runat="server" />
   </p>
   </form>
   </body>
  </html>
  
  
  
  正如你所看到的这样简单易用。我们可以把这段加密程序封装在一个函数里便于重复的使用。代码如下:
  public string encryptpassword(string passwordstring,string passwordformat )
   {
   if (passwordformat="sha1"){
   encryptpassword=formsauthortication.hashpasswordforstoringinconfigfile(passwordstring ,"sha1");
   }
   elseif (passwordformat="md5")
   { encryptpassword=formsauthortication.hashpasswordforstoringinconfigfile(passwordstring ,"md5");
   }
   else
   {
   encryptpassword="";
   }
  
  我们可以在数据库中添加一个字段,使用insert将加密的口令作为一个string存入数据库中。当用户登陆的时候,就可以将用户输入的口令加密结果和数据库中的正确结果比较,通过这种办法来验证口令的正确性了。在此,我就不往下写了,关于数据库的知识还得读者自己去学习。
    


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons