在 .NET 中,可以使用 System.Security.Cryptography 命名空间中的 MD5 类来进行 MD5 加密。

示例代码:

string input = "Hello World";
byte[] inputBytes = Encoding.UTF8.GetBytes(input);

MD5 md5 = MD5.Create();
byte[] hashBytes = md5.ComputeHash(inputBytes);

string hash = BitConverter.ToString(hashBytes).Replace("-", "").ToLower();
Console.WriteLine(hash); // 输出:b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9

首先将要加密的字符串转换成字节数组,然后创建 MD5 实例并调用 ComputeHash 方法进行加密,得到一个字节数组,最后将字节数组转换成十六进制字符串即可。

需要注意的是,MD5 加密是不可逆的,即无法通过加密后的结果来还原原始数据。因此,MD5 加密一般用于验证数据的完整性,而不是用于加密敏感数据。

.net MD5加密

原文地址: https://www.cveoy.top/t/topic/tyN 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录