非WEBForm的MD5字符串加密
/// <summary>
/// MD5加密字符串
/// </summary>
/// <param name="str">要加密的字串</param>
/// <returns></returns>
public static string encrypt(string str)
{
byte[] bValue;
byte[] bHash;
string result=null;
System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
bValue=System.Text.Encoding.UTF8.GetBytes(str);
bHash = md5.ComputeHash(bValue);
md5.Clear();
for (int i=0; i<bHash.Length; i++)
{
if(bHash[i].ToString("x").Length==1)
{
//如果返回来是07这样的值,0会被省掉,所以强制加了一个0
result += "0"+bHash[i].ToString("x");
}
else
{
result += bHash[i].ToString("x");
}
}
return result.ToUpper();
}
/// MD5加密字符串
/// </summary>
/// <param name="str">要加密的字串</param>
/// <returns></returns>
public static string encrypt(string str)
{
byte[] bValue;
byte[] bHash;
string result=null;
System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
bValue=System.Text.Encoding.UTF8.GetBytes(str);
bHash = md5.ComputeHash(bValue);
md5.Clear();
for (int i=0; i<bHash.Length; i++)
{
if(bHash[i].ToString("x").Length==1)
{
//如果返回来是07这样的值,0会被省掉,所以强制加了一个0
result += "0"+bHash[i].ToString("x");
}
else
{
result += bHash[i].ToString("x");
}
}
return result.ToUpper();
}
posted on 2004-08-04 17:28 HelloSnoopy 阅读(431) 评论(0) 编辑 收藏 举报