C#的MD5加密函数

发布时间:2009年06月01日      浏览次数:874 次
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
namespace MySoft.Inc
{
public static class my_class
{
//////////////////////////////////////////////////////////////////////////////////////////
// MD5加密函数
//////////////////////////////////////////////////////////////////////////////////////////

public static string MD5(string str)
{
//获取str的byte类型数组
byte[] bytestr = Encoding.UTF8.GetBytes(str);
//实例化MD5CryptoServiceProvider
MD5CryptoServiceProvider myMD5 = new MD5CryptoServiceProvider();
// byte类型数组的值转换为 byte类型的MD5值
byte[] byteMD5str = myMD5.ComputeHash(bytestr);
//将byte类型的MD5值转换为字符串
string MD5_str = "";
for (int i = 0; i < byteMD5str.Length; i++)
{
MD5_str += byteMD5str[i].ToString("x").PadLeft(2, '0');
//大写的X,得到的MD5_str是大写字母和数字组成的字符串
//小写的x,得到的MD5_str是小写字母和数字组成的字符串
}
//返回MD5字符串
return MD5_str;
}
}
免责声明:本站相关技术文章信息部分来自网络,目的主要是传播更多信息,如果您认为本站的某些信息侵犯了您的版权,请与我们联系,我们会即时妥善的处理,谢谢合作!