RsaKeyPairGenerator r = new RsaKeyPairGenerator();
r.Init(new KeyGenerationParameters(new SecureRandom(), 1024));
AsymmetricCipherKeyPair keys = r.GenerateKeyPair();
AsymmetricKeyParameter private_key = keys.Private;
AsymmetricKeyParameter public_key = keys.Public;
TextWriter textWriter = new StringWriter();
PemWriter pemWriter = new PemWriter(textWriter);
pemWriter.WriteObject(keys.Private);
pemWriter.Writer.Flush();
string privateKey = textWriter.ToString();
txtPri.Text = privateKey;
TextWriter textpubWriter = new StringWriter();
PemWriter pempubWriter = new PemWriter(textpubWriter);
pempubWriter.WriteObject(keys.Public);
pempubWriter.Writer.Flush();
string pubKey = textpubWriter.ToString();
txtPub.Text = pubKey;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Generators;
using Org.BouncyCastle.OpenSsl;
using Org.BouncyCastle.Security;
--XML格式转PEM格式,引用:http://www.jianshu.com/p/faefcc58c79b
public static void XMLConvertToPEM()//XML格式密钥转PEM
var rsa2 = new RSACryptoServiceProvider();
using (var sr = new StreamReader("e:\\PrivateKey.xml"))
rsa2.FromXmlString(sr.ReadToEnd());
var p = rsa2.ExportParameters(true);
var key = new RsaPrivateCrtKeyParameters(
new BigInteger(1, p.Modulus), new BigInteger(1, p.Exponent), new BigInteger(1, p.D),
new BigInteger(1, p.P), new BigInteger(1, p.Q), new BigInteger(1, p.DP), new BigInteger(1, p.DQ),
new BigInteger(1, p.InverseQ));
using (var sw = new StreamWriter("e:\\PrivateKey.pem"))
var pemWriter = new Org.BouncyCastle.OpenSsl.PemWriter(sw);
pemWriter.WriteObject(key);
网上示例都是从文件读取出XML格式私钥,存入PEM文件中,PEM中就是转换好的格式。
读取XML私钥的代码可以换成生成XML私钥,以下代码是转好格式存入字符串string 中。
public void xmlToPem()
CspParameters parameters = new CspParameters();
parameters.KeyContainerName = DateTime.Now.ToString();
//可以选择导入XML格式的私钥还是生成。无论是存储在.XML文件中还是在数据库等地方。 这里是新生成一个C# XML格式私钥来转成PEM格式。
var rsa2 = new RSACryptoServiceProvider(parameters);
//using (var sr = new StreamReader("e:\\PrivateKey.xml"))
// rsa2.FromXmlString(sr.ReadToEnd());
var p = rsa2.ExportParameters(true);
var key = new RsaPrivateCrtKeyParameters(
new BigInteger(1, p.Modulus), new BigInteger(1, p.Exponent), new BigInteger(1, p.D),
new BigInteger(1, p.P), new BigInteger(1, p.Q), new BigInteger(1, p.DP), new BigInteger(1, p.DQ),
new BigInteger(1, p.InverseQ));
using (TextWriter sw = new StringWriter())
var pemWriter = new Org.BouncyCastle.OpenSsl.PemWriter(sw);
pemWriter.WriteObject(key);
pemWriter.Writer.Flush();
string priKey = sw.ToString();
XML公钥转PEM公钥的代码还未研究。