本项目提供了一个使用C#语言实现MD5加密算法的具体示例代码。通过该源码,开发者能够了解如何在实际应用中进行数据的安全加密处理。
C# MD5加密 实例源码(加密解密)
以下为使用 C# 进行MD5 加密的示例代码:
```csharp
using System;
using System.Security.Cryptography;
using System.Text;
public class MD5EncryptionExample {
public static void Main() {
string input = Hello, World!;
// 调用加密方法,并打印结果
Console.WriteLine(原始字符串: {0}, input);
Console.WriteLine(MD5 加密后的值: {0}, EncryptStringToHex(input));
}
///
/// 使用 MD5 算法对输入的字符串进行哈希处理。
///
public static string EncryptStringToHex(string plainText) {
using (var provider = MD5.Create()) {
byte[] bytes = Encoding.UTF8.GetBytes(plainText);
var hashValue = provider.ComputeHash(bytes);
// 将字节数组转换为十六进制字符串
StringBuilder hexOutput = new StringBuilder(hashValue.Length * 2);
foreach (byte b in hashValue) {
hexOutput.AppendFormat({0:x2}, b);
}
return hexOutput.ToString();
}
}
}
```
此代码段展示了如何在 C# 中使用 MD5 算法对字符串进行加密,并将结果以十六进制格式输出。