设为首页收藏本站language 语言切换
查看: 944|回复: 0
收起左侧

用ASP.NET加密口令

[复制链接]
发表于 2010-2-25 10:21:06 | 显示全部楼层 |阅读模式
<p >每当我们要建立数据库驱动的个人化的web站点时,都必须要保护用户的数据。尽管黑客可以盗取个人的口令,然而更严重的问题是有人能够盗走整个数据库,然后立刻就是所有的口令。<p ><ccid_nobr><table width="100%" bgcolor="#f7f3f7" ><tr><td><font color="#000084">原理</font></td></tr></table></ccid_nobr><p >有一个好的做法是不将实际的口令存储在数据库中,而是存储它们加密后的版本。当我们需要对用户进行鉴定时,只是对用户的口令再进行加密,然后将它与系统中的加密口令进行比较即可。<p >在ASP中,我们不得不借助外部对象来加密字符串。而.NET SDK解决了这个问题,它在System.Web.Security 名称空间中的CookieAuthentication类中提供了HashPasswordForStoringInConfigFile方法,这个方法的目的正如它的名字所提示的,就是要加密存储在配置文件甚至cookies中的口令。<p ><ccid_nobr><table width="100%" bgcolor="#f7f3f7" ><tr><td><font color="#000084">例子</font></td></tr></table></ccid_nobr><p >HashPasswordForStoringInConfigFile方法使用起来非常简单,它支持用于加密字符串的“SHA1”和“MD5”散列算法。为了看看“HashPasswordForStoringInConfigFile”方法的威力,让我们创建一个小小的ASP.NET页面,并且将字符串加密成SHA1和MD5格式。下面是这样的一个ASP.NET页面源代码:<p ><ccid_nobr><table width="580" border="1" cellspacing="0" cellpadding="0" bordercolorlight="black" bordercolordark="#ffffff"><tr><td bgcolor="e6e6e6" class="code">&lt;%@ Import Namespace=&quot;System.Web.Security&quot; %&gt;<br/>&lt;html&gt;<br/>&lt;head&gt;<br/>&lt;script language=&quot;VB&quot; runat=server&gt;<br/>' This function encrypts the input string using the SHA1 and MD5<br/>' encryption algorithms<br/>Sub encryptString(Src As Object, E As EventArgs)<br/>SHA1.Text = CookieAuthentication.HashPasswordForStoringInConfigFile(txtPassword.Text, &quot;SHA1&quot;)<br/>MD5.Text = CookieAuthentication.HashPasswordForStoringInConfigFile(txtPassword.Text, &quot;MD5&quot;)<br/>End Sub<br/>&lt;/script&gt;<br/>&lt;/head&gt;<br/>&lt;body&gt;<br/>&lt;form runat=server&gt;<br/>&lt;p&gt;&lt;b&gt;Original Clear Text Password: &lt;/b&gt;&lt;br&gt;<br/>&lt;asp:Textbox id=&quot;txtPassword&quot; runat=server /&gt;<br/>&lt;asp:Button runat=&quot;server&quot; text=&quot;Encrypt String&quot; onClick=&quot;encryptString&quot; /&gt;&lt;/p&gt;<br/>&lt;p&gt;&lt;b&gt;Encrypted Password In SHA1: &lt;/b&gt;<br/>&lt;asp:label id=&quot;SHA1&quot; runat=server /&gt;&lt;/p&gt;<br/>&lt;p&gt;&lt;b&gt;Encrypted Password In MD5: &lt;/b&gt;<br/>&lt;asp:label id=&quot;MD5&quot; runat=server /&gt;&lt;/p&gt;<br/>&lt;/form&gt;<br/>&lt;/body&gt;<br/>&lt;/html&gt;</td></tr></table></ccid_nobr><p ><ccid_nobr>点击这里</ccid_nobr>进行演示。<p >你可以看到,加密口令就是这么简单。我们还可以将这个功能包装在一个函数中,随时可以再利用它:<p ><ccid_nobr><table width="580" border="1" cellspacing="0" cellpadding="0" bordercolorlight="black" bordercolordark="#ffffff"><tr><td bgcolor="e6e6e6" class="code">Function EncryptPassword (PasswordString as String, PasswordFormat as String) as String<br/>If PasswordFormat = &quot;SHA1&quot; then<br/>EncryptPassword = CookieAuthentication.HashPasswordForStoringInConfigFile(PasswordString, &quot;SHA1&quot;)<br/>Elseif PasswordFormat = &quot;MD5&quot; then<br/>EncryptPassword= CookieAuthentication.HashPasswordForStoringInConfigFile(PasswordString, &quot;MD5&quot;)<br/>Else<br/>EncryptPassword = &quot;&quot;<br/>End if<br/>End Function</td></tr></table></ccid_nobr><p ><ccid_nobr><table width="100%" bgcolor="#f7f3f7" ><tr><td><font color="#000084">在数据库应用程序中使用加密方法</font></td></tr></table></ccid_nobr><p >每当你向数据库中增加一个用户记录时,都要使用这个函数来加密口令,并将这个口令作为加密过的字符串插入字符串中。当用户登录你的站点时,用这个函数对用户输入的口令进行加密,然后将它与从数据库中恢复的那个加密口令进行比较。<p >(责任编辑 <ccid_nobr>尤北</ccid_nobr>)                                 <p align="center"></p></p>
您需要登录后才可以回帖 登录 | 论坛注册

本版积分规则

QQ|Archiver|手机版|小黑屋|sitemap|鸿鹄论坛 ( 京ICP备14027439号 )  

GMT+8, 2025-4-10 15:43 , Processed in 0.068751 second(s), 23 queries , Redis On.  

  Powered by Discuz!

  © 2001-2025 HH010.COM

快速回复 返回顶部 返回列表