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

[c-sharp] view plain copy print ?
  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加解密算法實現的類,就是繼承了該接口

[c-sharp] view plain copy print ?
  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.     } 

 


測試代碼:

[c-sharp] view plain copy print ?
  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文件參考官方提供的測試和代碼


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM