用Bouncy Castle的C#版API产生公钥和私钥
2014-03-26 15:58
302 查看
开源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?
namespace Org.BouncyCastle.Crypto.Generators
{
public class RsaKeyPairGenerator : IAsymmetricCipherKeyPairGenerator
{
public RsaKeyPairGenerator();
public AsymmetricCipherKeyPair GenerateKeyPair();
public void Init(KeyGenerationParameters parameters);
}
}
接口IAsymmetricBlockCipher,RSA加解密算法实现的类,就是继承了该接口
[c-sharp] view plaincopyprint?
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);
}
}
测试代码:
[c-sharp] view plaincopyprint?
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();
}
}
}
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,简称为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?
namespace Org.BouncyCastle.Crypto.Generators
{
public class RsaKeyPairGenerator : IAsymmetricCipherKeyPairGenerator
{
public RsaKeyPairGenerator();
public AsymmetricCipherKeyPair GenerateKeyPair();
public void Init(KeyGenerationParameters parameters);
}
}
接口IAsymmetricBlockCipher,RSA加解密算法实现的类,就是继承了该接口
[c-sharp] view plaincopyprint?
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);
}
}
测试代码:
[c-sharp] view plaincopyprint?
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();
}
}
}
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产生公钥和私钥
- 用Bouncy Castle的C#版API产生公钥和私钥
- 用Bouncy Castle的C#版API产生公钥和私钥
- 整合一个基于c#的RSA私钥加密公钥解密的Helper类,含源码
- .net C#使用私钥sign公钥验证 验证JWS signature data
- 银联手机支付(.Net Csharp),3DES加密解密,RSA加密解密,RSA私钥加密公钥解密,.Net RSA 3DES C#
- C# 生成私钥和公钥
- C#使用RSA私钥加密公钥解密的改进,解决特定情况下解密后出现乱码的问题
- c#生成rsa公钥和私钥
- C# RSA加密、解密、加签、验签、支持JAVA格式公钥私钥、PEM格式公钥私钥、.NET格式公钥私钥、一般模式【支持公钥加密,私钥解密】(一)
- RSA,JAVA私钥加密,C#公钥解密
- 整合一个基于C#的RSA私钥加密公钥解密的Helper类,含源码
- C# 基于大整数类的RSA算法实现(公钥加密私钥解密,私钥加密公钥解密)
- C#下实现RSA公钥私钥由pcks8格式(java)转化为pcks1格式(非java)
- C#中用RSA算法生成公钥和私钥 openssl RSA密钥的生成与配置
- c#公钥加密私钥解密和验证
- C# 与JAVA 的RSA 加密解密交互,互通,C#使用BouncyCastle来实现私钥加密,公钥解密的方法
- 银联手机支付(.Net Csharp),3DES加密解密,RSA加密解密,RSA私钥加密公钥解密,.Net RSA 3DES C#
- 整合一个基于c#的RSA私钥加密公钥解密的Helper类,含源码