• asp.net中密码加密MD5


    MD5普通加密方法(这种方法一般不做为用户密码的加密)

    <%@ Page Language="C#" %> 

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

    <script runat="server"> 
        protected void Page_Load(object sender, EventArgs e) 
        { 
            //获取要加密的字段,并转化为Byte[]数组  
            byte[] data = System.Text.Encoding.Unicode.GetBytes(source.Text.ToCharArray()); 
            //建立加密服务  
            System.Security.Cryptography.MD5 md5 = new  System.Security.Cryptography.MD5CryptoServiceProvider(); 
            //加密Byte[]数组  
            byte[] result = md5.ComputeHash(data); 
            //将加密后的数组转化为字段  
            string sResult = System.Text.Encoding.Unicode.GetString(result); 
            //显示出来  
            pass_1.Text =  sResult.ToString() ; 
        } 
    </script> 

    <html xmlns="http://www.w3.org/1999/xhtml"> 
    <head runat="server"> 
        <title>MD5加密 www.itstudy.cn</title> 
    </head> 
    <body> 
        <form id="form1" runat="server"> 
            MD5普通加密:<asp:label id="pass_1" runat="server"></asp:label> <br /> 
        <asp:textbox ID="source" runat="server" Text="test" AutoPostBack="true" /> 
    (回车)  
        </form> 
    </body> 
    </html> 

    MD5密码加密(这是常用的方法)
       页面:

    <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:TextBox ID="TextBox1" runat="server" TextMode="Password"></asp:TextBox>
            <asp:Button ID="Button1" runat="server" Text="MD5加密" onclick="Button1_Click" />
            <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        </div>
        </form>
    </body>
    </html>

     
      后台代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Computer c = new Computer();
        }
        protected void Button1_Click(object sender, EventArgs e)
        {       
            //作为密码方式加密  
            string EnPswdStr = md5(TextBox1.Text,16);
            //显示出来  
            TextBox2.Text = EnPswdStr;
        }


        public string md5(string str, int code)  //code 16 或 32 
        {
            if (code == 16) //16位MD5加密(取32位加密的9~25字符) 
            {
                return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5").ToLower().Substring(8, 16);
            }

            if (code == 32) //32位加密 
            {
                return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5").ToLower();
            }

            return "00000000000000000000000000000000";
        }
    }

    注:除了采用单一的加密方法,还可以使用多个加密方法,取多个加密后的某几位组成一个新的加密结果。

  • 相关阅读:
    【LibreOJ】#6257. 「CodePlus 2017 12 月赛」可做题2
    【Atcoer】ARC088 E
    【Atcoder】ARC088 D
    【CodeForces】671 D. Roads in Yusland
    【CodeForces】671 B. Robin Hood
    【CodeForces】671 C. Ultimate Weirdness of an Array
    【CodeForces】679 A. Bear and Prime 100
    【CodeForces】679 B. Bear and Tower of Cubes
    【BZOJ】3262: 陌上花开
    【CodeForces】899 F. Letters Removing
  • 原文地址:https://www.cnblogs.com/tianguook/p/1892399.html
Copyright © 2020-2023  润新知