C# 调用BouncyCastle生成PEM格式的私钥和公钥 , 加密解密 PKCS#1

引用第三方类库 BouncyCastle 

nuget:  https://www.nuget.org/packages/BouncyCastle

网址: http://www.bouncycastle.org/csharp/index.html

1.生成 pem  私钥

TextWriter textWriter = new StringWriter();
PemWriter pemWriter = new PemWriter(textWriter);
pemWriter.WriteObject(keys.Private);
pemWriter.Writer.Flush();

string privateKey = textWriter.ToString();

//公钥

 TextWriter textpubWriter = new StringWriter();
 PemWriter pempubWriter = new PemWriter(textpubWriter);
 pempubWriter.WriteObject(keys.Public);
 pempubWriter.Writer.Flush();
 string pubKey = textpubWriter.ToString();

生成结果

公钥:

-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCDzNqPhq2nk1aIsIK30X7UAHC5
jNqVKk12ZxsVEOjPFajV82VuJ7a31uLtU3NWRN4SIzhXmhAiptiDXq04AReV6veh
3xdI069xmsIx/EEJNHnL034is6gSVev8p7wJ+qevxdYUGaMVgAV6osYAeQhMTTld
emiiriKVAoeezhs5fQIDAQAB
-----END PUBLIC KEY-----

私钥

-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQCDzNqPhq2nk1aIsIK30X7UAHC5jNqVKk12ZxsVEOjPFajV82Vu
J7a31uLtU3NWRN4SIzhXmhAiptiDXq04AReV6veh3xdI069xmsIx/EEJNHnL034i
s6gSVev8p7wJ+qevxdYUGaMVgAV6osYAeQhMTTldemiiriKVAoeezhs5fQIDAQAB
AoGAC5l5psx1Xg+WiKmLZ5gb1f0P7q4zbCtXILnxGG9VJFySPMb5QOX+RVTqwqfn
uUqOuZpSuOSWa1mAKdepEQoixCIBTbG8THhbXi5/vdN4FMUXmHBe1Gage0HFE5Ep
EMGpFuGZW9PmXG5m78gkTwc/cYJYWSA6I9AMWJ8rBGfti/UCQQC9ehPq0wFV8icL
qcW3HlNsPzpqKPWFKp9UWz8kB6ayyTX9pci02Rp4Fig3i625+eMnJ+Kq0DRVGeJE
4auCugb7AkEAshLgdDxnvQVbPFJvxF8yAZe3Tu4L2vbEA55wqnEdHMUSnHERXO72
9Uk1t2n5v67xIksGewyZ0X6x/7wbsg035wJAR+0FjoiW4zdPFyBVS5QZeuftlssB
mJF7dhxFNbopz4iOFEFQlUYsxBU1IxDE2+o+DmkXpFvw4bU3rYo0/yT67QJAQcpR
nQA4P5C8DYRTgNS+7/Gyqvmt7Z8f+kpOS9pwrBFvd9caWCB9o1ACBZqkyBoFQus/
ILUeG1VYxCgmf1j83wJBAK1ETGDQM9LPOZAtB7Idv1Xo4sJtDn90yIyjhJjE6hhA
pvGGRb/1aOB1DMrx3JweIUB7rKMpkGVcouFIO4/s9cU=
-----END RSA PRIVATE KEY-----

2. 使用pem 证书解密或解密.  公钥私钥类型不一样

使用公钥加解密:

using (TextReader reader = new StringReader(pubkeyPemString}))  
{
      var enType=true; //加密true  解密 false;
      var key = new Org.BouncyCastle.OpenSsl.PemReader(reader).ReadObject() as AsymmetricKeyParameter;
      var rsa = new RsaEngine();
      rsa.Init(enType, key);  
      var data = Encoding.UTF8.GetBytes(tb_input.Text);
      entData = rsa.ProcessBlock(data, 0, data.Length);
      tb_output.Text = "加密: \r\n\r\n" + Encoding.UTF8.GetString(entData);
  }

使用 私钥加解密:  私钥中带有公钥信息.解析出来的是密钥对AsymmetricCipherKeyPair

using (TextReader reader2 = new StringReader(tb_privitekey.Text))
{
         dynamic key = new Org.BouncyCastle.OpenSsl.PemReader(reader2).ReadObject();
         var rsa2 = new RsaEngine();
         if (key is AsymmetricKeyParameter)
         {
                    key = (AsymmetricKeyParameter)key;
         }
         else if(key is AsymmetricCipherKeyPair)
         {
                    key = ((AsymmetricCipherKeyPair)key).Private;
         }
         rsa2.Init(false, key);  //加密true 解密 false

      //entData 待加密解密的 串 
      byte[] entData= Encoding.UTF8.GetBytes(tb_input.Text);
      entData = rsa2.ProcessBlock(entData, 0, entData.Length);
      tb_output.Text += "解密:\r\n \r\n" + Encoding.UTF8.GetString(entData);
}

参考文章:

 c#.net 调用BouncyCastle生成PEM格式的私钥和公钥 https://www.cnblogs.com/runliuv/p/5965951.html




展开阅读全文

没有更多推荐了,返回首页