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


原文:https://www.cnblogs.com/zjoch/archive/2012/09/30/2709023.html

开源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中自行查看

[c-sharp] view plaincopyprint?
  1. namespace Org.BouncyCastle.Crypto.Generators 
  2. { 
  3.     public class RsaKeyPairGenerator : IAsymmetricCipherKeyPairGenerator 
  4.     { 
  5.         public RsaKeyPairGenerator(); 
  6.         public AsymmetricCipherKeyPair GenerateKeyPair(); 
  7.         public void Init(KeyGenerationParameters parameters); 
  8.     } 
  9. }  

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

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

 

测试代码:

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

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