wcs_server_kate_suzhou/Tools/EncryptTool/Md5Encrypt.cs

53 lines
1.4 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
namespace EncryptTool;
/*
* 作者:菻蔃
* 注意MD5 加密会被机器容易的破解,请勿用于机密数据
* 版本时间2024年1月21日
*/
/// <summary>
/// MD5 加密
/// </summary>
public static class Md5Encrypt
{
/// <summary>
/// 魔改版加密,用于加密密码,无法破解,本人也无法知道原密码
/// </summary>
/// <param name="password">需要加密的字符串</param>
/// <param name="length">加密后返回的长度</param>
/// <returns></returns>
public static string EncryptPassword(string? password, int length = 255)
{
if (string.IsNullOrEmpty(password)) { return string.Empty; }
var result = string.Empty;
var i = 0;
while (result.Length < length)
{
var bytes = MD5.HashData(Encoding.UTF8.GetBytes(password));
var tempStr = Convert.ToBase64String(bytes);
tempStr = Regex.Replace(tempStr, "\\W", "").ToUpper();
if (i % 2 == 0)
{
result += Regex.Replace(tempStr, "[0-9]", "");
}
else
{
result += Regex.Replace(tempStr, "[A-Z]|[a-z]", "");
}
password = tempStr;
i++;
}
return result[..length];
}
}