首页 > 编程语言 >C#实现MD5加密(32位md5加密和16位md5加密)

C#实现MD5加密(32位md5加密和16位md5加密)

时间:2022-10-29 11:35:15浏览次数:42  
标签:加密 string GetMD5 16 md5 MD5


MD5是经典HASH算法,这里用C#实现一边:

using System;
using System.Security.Cryptography;
using System.Text;

namespace ConsoleApp1
{
class Program
{
// 32位md5加密
public static string GetMD5_32(string s, string _input_charset)
{
MD5 md5 = MD5.Create();
byte[] t = md5.ComputeHash(Encoding.GetEncoding(_input_charset).GetBytes(s));
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < t.Length; i++)
{
stringBuilder.Append(t[i].ToString("x").PadLeft(2, '0'));
}
return stringBuilder.ToString();
}

// 16位md5加密
public static string GetMD5_16(string s)
{
MD5 md5 = MD5.Create();
byte[] t = md5.ComputeHash(UTF8Encoding.Default.GetBytes(s));
string t2 = BitConverter.ToString(t, 4, 8);
t2 = t2.Replace("-", "");
return t2;
}
static void Main(string[] args)
{
Console.WriteLine(GetMD5_32("123456", "utf-8"));
Console.WriteLine(GetMD5_16("123456"));
Console.ReadLine();
}
}
}

C#实现MD5加密(32位md5加密和16位md5加密)_开发语言

C#实现MD5加密(32位md5加密和16位md5加密)_c#_02


标签:加密,string,GetMD5,16,md5,MD5
From: https://blog.51cto.com/lilongsy/5805841

相关文章