Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

c# md5加密

作者:朕不想活了   发布日期:2025-03-02   浏览:71

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

public class Program
{
    public static void Main()
    {
        string input = "Hello, World!";
        string hash = GetMd5Hash(input);
        Console.WriteLine("MD5 Hash: " + hash);
    }

    public static string GetMd5Hash(string input)
    {
        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"));
            }
            return sb.ToString();
        }
    }
}

解释说明:

  1. 命名空间引用:代码使用了 System, System.TextSystem.Security.Cryptography 命名空间,以支持字符串处理和加密功能。
  2. GetMd5Hash 方法:此方法接收一个字符串作为输入,并返回该字符串的 MD5 哈希值。它首先将输入字符串转换为字节数组,然后使用 MD5 类计算哈希值,最后将哈希值转换为十六进制字符串。
  3. Main 方法:在主程序中,定义了一个测试字符串 "Hello, World!",并调用 GetMd5Hash 方法来获取其 MD5 哈希值,最后将结果打印到控制台。

这段代码展示了如何在 C# 中实现 MD5 加密的基本流程。

上一篇:c# byte数组转string

下一篇:c# 定时器

大家都在看

c# 二进制

c# tcp client

c# type.gettype

c# sqlconnection

.net和c#

c#游戏开发

c#网络编程

c# rectangle

c# if else

c# rtsp

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站