開(kāi)源API鏈接地址:The Legion of the Bouncy Castle
Bouncy Castle舶担,簡(jiǎn)稱為BC,原本是java的一個(gè)開(kāi)源JCE提供者箕昭,后來(lái)也提供了C#版本的API灵妨,我下載其編譯好的DLL,在C#項(xiàng)目中直接引用落竹,用其幾個(gè)API泌霍,產(chǎn)生我指定位數(shù)的公鑰和私鑰(目前是1024位,但產(chǎn)生CA的密鑰時(shí)述召,要2048位才能滿足安全需求)朱转。雖然開(kāi)源很好很強(qiáng)大,但這個(gè)API就是文檔很缺陷桨武,C#的文檔更是少得可憐肋拔,沒(méi)辦法,下載源代碼慢慢看吧
在接下來(lái)的幾篇關(guān)于CA文章中呀酸,大體按下面鏈接網(wǎng)址的思路去整理凉蜂,不過(guò)整理出來(lái)的是C#版本的實(shí)現(xiàn),基本目標(biāo)架設(shè)一個(gè)CA性誉,產(chǎn)生用戶使用的數(shù)字證書窿吩。網(wǎng)頁(yè)鏈接:
產(chǎn)生密鑰,主要是用RsaKeyPairGenerator错览,根據(jù)參數(shù)RsaKeyGenerationParameters纫雁,產(chǎn)生一個(gè)密鑰對(duì),再分離出公鑰和私鑰倾哺,再用公鑰和私鑰進(jìn)行加解密轧邪。
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加解密算法實(shí)現(xiàn)的類忌愚,就是繼承了該接口
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è)試代碼:
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密鑰對(duì)的構(gòu)造器
RsaKeyPairGenerator keyGenerator = new RsaKeyPairGenerator();
//RSA密鑰構(gòu)造器的參數(shù)
RsaKeyGenerationParameters param = new RsaKeyGenerationParameters(
Org.BouncyCastle.Math.BigInteger.ValueOf(3),
new Org.BouncyCastle.Security.SecureRandom(),
1024, //密鑰長(zhǎng)度
25);
//用參數(shù)初始化密鑰構(gòu)造器
keyGenerator.Init(param);
//產(chǎn)生密鑰對(duì)
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");
}
//一個(gè)測(cè)試……………………
//輸入曲管,十六進(jìn)制的字符串,解碼為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);
//非對(duì)稱加密算法硕糊,加解密用
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();
}
}
}