• 大小: 2.22MB
    文件类型: .rar
    金币: 2
    下载: 1 次
    发布日期: 2023-11-04
  • 语言: C#
  • 标签: C#  PEM  RSA  加密解密  

资源简介

C#使用pem格式的密钥对文件来做RSA加解密接签名和验证签名,这里的pem文件是openssl命令生成的密钥对文件,其中私钥pem文件需要用openssl命令转换成pkcs8格式的pem文件。如果已有pem文件,也可以通过openssl命令转换成pkscs8格式的。转换命令为:openssl pkcs8 -topk8 -inform PEM -in private_key.pem -outform PEM -nocrypt -out private_pk8.pem 完整的C#代码,vs2008工程,可编译和测试。

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Security;
using System.Security.Cryptography;

namespace RSAPem测试
{
    class Program
    {
        static void Main(string[] args)
        {
            /**RSA加密测试RSA中的密钥对通过SSL工具生成,生成命令如下: 
            * 1 生成RSA私钥: 
            * openssl genrsa -out private_key.pem 1024 
            * 
            *2 得到RSA公钥 
            * openssl rsa -in private_key.pem -pubout -out public_key.pem 
            * 
            * 3 将RSA私钥转换成PKCS8格式 
            * openssl pkcs8 -topk8 -inform PEM -in private_key.pem -outform PEM -nocrypt -out private_pk8.pem 
            * 
            * 打开private_pk8.pem和pub_pk8.pem文件拷贝内容,去掉头和尾,将换行符删除,按照如下的格式写
            * */
            //private_pk8.pem内容,去掉头和尾,将换行符删除
            string privatekey = “MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBALmGxRTvMvpFLeVHEGPaxOc5feo7JHYzp58JxYr2y4WhleRBcaXwDuTU6m/LIyc8TPpegLgpLxdPyz6vuqeCqyyrSbH3TtYbkV54uZApIzT1rJN5F6V6dyriJxHBwzLzTx9n9LDaVuvyCaSEA8Ws6MdJ2Y4WfT6OPcDBLEtvsifJAgMBAAECgYBLym+2xqUnsp02PhrHADdSUp1K1BBoLV/7y40k4P9xvvox/j5EUNZ0yzpeozjgLXUxDuw8z65KMrHQERAKoIn2iZ0RxcDjrCG0ihRrTOljtVJTpf/s7gsKFR5wUzpoWCDQGAZLqhbcRnFZdlJ8T8HkifE/Dnee3jtkndEG3dqmUQJBAPGXP/ZRNppBa9YILH/ammHVXlf690fSNR6g2nEjhm7bkuLcG1D7EUVBbaf6kvNti0vtwkvITE7RhevXLOfj7l0CQQDEl38MRuaz3zKFO7K5qBBJcu62bLuuLKujfK27eu6Ekvx4y7WrmrrGDFMNEdh3u2REn6Uz7cJ80509+yUjjdBdAkAMLQpNOMSeu6NasyjC/NqBCcbqejySQxboR0UbJKY22hwqENvTlgPF0MldCPT1DdVSa1Y6iYrHAipUu5CI02UFAkAU/GcflGIESYtHFbxafUpfzNkWuDzAtxwLc7/IB7N2i4pQCs715qqRgbdL6d/FyEdH3BLynCrq0Fw0xZ6NURIxAkEAqfutNMv4ch3qen2X0Xlh59eYlUwObj7NnGHvMwQFk11YC6bZXUVQmNA0N5JgCApWAQFafROCjWyIy1jLhzRGFg==“;
            //public_key.pem内容,去掉头和尾,将换行符删除
            string publickey = “MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC5hsUU7zL6RS3lRxBj2sTnOX3qOyR2M6efCcWK9suFoZXkQXGl8A7k1OpvyyMnPEz6XoC4KS8XT8s+r7qngqssq0mx907WG5FeeLmQKSM09ayTeRelencq4icRwcMy808fZ/Sw2lbr8gmkhAPFrOjHSdmOFn0+jj3AwSxLb7InyQIDAQAB“;
            
            //加密字符串  
            string plaintext = “hahah123124“;

            //加密 和解密
            string encode = RSAUtils.encryptData(plaintext publickey “UTF-8“);
            string decode = RSAUtils.decryptData(encode privatekey “UTF-8“);
            Console.WriteLine(plaintext.Equals(decode));

            //签名  
            string src = “20180522201658IMFINE“;
            string sign = RSAUtils.sign(src privatekey “UTF-8“);
            Console.WriteLine(“签名:“ + sign);

            bool st = RSAUtils.verify(src sign publickey “UTF-8“);
            Console.WriteLine(“验签结果:“+st);

            Console.ReadLine();
        }
        
    }
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       3094  2019-05-06 14:37  Program.cs

     文件       2778  2019-05-06 14:34  RSAPem测试.csproj

     文件        913  2019-05-06 14:36  RSAPem测试.sln

    ..A..H.     11776  2019-05-06 14:42  RSAPem测试.suo

     文件      17111  2019-05-06 14:36  RSAUtils.cs

     文件    2236416  2015-12-28 21:38  bin\Debug\BouncyCastle.Crypto.dll

     文件      12800  2019-05-06 14:36  bin\Debug\RSAPem测试.exe

     文件      48640  2019-05-06 14:36  bin\Debug\RSAPem测试.pdb

     文件      14328  2019-05-06 14:36  bin\Debug\RSAPem测试.vshost.exe

     文件        490  2009-06-11 05:14  bin\Debug\RSAPem测试.vshost.exe.manifest

     文件       2715  2019-03-27 12:00  obj\Debug\ResolveAssemblyReference.cache

     文件        744  2019-05-06 14:36  obj\Debug\RSAPem测试.csproj.FileListAbsolute.txt

     文件      12800  2019-05-06 14:36  obj\Debug\RSAPem测试.exe

     文件      48640  2019-05-06 14:36  obj\Debug\RSAPem测试.pdb

     文件       1374  2019-03-19 15:09  Properties\AssemblyInfo.cs

     文件    2236416  2015-12-28 21:38  SharpPcap\BouncyCastle.Crypto.dll

     文件     196608  2013-01-14 19:59  SharpPcap\PacketDotNet.dll

     文件     476563  2013-01-14 19:59  SharpPcap\PacketDotNet.xml

     文件      72704  2013-01-14 19:59  SharpPcap\SharpPcap.dll

     文件        492  2013-01-14 19:59  SharpPcap\SharpPcap.dll.config

     文件     827393  2018-06-02 15:13  SharpPcap\SharpPcap.rar

     文件     186728  2013-01-14 19:59  SharpPcap\SharpPcap.xml

     文件     114688  2014-04-13 19:08  SharpPcap\SnmpSharpNet.dll

     目录          0  2019-03-19 15:55  obj\Debug\Refactor

     目录          0  2019-03-19 15:09  obj\Debug\TempPE

     目录          0  2019-05-06 14:36  bin\Debug

     目录          0  2019-05-06 14:36  obj\Debug

     目录          0  2019-05-06 14:28  bin

     目录          0  2019-05-06 14:28  obj

     目录          0  2019-05-06 14:28  Properties

............此处省略4个文件信息

评论

共有 条评论