.NET MD5 加密(备忘)
// CODE1 建立MD5加密主函数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Cryptography;
using System.Text;
namespace Ts.BusinessRules
{
/// <summary>
/// 公共方法类
/// </summary>
public class Utitly
{
/// <summary>
/// 使用MD5加密字符串
/// </summary>
/// <param name="str">待加密的字符</param>
/// <returns></returns>
public static string GetStrUseMD5(string str)
{
if(str.IsNullOrEmpty())
{
return string.Empty;
}
MD5CryptoServiceProvider md5=new MD5CryptoServiceProvider();
byte[] arr = UTF8Encoding.Default.GetBytes(str);
byte[] bytes = md5.ComputeHash(arr);
str = BitConverter.ToString(bytes);
str=str.Replace("-", "");
return str;
}
}
}
// CODE2 建立 string 类型的扩展方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Ts.BusinessRules
{
/// <summary>
/// 扩展方法
/// </summary>
public static class ExtendMethod
{
/// <summary>
/// 判断字符串是否为空或Null
/// </summary>
/// <param name="str">字符串</param>
/// <returns></returns>
/// <remarks>创建人员(日期):★ben★(101029 13:41)</remarks>
public static bool IsNullOrEmpty(this string str)
{
return string.IsNullOrEmpty(str);
}
/// <summary>
/// 得到MD5加密后的字符
/// </summary>
/// <param name="str">待转换字符</param>
/// <returns></returns>
public static string ToMD5(this string str)
{
return Utitly.GetStrUseMD5(str);
}
}
}
// CODE3 字符串加密的使用方法
namespace Ts.BusinessRules
{
/// <summary>
/// 公共方法类
/// </summary>
public class Utitly
{
public string Test(string str)
{
string md5=str.ToMD5();
return md5;
}
}
}