53 lines
1.4 KiB
C#
53 lines
1.4 KiB
C#
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];
|
||
}
|
||
|
||
|
||
|
||
}
|