C#でSHA256暗号化アルゴリズムを実装する方法は何ですか?

C#でSHA256暗号化アルゴリズムを実装するには、SHA256Managedクラスを使用できます。以下は簡単なサンプルコードです:

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

public class Program
{
    public static string CalculateSHA256(string input)
    {
        using (SHA256 sha256 = SHA256Managed.Create())
        {
            byte[] bytes = Encoding.UTF8.GetBytes(input);
            byte[] hash = sha256.ComputeHash(bytes);

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < hash.Length; i++)
            {
                sb.Append(hash[i].ToString("x2"));
            }

            return sb.ToString();
        }
    }

    public static void Main()
    {
        string input = "Hello, World!";
        string hash = CalculateSHA256(input);

        Console.WriteLine("Input: " + input);
        Console.WriteLine("SHA256 Hash: " + hash);
    }
}

上記の例では、CalculateSHA256メソッドは文字列の入力を受け取り、そのSHA256ハッシュ値を返します。主な関数では、このメソッドを使用してSHA256ハッシュを計算し、結果を出力する方法を示しています。暗号化したい文字列は、input変数の内容に置き換えることができます。

コメントを残す 0

Your email address will not be published. Required fields are marked *


广告
広告は10秒後に閉じます。
bannerAds