温馨提示×

如何在c#中实现md5加密

c#
小樊
118
2024-06-30 00:44:42
栏目: 编程语言

在C#中实现MD5加密,可以使用System.Security.Cryptography命名空间中的MD5类。以下是一个简单的示例代码:

using System; using System.Security.Cryptography; using System.Text; class Program { static void Main() { string input = "Hello, world!"; using (MD5 md5 = MD5.Create()) { byte[] inputBytes = Encoding.ASCII.GetBytes(input); byte[] hashBytes = md5.ComputeHash(inputBytes); StringBuilder sb = new StringBuilder(); for (int i = 0; i < hashBytes.Length; i++) { sb.Append(hashBytes[i].ToString("x2")); } string hash = sb.ToString(); Console.WriteLine("MD5 hash of '{0}': {1}", input, hash); } } } 

以上代码将字符串"Hello, world!"进行MD5加密,并输出加密后的结果。在实际应用中,可以将加密后的结果存储到数据库中或者用于数据传输验证等场景。

0