开源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. {
  3. public class RsaKeyPairGenerator : IAsymmetricCipherKeyPairGenerator
  4. {
  5. public RsaKeyPairGenerator();
  6. public AsymmetricCipherKeyPair GenerateKeyPair();
  7. public void Init(KeyGenerationParameters parameters);
  8. }
  9. }

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

  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. }


测试代码:

  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. //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. catch (Exception e)
  61. {
  62. Console.WriteLine("failed - exception " + e.ToString());
  63. }
  64. if (input.Equals(Encoding.UTF8.GetString(testData)))
  65. {
  66. Console.WriteLine("解密成功");
  67. }
  68. Console.Read();
  69. }
  70. }
  71. }

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

下篇思路:生成自签的CA根证书、生成用户的证书


密钥对的保存:http://blog.csdn.net/popozhu/archive/2010/08/10/5802656.aspx

用Bouncy Castle的C#版API产生公钥和私钥的更多相关文章

  1. 在C#中保存Bouncy Castle生成的密钥对

    在用Bouncy Castle的C#版API产生公钥和私钥 中产生了一对密钥对,可以用bouncy caslte提供的API进行保存 公钥方面的3个类,具体代码根据命名空间自行查看其源代码: Org. ...

  2. Bouncy Castle Crypto API c# port

    Bouncy Castle 是一种用于 Java 平台的开放源码的轻量级密码术包.它支持大量的密码术算法,并提供 JCE 1.2.1 的实现.现在有了C#的版本.下面是网站上的介绍 This port ...

  3. Arcgis API For IOS扩展AGSDynamicLayer新旧版API对比

    AGSDynamicLayer(ForSubclassEyesOnly) Category Reference Description This category organizes the meth ...

  4. 加密 bouncy castle

    1.去官方站点下载Bouncy Castle的JCE Provider包 bcprov-ext-jdk15-145.jar 2.把jar文件复制到 $JAVA_HOME$\jre\lib\ext 目录 ...

  5. bouncy castle的配置

    Bouncy Castle 是一种用于 Java 平台的开放源码的轻量级密码术包.它支持大量的密码术算法,并提供 JCE 1.2.1 的实现.因为 Bouncy Castle 被设计成轻量级的,所以从 ...

  6. 高德地图车机版API演示程序

    高德地图车机版API演示程序 做车载的应该和这个程序打交道打的比较多吧,这里是我今天写的一个实现了他的API的一个演示程序 首先我们来看下他的官网. http://lbs.amap.com/api/a ...

  7. 基于.Net平台C#的微信网页版API

    git上有很多类似的项目,但大多都是python和js的,为了便于.Net windows平台的使用,我重构了一个.Net版本的,已整理开源 https://github.com/leestar54/ ...

  8. 【Java密码学】使用Bouncy Castle生成数字签名、数字信封

    Bouncy Castle(轻量级密码术包)是一种用于 Java 平台的开放源码的轻量级密码术包,它支持大量的密码术算法,并提供 JCE 1.2.1 的实现.最近项目上正好用到了Bouncy Cast ...

  9. 将html版API文档转换成chm格式的API文档

    文章完全转载自: https://blog.csdn.net/u012557538/article/details/42089277 将html版API文档转换成chm格式的API文档并不是一件难事, ...

随机推荐

  1. CentOS 6.6 FTP install

    /************************************************************************* * CentOS 6.6 FTP install ...

  2. PHP startsWith and endsWith

    function startsWith($haystack, $needle) { // search backwards starting from haystack length characte ...

  3. 用jquery将复选框改成单选框

    前提是要包含jquery文件. 相关代码: jQuery(function($) { init_price_checkbox("by_price"); init_price_che ...

  4. 【转】论创新工场、职业发展、offer如何比较选择、移动互联网

    大纲:一.缘由.概述二.创新工场的模式三.职业发展道路的影响因素四.职业选择的几个小问题五.李开复的移动互联网和我眼中的移动互联网六.再见和祝福 一.缘由.概述1.缘由        前两周,有个师弟 ...

  5. 使用Hibernate命名查询

    HQL查询支持将查询所用的HQL语句放入配置文件中,而不是代码中,在Hibernate映射文件的<hibernate-mapping>元素中使用<query>子元素来定义命名查 ...

  6. Map-API及详解

    常用API 类别 方法 增加 put.putAll 删除 remove.removeAll.clear 查询 get 取值 entrySet.keySet.values 长度 size 判断 cont ...

  7. 推荐一些android开发学习的资料

    网址: 1:http://v.youku.com/v_show/id_XMTgwMTQ1MTgw.html 2:http://mars.apkbus.com/ 3:http://wenku.baidu ...

  8. Sql Server_笔记

    1.随机取出10条数据:select top 10 * from tablename order by newid()

  9. 用自己的机器人和ubuntu PC实现通信和控制--26

    原创博客:转载请表明出处:http://www.cnblogs.com/zxouxuewei/ 前提: 1.拥有一台能够采用手动或者自动移动的机器人移动平台. 2.在电机端需要安装高分辨率的霍尔编码器 ...

  10. html---文本框样式;

    一.一个单行文本框的例子 <form name="form1" action="mailto:3400982550@qq.com" method=&quo ...