用Bouncy Castle的C#版API产生公钥和私钥

开源API链接地址:The Legion of the Bouncy Castle

     Bouncy Castle,简称为BC,原本是java的一个开源JCE提供者,后来也提供了C#版本的API,我下载其编译好的DLL,在C#项目中直接引用,用其几个API,产生我指定位数的公钥和私钥(目前是1024位,但产生CA的密钥时,要2048位才能满足安全需求)。虽然开源很好很强大,但这个API就是文档很缺陷,C#的文档更是少得可怜,没办法,下载源代码慢慢看吧。。。

     在接下来的几篇关于CA文章中,大体按下面链接网址的思路去整理,不过整理出来的是C#版本的实现,基本目标架设一个CA,产生用户使用的数字证书。网页链接:bouncycastle 产生证书 

     产生密钥,主要是用RsaKeyPairGenerator,根据参数RsaKeyGenerationParameters,产生一个密钥对,再分离出公钥和私钥,再用公钥和私钥进行加解密。

RsaKeyPairGenerator的类,类中的其他类自行加载“BouncyCastle.Crypto.dll”到VS中自行查看

  1. namespace Org.BouncyCastle.Crypto.Generators 
  2.     public class RsaKeyPairGenerator : IAsymmetricCipherKeyPairGenerator 
  3.     { 
  4.         public RsaKeyPairGenerator(); 
  5.         public AsymmetricCipherKeyPair GenerateKeyPair(); 
  6.         public void Init(KeyGenerationParameters parameters); 
  7.     } 
  8. }  

接口IAsymmetricBlockCipher,RSA加解密算法实现的类,就是继承了该接口

  1. namespace Org.BouncyCastle.Crypto 
  2.     public interface IAsymmetricBlockCipher 
  3.     { 
  4.         string AlgorithmName { get; } 
  5.         int GetInputBlockSize(); 
  6.         int GetOutputBlockSize(); 
  7.         void Init(bool forEncryption, ICipherParameters parameters); 
  8.         byte[] ProcessBlock(byte[] inBuf, int inOff, int inLen); 
  9.     } 


测试代码:

  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Text; 
  4. using Org.BouncyCastle.Crypto.Generators; 
  5. using Org.BouncyCastle.Crypto.Parameters; 
  6. using Org.BouncyCastle.Crypto; 
  7. using Org.BouncyCastle.Security; 
  8. using Org.BouncyCastle.Crypto.Engines;  //IAsymmetricBlockCipher engine = new RsaEngine(); 
  9. namespace ConsoleApplication1 
  10.     class Program 
  11.     {  
  12.         static void Main(string[] args) 
  13.         { 
  14.             //RSA密钥对的构造器 
  15.             RsaKeyPairGenerator keyGenerator = new RsaKeyPairGenerator(); 
  16.              
  17.             //RSA密钥构造器的参数 
  18.             RsaKeyGenerationParameters param = new RsaKeyGenerationParameters( 
  19.                 Org.BouncyCastle.Math.BigInteger.ValueOf(3),  
  20.                 new Org.BouncyCastle.Security.SecureRandom(),  
  21.                 1024,   //密钥长度 
  22.                 25); 
  23.             //用参数初始化密钥构造器 
  24.             keyGenerator.Init(param); 
  25.             //产生密钥对 
  26.             AsymmetricCipherKeyPair keyPair = keyGenerator.GenerateKeyPair(); 
  27.             //获取公钥和密钥 
  28.             AsymmetricKeyParameter publicKey = keyPair.Public; 
  29.             AsymmetricKeyParameter privateKey = keyPair.Private; 
  30.             if( ((RsaKeyParameters)publicKey).Modulus.BitLength<1024 ) 
  31.             { 
  32.                 Console.WriteLine("failed key generation (1024) length test");                 
  33.             } 
  34.             //一个测试…………………… 
  35.             //输入,十六进制的字符串,解码为byte[] 
  36.             //string input = "4e6f77206973207468652074696d6520666f7220616c6c20676f6f64206d656e"; 
  37.             //byte[] testData = Org.BouncyCastle.Utilities.Encoders.Hex.Decode(input);            
  38.             string input = "popozh RSA test"
  39.             byte[] testData = Encoding.UTF8.GetBytes(input); 
  40.             Console.WriteLine("明文:" + input + Environment.NewLine); 
  41.             //非对称加密算法,加解密用 
  42.             IAsymmetricBlockCipher engine = new RsaEngine(); 
  43.             //公钥加密 
  44.             engine.Init(true, publicKey); 
  45.             try 
  46.             { 
  47.                 testData = engine.ProcessBlock(testData, 0, testData.Length);                
  48.                 Console.WriteLine("密文(base64编码):" + Convert.ToBase64String(testData) + Environment.NewLine); 
  49.             } 
  50.             catch (Exception ex) 
  51.             { 
  52.                 Console.WriteLine("failed - exception " + Environment.NewLine + ex.ToString()); 
  53.             } 
  54.             //私钥解密 
  55.             engine.Init(false, privateKey); 
  56.             try 
  57.             { 
  58.                 testData = engine.ProcessBlock(testData, 0, testData.Length); 
  59.     
  60.             } 
  61.             catch (Exception e) 
  62.             { 
  63.                 Console.WriteLine("failed - exception " + e.ToString()); 
  64.             } 
  65.             if (input.Equals(Encoding.UTF8.GetString(testData))) 
  66.             { 
  67.                 Console.WriteLine("解密成功"); 
  68.             } 
  69.             Console.Read(); 
  70.         } 
  71.     } 

BC的API源代码中,以上的代码测试思路来自:csharp/crypto/test/src/crypto/test/RsaTest.cs,可以定位到该CS文件参考官方提供的测试和代码

原文地址:https://www.cnblogs.com/zjoch/p/2709023.html