• 企业400电话
  • 微网小程序
  • AI电话机器人
  • 电商代运营
  • 全 部 栏 目

    企业400电话 网络优化推广 AI电话机器人 呼叫中心 网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    ASP.NET加密口令的方法实例

    每当我们要建立数据库驱动的个人化的web站点时,都必须要保护用户的数据。尽管黑客可以盗取个人的口令,然而更严重的问题是有人能够盗走整个数据库,然后立刻就是所有的口令。

    原理

    有一个好的做法是不将实际的口令存储在数据库中,而是存储它们加密后的版本。当我们需要对用户进行鉴定时,只是对用户的口令再进行加密,然后将它与系统中的加密口令进行比较即可。

    在ASP中,我们不得不借助外部对象来加密字符串。而.NET SDK解决了这个问题,它在System.Web.Security名称空间中的FormsAuthentication类中提供了HashPasswordForStoringInConfigFile方法,这个方法的目的正如它的名字所提示的,就是要加密存储在Form表单的口令。

    例子

    HashPasswordForStoringInConfigFile方法使用起来非常简单,它支持用于加密字符串的“SHA1”和“MD5”散列算法。为了看看“HashPasswordForStoringInConfigFile”方法的威力,让我们创建一个小小的ASP.NET页面,并且将字符串加密成SHA1和MD5格式。

    下面是这样的一个ASP.NET页面源代码:

    ASPX文件:

    复制代码 代码如下:

    %@ Page language="c#" Codebehind="loginform.aspx.cs" AutoEventWireup="false" Inherits="konson.log.loginform" %>
    !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    HTML>
    HEAD>
    title>loginform/title>
    meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
    meta name="CODE_LANGUAGE" Content="C#">
    meta name="vs_defaultClientScript" content="JavaScript">
    meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    /HEAD>
    body MS_POSITIONING="GridLayout">
    form id="loginform" method="post" runat="server">
    table style="WIDTH: 205px; HEIGHT: 86px">
    tr>
    td style="WIDTH: 78px">登录名/td>
    td>asp:TextBox id="userid" runat="server" Width="101px">/asp:TextBox>/td>
    /tr>
    tr>
    td style="WIDTH: 78px">密码/td>
    td>asp:TextBox id="pwd" runat="server" Width="101px">/asp:TextBox>/td>
    /tr>
    tr>
    td style="WIDTH: 78px">asp:Button id="login" runat="server" Text="登 录">/asp:Button>/td>
    td>asp:Button ID="cancel" Runat="server" Text="取 消">/asp:Button>/td>
    /tr>
    /table>
    /form>
    /body>
    /HTML>

    Code Behind文件:

    复制代码 代码如下:

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Web.Security;

    namespace konson.log
    {
    public class loginform : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.TextBox userid;
    protected System.Web.UI.WebControls.Button login;
    protected System.Web.UI.WebControls.Button cancel;
    protected System.Web.UI.WebControls.TextBox pwd;
    string epwd;
    private void Page_Load(object sender, System.EventArgs e)
    {}
    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    InitializeComponent();
    base.OnInit(e);
    }

    private void InitializeComponent()
    {   
    this.login.Click += new System.EventHandler(this.login_Click);
    this.Load += new System.EventHandler(this.Page_Load);
    }
    #endregion

    private void login_Click(object sender, System.EventArgs e)
    {
    epwd=FormsAuthentication.HashPasswordForStoringInConfigFile(pwd.Text, "SHA1");
    //epwd=FormsAuthentication.HashPasswordForStoringInConfigFile(pwd.Text, "MD5");
    Response.Write(epwd);
    }
    }
    }


    上面的代码中,你只要把加密后的epwd串写时数据库就ok了。加密口令就是这么简单。

    您可能感兴趣的文章:
    • asp.net实现的MD5加密和DES加解密算法类完整示例
    • asp.net TripleDES加密、解密算法
    • asp.net实现md5加密
    • asp.net下常用的加密算法MD5、SHA-1应用代码
    • asp.net 字符串加密解密技术
    • ASP.NET中MD5与SHA1加密的几种方法
    • 在ASP.Net中实现RSA加密的方法
    • ASP.net中md5加密码的方法
    • asp.net下XML的加密和解密实现方法
    • asp.net的加密解密技巧
    • asp.net实现的DES加密解密操作示例
    上一篇:ASP.NET中后台注册js脚本使用的方法对比
    下一篇:在ASP.NET中连接SQL Server的简单方法
  • 相关文章
  • 

    © 2016-2020 巨人网络通讯

    时间:9:00-21:00 (节假日不休)

    地址:江苏信息产业基地11号楼四层

    《增值电信业务经营许可证》 苏B2-20120278

    ASP.NET加密口令的方法实例 ASP.NET,加密,口令,的,方法,