• C# RSA加密和解密 签名和验签示例


    在C#中使用RSA进行加密和解密通常涉及到使用RSACryptoServiceProvider类。以下是一个简单的示例代码,演示如何使用C#进行RSA加密和解密:

    using System;
    using System.Security.Cryptography;
    using System.Text;
    
    class Program
    {
        static void Main()
        {
            try
            {
                // 创建RSA加密解密服务提供程序
                using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
                {
                    // 获取公钥和私钥
                    string publicKey = rsa.ToXmlString(false); // 获取公钥
                    string privateKey = rsa.ToXmlString(true); // 获取私钥
    
                    // 显示公钥和私钥(通常私钥需要保密)
                    Console.WriteLine("Public Key:");
                    Console.WriteLine(publicKey);
                    Console.WriteLine("Private Key:");
                    Console.WriteLine(privateKey);
    
                    // 要加密的数据
                    string originalData = "Hello, RSA Encryption and Decryption!";
    
                    // 使用公钥进行加密
                    byte[] encryptedData = Encrypt(publicKey, originalData);
                    Console.WriteLine("Encrypted Data:");
                    Console.WriteLine(Convert.ToBase64String(encryptedData));
    
                    // 使用私钥进行解密
                    string decryptedData = Decrypt(privateKey, encryptedData);
                    Console.WriteLine("Decrypted Data:");
                    Console.WriteLine(decryptedData);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"An error occurred: {ex.Message}");
            }
        }
    
        // 使用公钥加密数据
        static byte[] Encrypt(string publicKey, string data)
        {
            using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
            {
                rsa.FromXmlString(publicKey);
                byte[] dataBytes = Encoding.UTF8.GetBytes(data);
                byte[] encryptedData = rsa.Encrypt(dataBytes, false);
                return encryptedData;
            }
        }
    
        // 使用私钥解密数据
        static string Decrypt(string privateKey, byte[] encryptedData)
        {
            using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
            {
                rsa.FromXmlString(privateKey);
                byte[] decryptedDataBytes = rsa.Decrypt(encryptedData, false);
                string decryptedData = Encoding.UTF8.GetString(decryptedDataBytes);
                return decryptedData;
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67

    在这个示例中,我们创建了一个RSACryptoServiceProvider,生成了公钥和私钥,并使用公钥加密数据,然后使用私钥解密数据。请注意,RSA加密和解密使用不同的密钥,因此需要分别获取公钥和私钥。此示例将数据转换为字节数组以进行加密和解密,然后将结果显示在控制台上。

    请注意,这只是一个简单的示例,实际应用中可能需要更复杂的密钥管理和数据保护机制。此外,为了提高安全性,通常建议使用更长的RSA密钥。

    以下是一个使用C#实现RSA签名和验签的例子:

    using System;
    using System.Security.Cryptography;
    using System.Text;
    
    class Program
    {
        static void Main()
        {
            // 创建RSA实例
            using (RSA rsa = RSA.Create())
            {
                // 加载私钥
                rsa.FromXmlString("<私钥>");
    
                // 要签名的数据
                string data = "要签名的数据";
    
                // 使用SHA256计算数据的哈希值
                byte[] hash = SHA256.Create().ComputeHash(Encoding.UTF8.GetBytes(data));
    
                // 对哈希值进行RSA签名
                byte[] signature = rsa.SignHash(hash, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
    
                // 将签名转换为Base64字符串输出
                Console.WriteLine("签名结果: " + Convert.ToBase64String(signature));
    
                // 加载公钥
                rsa.FromXmlString("<公钥>");
    
                // 使用SHA256计算要验签的数据的哈希值
                byte[] hashToVerify = SHA256.Create().ComputeHash(Encoding.UTF8.GetBytes(data));
    
                // 使用公钥进行验签
                bool verified = rsa.VerifyHash(hashToVerify, signature, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
    
                if (verified)
                {
                    Console.WriteLine("验签成功");
                }
                else
                {
                    Console.WriteLine("验签失败");
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    注意,你需要将<私钥><公钥>替换为你实际使用的RSA密钥的值。此外,你也可以根据需要修改哈希算法和签名填充方式。

  • 相关阅读:
    【统计DataFrame中每列非空值的个数】
    Matlab绘图
    JavaScript 数组字符串方法
    kafka客户端应用参数详解
    [node.js] node.js下载与安装;nodejs模块化介绍;同步与异步区别try-catch捕捉异常;fs模块;path路径模块;http模块
    MySQL5.5.28版本的安装与配置完整版
    物联网技术助力智慧城市安全建设:构建全方位、智能化的安全防护体系
    .NET 7 RC 2 发布,倒计时一个月发布正式版
    38.JavaScript中异步与回调的基本概念,以及回调地狱现象
    【C++】函数重载 ④ ( 函数指针定义的三种方式 | 直接定义函数指针 | 通过 函数类型 定义 函数指针 | 通过 函数指针类型 定义 函数指针 )
  • 原文地址:https://blog.csdn.net/chenhao0568/article/details/134325879