.NETは常用16、32ビットMD 5暗号化の2つの方法を生成する

1226 ワード

//MD5        , .NET             。
//          MD5     :
//              DVBBS       MD5  

//⑴:  C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\System.Security.dll
public static string MD5(string Password,int Length)
{

if (Length!=16&&Length!=32) throw new System.ArgumentException("Length    ,   16  32 ");
System.Security.Cryptography.MD5CryptoServiceProvider MD5=new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] b= MD5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(Password));
System.Text.StringBuilder StrB=new System.Text.StringBuilder();
for(int i=0;i<b.Length;i++)
StrB.Append(b[i].ToString("x").PadLeft(2,'0'));

if (Length==16)
       return StrB.ToString(8,16);
else
       return     StrB.ToString();

}


//⑵: ASP。NET       System.Web.Security     FormsAuthentication 

public string md5(string str,int code) 
{ 
if(code==16) //16 MD5  ( 32    9~25  ) 
{ 
return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str,"MD5").ToLower().Substring(8,16) ; 
}  
else//32    
{ 
return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str,"MD5").ToLower(); 
}  
}