用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,产生用户使用的数字证书。网页链接:
产生密钥,主要是用RsaKeyPairGenerator,根据参数RsaKeyGenerationParameters,产生一个密钥对,再分离出公钥和私钥,再用公钥和私钥进行加解密。

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

namespace Org.BouncyCastle.Crypto.Generators 
{ 
    public class RsaKeyPairGenerator : IAsymmetricCipherKeyPairGenerator 
    { 
        public RsaKeyPairGenerator(); 
        public AsymmetricCipherKeyPair GenerateKeyPair(); 
        public void Init(KeyGenerationParameters parameters); 
    } 
}  

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

namespace Org.BouncyCastle.Crypto 
{ 
    public interface IAsymmetricBlockCipher 
    { 
        string AlgorithmName { get; } 
        int GetInputBlockSize(); 
        int GetOutputBlockSize(); 
        void Init(bool forEncryption, ICipherParameters parameters); 
        byte[] ProcessBlock(byte[] inBuf, int inOff, int inLen); 
    } 
} 

测试代码:

using System; 
using System.Collections.Generic; 
using System.Text; 
using Org.BouncyCastle.Crypto.Generators; 
using Org.BouncyCastle.Crypto.Parameters; 
using Org.BouncyCastle.Crypto; 
using Org.BouncyCastle.Security; 
using Org.BouncyCastle.Crypto.Engines;  //IAsymmetricBlockCipher engine = new RsaEngine(); 
namespace ConsoleApplication1 
{ 
    class Program 
    {  
        static void Main(string[] args) 
        { 
            //RSA密钥对的构造器 
            RsaKeyPairGenerator keyGenerator = new RsaKeyPairGenerator(); 
             
            //RSA密钥构造器的参数 
            RsaKeyGenerationParameters param = new RsaKeyGenerationParameters( 
                Org.BouncyCastle.Math.BigInteger.ValueOf(3),  
                new Org.BouncyCastle.Security.SecureRandom(),  
                1024,   //密钥长度 
                25); 
            //用参数初始化密钥构造器 
            keyGenerator.Init(param); 
            //产生密钥对 
            AsymmetricCipherKeyPair keyPair = keyGenerator.GenerateKeyPair(); 
            //获取公钥和密钥 
            AsymmetricKeyParameter publicKey = keyPair.Public; 
            AsymmetricKeyParameter privateKey = keyPair.Private; 
            if( ((RsaKeyParameters)publicKey).Modulus.BitLength<1024 ) 
            { 
                Console.WriteLine("failed key generation (1024) length test");                 
            } 
            //一个测试…………………… 
            //输入,十六进制的字符串,解码为byte[] 
            //string input = "4e6f77206973207468652074696d6520666f7220616c6c20676f6f64206d656e"; 
            //byte[] testData = Org.BouncyCastle.Utilities.Encoders.Hex.Decode(input);            
            string input = "popozh RSA test"; 
            byte[] testData = Encoding.UTF8.GetBytes(input); 
            Console.WriteLine("明文:" + input + Environment.NewLine); 
            //非对称加密算法,加解密用 
            IAsymmetricBlockCipher engine = new RsaEngine(); 
            //公钥加密 
            engine.Init(true, publicKey); 
            try 
            { 
                testData = engine.ProcessBlock(testData, 0, testData.Length);                
                Console.WriteLine("密文(base64编码):" + Convert.ToBase64String(testData) + Environment.NewLine); 
            } 
            catch (Exception ex) 
            { 
                Console.WriteLine("failed - exception " + Environment.NewLine + ex.ToString()); 
            } 
            //私钥解密 
            engine.Init(false, privateKey); 
            try 
            { 
                testData = engine.ProcessBlock(testData, 0, testData.Length); 
    
            } 
            catch (Exception e) 
            { 
                Console.WriteLine("failed - exception " + e.ToString()); 
            } 
            if (input.Equals(Encoding.UTF8.GetString(testData))) 
            { 
                Console.WriteLine("解密成功"); 
            } 
            Console.Read(); 
        } 
    } 
} 

你可能感兴趣的:(用Bouncy Castle的C#版API产生公钥和私钥)