C# 调用BouncyCastle将RSA密钥与pem格式进行互转

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
using System.IO;

using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Crypto;


public sealed class CryptoHelper
{
    /// <summary>
    /// RSA密钥转Pem密钥
    /// </summary>
    /// <param name="RSAKey">RSA密钥</param>
    /// <param name="isPrivateKey">是否是私钥</param>
    /// <returns>Pem密钥</returns>
    public static string RSAKeyToPem(string RSAKey, bool isPrivateKey)
    {
        string pemKey = string.Empty;
        var rsa = new RSACryptoServiceProvider();
        rsa.FromXmlString(RSAKey);
        RSAParameters rsaPara = new RSAParameters();
        RsaKeyParameters key = null;
        //RSA私钥
        if (isPrivateKey)
        {
            rsaPara=rsa.ExportParameters(true);
            key = new RsaPrivateCrtKeyParameters(
                new BigInteger(1, rsaPara.Modulus), new BigInteger(1, rsaPara.Exponent), new BigInteger(1, rsaPara.D),
                new BigInteger(1, rsaPara.P), new BigInteger(1, rsaPara.Q), new BigInteger(1, rsaPara.DP), new BigInteger(1, rsaPara.DQ),
                new BigInteger(1, rsaPara.InverseQ));
        }
        //RSA公钥
        else
        {
            rsaPara = rsa.ExportParameters(false);
            key = new RsaKeyParameters(false,
                new BigInteger(1, rsaPara.Modulus),
                new BigInteger(1, rsaPara.Exponent));
        }
        using (TextWriter sw = new StringWriter())
        {
            var pemWriter = new Org.BouncyCastle.OpenSsl.PemWriter(sw);
            pemWriter.WriteObject(key);
            pemWriter.Writer.Flush();
            pemKey = sw.ToString();
        }
        return pemKey;
    }
    /// <summary>
    /// Pem密钥转RSA密钥
    /// </summary>
    /// <param name="pemKey">Pem密钥</param>
    /// <param name="isPrivateKey">是否是私钥</param>
    /// <returns>RSA密钥</returns>
    public static string PemToRSAKey(string pemKey, bool isPrivateKey)
    {
        string rsaKey = string.Empty;
        object pemObject = null;
        RSAParameters rsaPara = new RSAParameters();
        using (StringReader sReader = new StringReader(pemKey))
        {
            var pemReader = new Org.BouncyCastle.OpenSsl.PemReader(sReader);
            pemObject = pemReader.ReadObject();
        }
        //RSA私钥
        if (isPrivateKey)
        {
            RsaPrivateCrtKeyParameters key = (RsaPrivateCrtKeyParameters)((AsymmetricCipherKeyPair)pemObject).Private;
            rsaPara = new RSAParameters
            {
                Modulus = key.Modulus.ToByteArrayUnsigned(),
                Exponent = key.PublicExponent.ToByteArrayUnsigned(),
                D = key.Exponent.ToByteArrayUnsigned(),
                P = key.P.ToByteArrayUnsigned(),
                Q = key.Q.ToByteArrayUnsigned(),
                DP = key.DP.ToByteArrayUnsigned(),
                DQ = key.DQ.ToByteArrayUnsigned(),
                InverseQ = key.QInv.ToByteArrayUnsigned(),
            };
        }
        //RSA公钥
        else
        {
            RsaKeyParameters key = (RsaKeyParameters)pemObject;
            rsaPara = new RSAParameters
            {
                Modulus = key.Modulus.ToByteArrayUnsigned(),
                Exponent = key.Exponent.ToByteArrayUnsigned(),
            };
        }
        RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
        rsa.ImportParameters(rsaPara);
        using (StringWriter sw = new StringWriter())
        {
            sw.Write(rsa.ToXmlString(isPrivateKey?true:false));
            rsaKey = sw.ToString();
        }
        return rsaKey;
    }
}

使用

RSA私钥转pem

CryptoHelper.RSAKeyToPem(privateKey,true);

转换前:

转换后

RSA公钥转pem

CryptoHelper.RSAKeyToPem(publicKey,false);

转换前

转换后

pem转RSA私钥

CryptoHelper.PemToRSAKey(pemkey,true)

pem转RSA公钥

CryptoHelper.PemToRSAKey(pemkey, false)

©️2020 CSDN 皮肤主题: 书香水墨 设计师:CSDN官方博客 返回首页