asp/asp.net MD5加密比较

 


asp MD5加密采用动网论坛的加密算法,具体参考动网论坛 inc/md5.asp

asp.net的MD5加密算法:

  public   string  MD5_1( string  str)
    
{
        
return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(this.oldTxt.Text.Trim(), "MD5").ToLower();
    }

    
public   string  MD5_2( string  str)
    
{
        System.Security.Cryptography.MD5CryptoServiceProvider hashmd5;
        hashmd5 
= new System.Security.Cryptography.MD5CryptoServiceProvider();
        
return BitConverter.ToString(hashmd5.ComputeHash(System.Text.Encoding.Default.GetBytes(str))).Replace("-","").ToLower();
    }


asp加密结果:

admin - MD5加密的结果(32位):21232f297a57a5a743894a0e4a801fc3
中国 -  加密后结果(32):cf0832dedf7457bbcbfa00bbd87b300a

asp.net 加密结果:

 (1)加密字符串-admin,加密结果如下:

21232f297a57a5a743894a0e4a801fc3 (MD5_1)
21232f297a57a5a743894a0e4a801fc3 (MD5_2)

(2)加密字符串-中国,加密结果:

c13dceabcb143acd6c9298265d618a9f (MD5_1)
cf0832dedf7457bbcbfa00bbd87b300a (MD5_2)


asp与asp.net MD5加密结果比较总结

(1)在加密英文字符时,aspMD5加密与asp.netMD5加密结果一样。

(2)在加密中文字符或者其它双字节字符时,sp.net加密结果与字符编码有关。

更详细测试:

前台页面

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

<! 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 > MD5测试 </ title >
</ head >
< body >
    
< form  id ="form1"  runat ="server" >
    
< div >
        
< asp:Label  ID ="Label2"  runat ="server"  Text ="原始字符串" ></ asp:Label > &nbsp;
        
< asp:TextBox  ID ="oldTxt"  runat ="server"  Width ="287px" ></ asp:TextBox >< br  />
        
< br  />
        ASP MD5加密结果(动网论坛MD5加密32位)
< br  />
        admin 加密后结果: 
&nbsp;   &nbsp;   &nbsp;&nbsp;   &nbsp; 21232f297a57a5a743894a0e4a801fc3 < br  />
        admin123 加密后结果:
&nbsp;   &nbsp; 0192023a7bbd73250516f069df18b500 < br  />
        admin中国 加密后结果:b15d57a81157056fe4e24d89da136234
< br  />
        中国 加密后结果: 
&nbsp;   &nbsp;   &nbsp;&nbsp;   &nbsp; cf0832dedf7457bbcbfa00bbd87b300a < br  />
        
< br  />
        
< br  />
        ASP.NET MD5加密结果
< br  />
        
&nbsp;&nbsp; < asp:ListBox  ID ="ListBox1"  runat ="server"  Height ="145px"  Width ="306px"   ></ asp:ListBox >< br  />
        
< br  />
        
< asp:Button  ID ="Button1"  runat ="server"  OnClick ="Button1_Click"  Text ="加密1"   />
        
< asp:Button  ID ="Button2"  runat ="server"  OnClick ="Button2_Click"  Text ="加密2"   /> &nbsp; < asp:Button
            
ID ="Button3"  runat ="server"  OnClick ="Button3_Click"  Text ="加密3"   /> &nbsp;
        
< asp:Button  ID ="Button4"  runat ="server"  OnClick ="Button4_Click"  Text ="加密4"   />
        
< asp:Button  ID ="Button5"  runat ="server"  OnClick ="Button5_Click"  Text ="清除结果"   /></ div >
    
</ form >
</ body >
</ html >


后台代码

using  System;
using  System.Data;
using  System.Configuration;
using  System.Collections;
using  System.Web;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Web.UI.HtmlControls;

public   partial   class  md5 : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{

    }

    
public string MD5_1(string str)
    
{
        
return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(this.oldTxt.Text.Trim(), "MD5").ToLower();
    }

    
public string MD5_2(string str)
    
{
        System.Security.Cryptography.MD5CryptoServiceProvider hashmd5;
        hashmd5 
= new System.Security.Cryptography.MD5CryptoServiceProvider();
        
return BitConverter.ToString(hashmd5.ComputeHash(System.Text.Encoding.Default.GetBytes(str))).Replace("-","").ToLower();
    }


    
public string MD5_3(string str)
    
{
        System.Security.Cryptography.MD5CryptoServiceProvider hashmd5;
        hashmd5 
= new System.Security.Cryptography.MD5CryptoServiceProvider();
        
return BitConverter.ToString(hashmd5.ComputeHash(System.Text.Encoding.Unicode.GetBytes(str))).Replace("-""").ToLower();
    }


    
public string MD5_4(string str)
    
{
        System.Security.Cryptography.MD5CryptoServiceProvider hashmd5;
        hashmd5 
= new System.Security.Cryptography.MD5CryptoServiceProvider();
        
return BitConverter.ToString(hashmd5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(str))).Replace("-""").ToLower();
    }
 
    
    
protected void Button1_Click(object sender, EventArgs e)
    
{
       
this.ListBox1.Items.Add(MD5_1(oldTxt.Text.Trim()));
    }




    
protected void Button2_Click(object sender, EventArgs e)
    
{
        
this.ListBox1.Items.Add(MD5_2(this.oldTxt.Text.Trim()));
    }

    
protected void Button3_Click(object sender, EventArgs e)
    
{
        
this.ListBox1.Items.Add(MD5_3(this.oldTxt.Text.Trim()));
    }

    
protected void Button4_Click(object sender, EventArgs e)
    
{
        
this.ListBox1.Items.Add(MD5_4(this.oldTxt.Text.Trim()));
    }

    
protected void Button5_Click(object sender, EventArgs e)
    
{
        
this.ListBox1.Items.Clear();
    }


}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值