HashPasswordForStoringInConfigFile中的Md5算法并非常用的Md5算法

本来我也以为System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile中的MD5和常用的一样

一看MSDN的解释,原来是

Given a password and a string identifying the hash type, this routine produces a hash password suitable for storing in a configuration file.

大家不要用HashPasswordForStoringInConfigFile取得的加密保存在数据库中,这个只适合保存在配置文件中,
在需要验证的时候,会自动根据加密方式验证。

<script language="C#" runat="server">
   string qswhMD5(string str)
{
     
/************qiushuiwuhen(2002-9-27)***************/
     
byte[] b=System.Text.Encoding.Default.GetBytes(str);
     b
=new System.Security.Cryptography.MD5CryptoServiceProvider().ComputeHash(b);
     string ret
="";
     
for(int i=0;i<b.Length;i++)
      ret
+=b[i].ToString("x").PadLeft(2,'0');
     
return ret;
   }

   public 
void encryptString(Object sender, EventArgs e) 
   

     myMD5.Text
=qswhMD5(txtClear.Text);
     MD5.Text 
=System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(txtClear.Text, "MD5") ; 
   }
 
   
</script> 
   
<body onload=document.all.txtClear.select();> 
   
<form runat="server"> 
    明文:
<asp:Textbox id="txtClear" runat="server" /> 
    
<asp:Button runat="server" text="Md5摘要" onClick="encryptString" ID="Button1" /> 
    
<br/>通常用的 MD5:
    
<br/><asp:label id="myMD5" runat="server" /> <br/>
    
<br/>HashPasswordForStoringInConfigFile中的 MD5:
    
<br/><asp:label id="MD5" runat="server" /> 
   
</form> 



环境:vs.net2005/sql server2000/xp测试通过
1.MD5 16位加密实例
       
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;

namespace md5
{
    
class Program
    
{
        
static void Main(string[] args)
        
{
            Console.WriteLine(UserMd5(
"8"));
            Console.WriteLine(GetMd5Str(
"8"));
        }

        
/**//// <summary>
        
/// MD5 16位加密
        
/// </summary>
        
/// <param name="ConvertString"></param>
        
/// <returns></returns>

        public static string GetMd5Str(string ConvertString)
        
{
            MD5CryptoServiceProvider md5 
= new MD5CryptoServiceProvider();
            
string t2 = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(ConvertString)), 48);
            t2 
= t2.Replace("-""");
            
return t2;
        }


        
/**//// <summary>
        
/// MD5 32位加密
        
/// </summary>
        
/// <param name="str"></param>
        
/// <returns></returns>

       static  string UserMd5(string str)
        
{
            
string cl = str;
            
string pwd = "";
            MD5 md5 
= MD5.Create();//实例化一个md5对像
            
// 加密后是一个字节类型的数组,这里要注意编码UTF8/Unicode等的选择 
            byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl));
            
// 通过使用循环,将字节类型的数组转换为字符串,此字符串是常规字符格式化所得
            for (int i = 0; i < s.Length; i++)
            
{
                
// 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母,如果使用大写(X)则格式后的字符是大写字符 

                pwd 
= pwd + s[i].ToString("X");
                
            }

            
return pwd;
        }

    }

}

posted on 2007-08-20 09:24  执法长老  阅读(945)  评论(0编辑  收藏  举报

导航