C# HMAC-SHA256 和RSA-SHA256 的区别

384 阅读1分钟

一 HMAC-SHA256示例:

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

class Program
{
    static void Main()
    {
        // 密钥
        string key = "MySecretKey";
        
        // 要计算 HMAC 的消息
        string message = "Hello, HMAC!";
        
        // 将密钥转换为字节数组
        byte[] keyBytes = Encoding.UTF8.GetBytes(key);
        
        // 创建 HMAC-SHA256 算法的实例
        using (HMACSHA256 hmac = new HMACSHA256(keyBytes))
        {
            // 计算消息的 HMAC 值
            byte[] hashBytes = hmac.ComputeHash(Encoding.UTF8.GetBytes(message));
            
            // 将字节数组转换为十六进制字符串
            string hash = BitConverter.ToString(hashBytes).Replace("-", "").ToLower();
            
            // 输出 HMAC 值
            Console.WriteLine("HMAC-SHA256: " + hash);
        }
    }
}

二 RSA-SHA256示例:

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

class Program
{
    static void Main()
    {
        // 创建 RSA 实例
        using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
        {
            try
            {
                // 导出 RSA 公钥和私钥
                string publicKey = rsa.ToXmlString(false);
                string privateKey = rsa.ToXmlString(true);

                // 要签名的数据
                byte[] data = Encoding.UTF8.GetBytes("Hello, RSA!");

                // 使用私钥进行数字签名
                byte[] signature = rsa.SignData(data, new SHA256CryptoServiceProvider());

                // 输出签名
                Console.WriteLine("RSA-SHA256 Signature: " + Convert.ToBase64String(signature));

                // 使用公钥进行验证
                bool verified = rsa.VerifyData(data, new SHA256CryptoServiceProvider(), signature);

                // 输出验证结果
                Console.WriteLine("Signature Verified: " + verified);
            }
            catch (CryptographicException e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
}

三 SHA-256 和 RSA-SHA256 示例

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

class Program
{
    static void Main()
    {
        // 示例消息
        string message = "Hello, SHA-256 and RSA-SHA256!";
        
        // 使用SHA-256算法计算消息的哈希值
        byte[] hash;
        using (SHA256 sha256 = SHA256.Create())
        {
            hash = sha256.ComputeHash(Encoding.UTF8.GetBytes(message));
        }
        
        // 输出SHA-256哈希值
        Console.WriteLine("SHA-256 Hash: " + BitConverter.ToString(hash).Replace("-", "").ToLower());

        // 创建RSA实例
        using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
        {
            try
            {
                // 导出RSA公钥和私钥
                string publicKey = rsa.ToXmlString(false);
                string privateKey = rsa.ToXmlString(true);

                // 使用私钥进行数字签名
                byte[] signature;
                using (SHA256 sha256 = SHA256.Create())
                {
                    signature = rsa.SignData(hash, sha256);
                }

                // 输出数字签名
                Console.WriteLine("RSA-SHA256 Signature: " + Convert.ToBase64String(signature));

                // 使用公钥进行验证
                bool verified;
                using (SHA256 sha256 = SHA256.Create())
                {
                    verified = rsa.VerifyData(hash, sha256, signature);
                }

                // 输出验证结果
                Console.WriteLine("Signature Verified: " + verified);
            }
            catch (CryptographicException e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
}