Files
ww-jst/wmsjst/JSMachine.WMS.Infrastructure/Helper/Md5EncryptionHelper.cs
T
2026-09-02 16:31:50 +08:00

62 lines
2.3 KiB
C#

using Org.BouncyCastle.Crypto.Digests;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace JSMachine.WMS.Infrastructure.Helper
{
/// <summary>
/// Md5加密辅助类
/// </summary>
public static class Md5EncryptionHelper
{
/// <summary>
/// 16位MD5加密
/// </summary>
/// <param name="password"></param>
/// <returns></returns>
public static string MD5Encrypt16(string str)
{
MD5 md5 = MD5.Create();
string t2 = BitConverter.ToString(md5.ComputeHash(Encoding.Default.GetBytes(str)), 4, 8);
t2 = t2.Replace("-", "");
return t2;
}
/// <summary>
/// 32位MD5加密
/// </summary>
/// <param name="password"></param>
/// <param name="lower">是否小写输出</param>
/// <returns></returns>
public static string MD5Encrypt32(string str, bool lower)
{
string cl = str;
string pwd = string.Empty;
MD5 md5 = MD5.Create(); //实例化一个md5对像
// 加密后是一个字节类型的数组,这里要注意编码UTF8/Unicode等的选择 
byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl));
// 通过使用循环,将字节类型的数组转换为字符串,此字符串是常规字符格式化所得
for (int i = 0; i < s.Length; i++)
{
// 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母,如果使用大写(X)则格式后的字符是大写字符
pwd = lower ? pwd + s[i].ToString("x").PadLeft(2, '0') : pwd + s[i].ToString("X").PadLeft(2, '0');
}
return pwd;
}
public static string MD5Encrypt64(string str)
{
string cl = str;
//string pwd = "";
MD5 md5 = MD5.Create(); //实例化一个md5对像
// 加密后是一个字节类型的数组,这里要注意编码UTF8/Unicode等的选择 
byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl));
return Convert.ToBase64String(s);
}
}
}