当前位置:首页 > 网络编程 > WEB编程 > ASP.net > 技术应用-asp.net加密方法

技术应用-asp.net加密方法

点击次数:27 次 发布日期:2008-11-27 02:05:41 作者:源代码网
源代码网推荐

    本文由chenyangasp版权所有,可以转载,复制,粘贴,并请注明出处,但不得修改!


    无论什么时候只要我们使用数据库开发网站,我们就必须保护用户资料,这非常必要。


    黑客可以盗窃口令,个人隐私遭到严重的破坏。最好的方法就是不储存原始密码,而是加密后再放到数据库中。


    当我们想验证用户时,我们只需将用户输入的口令再次加密与数据库中的记录进行比较即可。


    在asp中我们需要额外的对象加密。


    但在asp.net中  SDK可以通过system.web.security  namespace中的CookieAuthentication类的


    HashPasswordForStoringInConfigFile方法来解决问题。


    这样做的目的是加密配置文件的口令,甚至cookies. HashPasswordForStoringInConfigFile方法非常容易使用,并且,它支持“SHA1”和“MD5”散列算法。


    为了清楚“HashPasswordForStoringInConfigFile”方法,让我们制作一个小的ASP.NET页,把输入串在SHA1和MD5格式


    中译成密码。


    now  go


    <%@  Import  Namespace="System.Web.Security"  %> <html> <head> <script  language="VB"  runat=server> Sub  encryptString(Src  As  Object,  E  As  EventArgs)


    SHA1.Text  =  CookieAuthentication.HashPasswordForStoringInConfigFile(txtPassword.Text,  "SHA1")


    MD5.Text  =  CookieAuthentication.HashPasswordForStoringInConfigFile(txtPassword.Text,  "MD5")


    End  Sub </script> </head> <body>


    <form  runat=server>


    <p><b>Original  Clear  Text  Password:  </b><br> <asp:Textbox  id="txtPassword"  runat=server  /> <asp:Button  runat="server"  text="Encrypt  String"  onClick="encryptString"  /></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>


    怎么样加密一串字符串是很容易的事,为了使它更容易使用我制作了一个函数。下面我就给出函数的源代码。


    Function  EncryptPassword  (PasswordString  as  String,  PasswordFormat  as  String)  as  String
         If  PasswordFormat  =  "SHA1"  then
            EncryptPassword  =  CookieAuthentication.HashPasswordForStoringInConfigFile(PasswordString,  "SHA1")


         Elseif 
             PasswordFormat  =  "MD5"  then
             EncryptPassword=  CookieAuthentication.HashPasswordForStoringInConfigFile(PasswordString,  "MD5")


          Else
         EncryptPassword  =  ""
      End  if
   End  Function



源代码网推荐
源代码网推荐
源代码网推荐
源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华