DotNetUtilities
A class containing methods to interface the BouncyCastle world to the .NET Crypto world.
using Org.BouncyCastle.Asn1.Pkcs;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Utilities;
using Org.BouncyCastle.X509;
using System;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
namespace Org.BouncyCastle.Security
{
public static class DotNetUtilities
{
public static System.Security.Cryptography.X509Certificates.X509Certificate ToX509Certificate(X509CertificateStructure x509Struct)
{
return new X509Certificate2(x509Struct.GetEncoded("DER"));
}
public static System.Security.Cryptography.X509Certificates.X509Certificate ToX509Certificate(Org.BouncyCastle.X509.X509Certificate x509Cert)
{
return ToX509Certificate(x509Cert.CertificateStructure);
}
public static Org.BouncyCastle.X509.X509Certificate FromX509Certificate(System.Security.Cryptography.X509Certificates.X509Certificate x509Cert)
{
return new Org.BouncyCastle.X509.X509Certificate(x509Cert.GetRawCertData());
}
public static AsymmetricCipherKeyPair GetDsaKeyPair(DSA dsa)
{
return GetDsaKeyPair(dsa.ExportParameters(true));
}
public static AsymmetricCipherKeyPair GetDsaKeyPair(DSAParameters dp)
{
DsaPublicKeyParameters dsaPublicKey = GetDsaPublicKey(dp);
DsaPrivateKeyParameters privateParameter = new DsaPrivateKeyParameters(new BigInteger(1, dp.X), dsaPublicKey.Parameters);
return new AsymmetricCipherKeyPair(dsaPublicKey, privateParameter);
}
public static DsaPublicKeyParameters GetDsaPublicKey(DSA dsa)
{
return GetDsaPublicKey(dsa.ExportParameters(false));
}
public static DsaPublicKeyParameters GetDsaPublicKey(DSAParameters dp)
{
DsaValidationParameters parameters = (dp.Seed != null) ? new DsaValidationParameters(dp.Seed, dp.Counter) : null;
DsaParameters parameters2 = new DsaParameters(new BigInteger(1, dp.P), new BigInteger(1, dp.Q), new BigInteger(1, dp.G), parameters);
return new DsaPublicKeyParameters(new BigInteger(1, dp.Y), parameters2);
}
public static AsymmetricCipherKeyPair GetRsaKeyPair(RSA rsa)
{
return GetRsaKeyPair(rsa.ExportParameters(true));
}
public static AsymmetricCipherKeyPair GetRsaKeyPair(RSAParameters rp)
{
RsaKeyParameters rsaPublicKey = GetRsaPublicKey(rp);
RsaPrivateCrtKeyParameters privateParameter = new RsaPrivateCrtKeyParameters(rsaPublicKey.Modulus, rsaPublicKey.Exponent, new BigInteger(1, rp.D), new BigInteger(1, rp.P), new BigInteger(1, rp.Q), new BigInteger(1, rp.DP), new BigInteger(1, rp.DQ), new BigInteger(1, rp.InverseQ));
return new AsymmetricCipherKeyPair(rsaPublicKey, privateParameter);
}
public static RsaKeyParameters GetRsaPublicKey(RSA rsa)
{
return GetRsaPublicKey(rsa.ExportParameters(false));
}
public static RsaKeyParameters GetRsaPublicKey(RSAParameters rp)
{
return new RsaKeyParameters(false, new BigInteger(1, rp.Modulus), new BigInteger(1, rp.Exponent));
}
public static AsymmetricCipherKeyPair GetKeyPair(AsymmetricAlgorithm privateKey)
{
DSA dSA = privateKey as DSA;
if (dSA != null)
return GetDsaKeyPair(dSA);
RSA rSA = privateKey as RSA;
if (rSA != null)
return GetRsaKeyPair(rSA);
throw new ArgumentException("Unsupported algorithm specified", "privateKey");
}
public static RSA ToRSA(RsaKeyParameters rsaKey)
{
return CreateRSAProvider(ToRSAParameters(rsaKey));
}
public static RSA ToRSA(RsaKeyParameters rsaKey, CspParameters csp)
{
return CreateRSAProvider(ToRSAParameters(rsaKey), csp);
}
public static RSA ToRSA(RsaPrivateCrtKeyParameters privKey)
{
return CreateRSAProvider(ToRSAParameters(privKey));
}
public static RSA ToRSA(RsaPrivateCrtKeyParameters privKey, CspParameters csp)
{
return CreateRSAProvider(ToRSAParameters(privKey), csp);
}
public static RSA ToRSA(RsaPrivateKeyStructure privKey)
{
return CreateRSAProvider(ToRSAParameters(privKey));
}
public static RSA ToRSA(RsaPrivateKeyStructure privKey, CspParameters csp)
{
return CreateRSAProvider(ToRSAParameters(privKey), csp);
}
public static RSAParameters ToRSAParameters(RsaKeyParameters rsaKey)
{
RSAParameters rSAParameters = default(RSAParameters);
rSAParameters.Modulus = rsaKey.Modulus.ToByteArrayUnsigned();
if (rsaKey.IsPrivate)
rSAParameters.D = ConvertRSAParametersField(rsaKey.Exponent, rSAParameters.Modulus.Length);
else
rSAParameters.Exponent = rsaKey.Exponent.ToByteArrayUnsigned();
return rSAParameters;
}
public static RSAParameters ToRSAParameters(RsaPrivateCrtKeyParameters privKey)
{
RSAParameters rSAParameters = default(RSAParameters);
rSAParameters.Modulus = privKey.Modulus.ToByteArrayUnsigned();
rSAParameters.Exponent = privKey.PublicExponent.ToByteArrayUnsigned();
rSAParameters.P = privKey.P.ToByteArrayUnsigned();
rSAParameters.Q = privKey.Q.ToByteArrayUnsigned();
rSAParameters.D = ConvertRSAParametersField(privKey.Exponent, rSAParameters.Modulus.Length);
rSAParameters.DP = ConvertRSAParametersField(privKey.DP, rSAParameters.P.Length);
rSAParameters.DQ = ConvertRSAParametersField(privKey.DQ, rSAParameters.Q.Length);
rSAParameters.InverseQ = ConvertRSAParametersField(privKey.QInv, rSAParameters.Q.Length);
return rSAParameters;
}
public static RSAParameters ToRSAParameters(RsaPrivateKeyStructure privKey)
{
RSAParameters rSAParameters = default(RSAParameters);
rSAParameters.Modulus = privKey.Modulus.ToByteArrayUnsigned();
rSAParameters.Exponent = privKey.PublicExponent.ToByteArrayUnsigned();
rSAParameters.P = privKey.Prime1.ToByteArrayUnsigned();
rSAParameters.Q = privKey.Prime2.ToByteArrayUnsigned();
rSAParameters.D = ConvertRSAParametersField(privKey.PrivateExponent, rSAParameters.Modulus.Length);
rSAParameters.DP = ConvertRSAParametersField(privKey.Exponent1, rSAParameters.P.Length);
rSAParameters.DQ = ConvertRSAParametersField(privKey.Exponent2, rSAParameters.Q.Length);
rSAParameters.InverseQ = ConvertRSAParametersField(privKey.Coefficient, rSAParameters.Q.Length);
return rSAParameters;
}
private static byte[] ConvertRSAParametersField(BigInteger n, int size)
{
return BigIntegers.AsUnsignedByteArray(size, n);
}
private static RSACryptoServiceProvider CreateRSAProvider(RSAParameters rp)
{
CspParameters cspParameters = new CspParameters();
cspParameters.KeyContainerName = $"""{Guid.NewGuid()}";
return CreateRSAProvider(rp, cspParameters);
}
private static RSACryptoServiceProvider CreateRSAProvider(RSAParameters rp, CspParameters csp)
{
RSACryptoServiceProvider rSACryptoServiceProvider = new RSACryptoServiceProvider(csp);
rSACryptoServiceProvider.ImportParameters(rp);
return rSACryptoServiceProvider;
}
}
}