64 lines
1.9 KiB
C#
64 lines
1.9 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Security.Cryptography;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace ThirdPartyClient.Method
|
||
{
|
||
public class MD5Util
|
||
{
|
||
/// <summary>
|
||
/// 获取大写的MD5签名结果
|
||
/// </summary>
|
||
/// <param name="encypStr">需要签名的串</param>
|
||
/// <param name="charset">编码</param>
|
||
/// <returns>返回大写的MD5签名结果</returns>
|
||
public static string GetMD5(string encypStr, string charset)
|
||
{
|
||
string retStr;
|
||
MD5CryptoServiceProvider m5 = new MD5CryptoServiceProvider();
|
||
|
||
//创建md5对象
|
||
byte[] inputBye;
|
||
byte[] outputBye;
|
||
|
||
//使用GB2312编码方式把字符串转化为字节数组.
|
||
try
|
||
{
|
||
inputBye = Encoding.GetEncoding(charset).GetBytes(encypStr);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
inputBye = Encoding.GetEncoding("GB2312").GetBytes(encypStr);
|
||
Console.WriteLine(ex);
|
||
}
|
||
outputBye = m5.ComputeHash(inputBye);
|
||
|
||
retStr = System.BitConverter.ToString(outputBye);
|
||
retStr = retStr.Replace("-", "").ToUpper();
|
||
return retStr;
|
||
}
|
||
|
||
#region 方法 -> MD5转译
|
||
/// <summary>
|
||
/// MD5转译
|
||
/// </summary>
|
||
/// <param name="str"></param>
|
||
/// <returns></returns>
|
||
public static string GetMD5(string str)
|
||
{
|
||
MD5 _MD5 = new MD5CryptoServiceProvider();
|
||
byte[] t = _MD5.ComputeHash(Encoding.GetEncoding("utf-8").GetBytes(str));
|
||
StringBuilder sb = new StringBuilder(32);
|
||
for (int i = 0; i < t.Length; i++)
|
||
{
|
||
sb.Append(t[i].ToString("x").PadLeft(2, '0'));
|
||
}
|
||
return sb.ToString();
|
||
}
|
||
#endregion
|
||
}
|
||
}
|