<PackageReference Include="BouncyCastle.Cryptography" Version="2.6.2" />

PrivateKeyInfoFactory

public static class PrivateKeyInfoFactory
using Org.BouncyCastle.Asn1; using Org.BouncyCastle.Asn1.CryptoPro; using Org.BouncyCastle.Asn1.EdEC; using Org.BouncyCastle.Asn1.Oiw; using Org.BouncyCastle.Asn1.Pkcs; using Org.BouncyCastle.Asn1.Rosstandart; using Org.BouncyCastle.Asn1.Sec; using Org.BouncyCastle.Asn1.X509; using Org.BouncyCastle.Asn1.X9; using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Crypto.Generators; using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.Math; using Org.BouncyCastle.Security; using Org.BouncyCastle.Utilities; using System; namespace Org.BouncyCastle.Pkcs { public static class PrivateKeyInfoFactory { public static PrivateKeyInfo CreatePrivateKeyInfo(AsymmetricKeyParameter privateKey) { return CreatePrivateKeyInfo(privateKey, null); } public static PrivateKeyInfo CreatePrivateKeyInfo(AsymmetricKeyParameter privateKey, Asn1Set attributes) { if (privateKey == null) throw new ArgumentNullException("privateKey"); if (!privateKey.IsPrivate) throw new ArgumentException("Public key passed - private key expected", "privateKey"); ElGamalPrivateKeyParameters elGamalPrivateKeyParameters = privateKey as ElGamalPrivateKeyParameters; if (elGamalPrivateKeyParameters != null) { ElGamalParameters parameters = elGamalPrivateKeyParameters.Parameters; ElGamalParameter parameters2 = new ElGamalParameter(parameters.P, parameters.G); return new PrivateKeyInfo(new AlgorithmIdentifier(OiwObjectIdentifiers.ElGamalAlgorithm, parameters2), new DerInteger(elGamalPrivateKeyParameters.X), attributes); } DsaPrivateKeyParameters dsaPrivateKeyParameters = privateKey as DsaPrivateKeyParameters; if (dsaPrivateKeyParameters != null) { DsaParameters parameters3 = dsaPrivateKeyParameters.Parameters; DsaParameter parameters4 = new DsaParameter(parameters3.P, parameters3.Q, parameters3.G); return new PrivateKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.IdDsa, parameters4), new DerInteger(dsaPrivateKeyParameters.X), attributes); } DHPrivateKeyParameters dHPrivateKeyParameters = privateKey as DHPrivateKeyParameters; if (dHPrivateKeyParameters != null) { DHParameters parameters5 = dHPrivateKeyParameters.Parameters; DHParameter parameters6 = new DHParameter(parameters5.P, parameters5.G, parameters5.L); return new PrivateKeyInfo(new AlgorithmIdentifier(dHPrivateKeyParameters.AlgorithmOid, parameters6), new DerInteger(dHPrivateKeyParameters.X), attributes); } RsaKeyParameters rsaKeyParameters = privateKey as RsaKeyParameters; if (rsaKeyParameters != null) { AlgorithmIdentifier privateKeyAlgorithm = new AlgorithmIdentifier(PkcsObjectIdentifiers.RsaEncryption, DerNull.Instance); RsaPrivateCrtKeyParameters rsaPrivateCrtKeyParameters = privateKey as RsaPrivateCrtKeyParameters; RsaPrivateKeyStructure privateKey2 = (rsaPrivateCrtKeyParameters == null) ? new RsaPrivateKeyStructure(rsaKeyParameters.Modulus, BigInteger.Zero, rsaKeyParameters.Exponent, BigInteger.Zero, BigInteger.Zero, BigInteger.Zero, BigInteger.Zero, BigInteger.Zero) : new RsaPrivateKeyStructure(rsaPrivateCrtKeyParameters.Modulus, rsaPrivateCrtKeyParameters.PublicExponent, rsaPrivateCrtKeyParameters.Exponent, rsaPrivateCrtKeyParameters.P, rsaPrivateCrtKeyParameters.Q, rsaPrivateCrtKeyParameters.DP, rsaPrivateCrtKeyParameters.DQ, rsaPrivateCrtKeyParameters.QInv); return new PrivateKeyInfo(privateKeyAlgorithm, privateKey2, attributes); } ECPrivateKeyParameters eCPrivateKeyParameters = privateKey as ECPrivateKeyParameters; if (eCPrivateKeyParameters != null) { DerBitString publicKey = new DerBitString(ECKeyPairGenerator.GetCorrespondingPublicKey(eCPrivateKeyParameters).Q.GetEncoded(false)); ECDomainParameters parameters7 = eCPrivateKeyParameters.Parameters; ECGost3410Parameters eCGost3410Parameters = parameters7 as ECGost3410Parameters; if (eCGost3410Parameters != null) { Gost3410PublicKeyAlgParameters parameters8 = new Gost3410PublicKeyAlgParameters(eCGost3410Parameters.PublicKeyParamSet, eCGost3410Parameters.DigestParamSet, eCGost3410Parameters.EncryptionParamSet); bool num = eCPrivateKeyParameters.D.BitLength > 256; DerObjectIdentifier algorithm = num ? RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512 : RosstandartObjectIdentifiers.id_tc26_gost_3410_12_256; int num2 = num ? 64 : 32; byte[] array = new byte[num2]; ExtractBytes(array, num2, 0, eCPrivateKeyParameters.D); return new PrivateKeyInfo(new AlgorithmIdentifier(algorithm, parameters8), new DerOctetString(array)); } int bitLength = parameters7.N.BitLength; AlgorithmIdentifier privateKeyAlgorithm2; ECPrivateKeyStructure privateKey3; if (eCPrivateKeyParameters.AlgorithmName == "ECGOST3410") { if (eCPrivateKeyParameters.PublicKeyParamSet == null) throw new NotImplementedException("Not a CryptoPro parameter set"); Gost3410PublicKeyAlgParameters parameters9 = new Gost3410PublicKeyAlgParameters(eCPrivateKeyParameters.PublicKeyParamSet, CryptoProObjectIdentifiers.GostR3411x94CryptoProParamSet); privateKeyAlgorithm2 = new AlgorithmIdentifier(CryptoProObjectIdentifiers.GostR3410x2001, parameters9); privateKey3 = new ECPrivateKeyStructure(bitLength, eCPrivateKeyParameters.D, publicKey, null); } else { X962Parameters parameters10 = parameters7.ToX962Parameters(); privateKey3 = new ECPrivateKeyStructure(bitLength, eCPrivateKeyParameters.D, publicKey, parameters10); privateKeyAlgorithm2 = new AlgorithmIdentifier(X9ObjectIdentifiers.IdECPublicKey, parameters10); } return new PrivateKeyInfo(privateKeyAlgorithm2, privateKey3, attributes); } Gost3410PrivateKeyParameters gost3410PrivateKeyParameters = privateKey as Gost3410PrivateKeyParameters; if (gost3410PrivateKeyParameters != null) { if (gost3410PrivateKeyParameters.PublicKeyParamSet == null) throw new NotImplementedException("Not a CryptoPro parameter set"); byte[] contents = Arrays.ReverseInPlace(gost3410PrivateKeyParameters.X.ToByteArrayUnsigned()); Gost3410PublicKeyAlgParameters parameters11 = new Gost3410PublicKeyAlgParameters(gost3410PrivateKeyParameters.PublicKeyParamSet, CryptoProObjectIdentifiers.GostR3411x94CryptoProParamSet, null); return new PrivateKeyInfo(new AlgorithmIdentifier(CryptoProObjectIdentifiers.GostR3410x94, parameters11), new DerOctetString(contents), attributes); } X448PrivateKeyParameters x448PrivateKeyParameters = privateKey as X448PrivateKeyParameters; if (x448PrivateKeyParameters != null) return new PrivateKeyInfo(new AlgorithmIdentifier(EdECObjectIdentifiers.id_X448), new DerOctetString(x448PrivateKeyParameters.GetEncoded()), attributes, x448PrivateKeyParameters.GeneratePublicKey().GetEncoded()); X25519PrivateKeyParameters x25519PrivateKeyParameters = privateKey as X25519PrivateKeyParameters; if (x25519PrivateKeyParameters != null) return new PrivateKeyInfo(new AlgorithmIdentifier(EdECObjectIdentifiers.id_X25519), new DerOctetString(x25519PrivateKeyParameters.GetEncoded()), attributes, x25519PrivateKeyParameters.GeneratePublicKey().GetEncoded()); Ed448PrivateKeyParameters ed448PrivateKeyParameters = privateKey as Ed448PrivateKeyParameters; if (ed448PrivateKeyParameters != null) return new PrivateKeyInfo(new AlgorithmIdentifier(EdECObjectIdentifiers.id_Ed448), new DerOctetString(ed448PrivateKeyParameters.GetEncoded()), attributes, ed448PrivateKeyParameters.GeneratePublicKey().GetEncoded()); Ed25519PrivateKeyParameters ed25519PrivateKeyParameters = privateKey as Ed25519PrivateKeyParameters; if (ed25519PrivateKeyParameters != null) return new PrivateKeyInfo(new AlgorithmIdentifier(EdECObjectIdentifiers.id_Ed25519), new DerOctetString(ed25519PrivateKeyParameters.GetEncoded()), attributes, ed25519PrivateKeyParameters.GeneratePublicKey().GetEncoded()); MLDsaPrivateKeyParameters mLDsaPrivateKeyParameters = privateKey as MLDsaPrivateKeyParameters; if (mLDsaPrivateKeyParameters != null) { AlgorithmIdentifier privateKeyAlgorithm3 = new AlgorithmIdentifier(mLDsaPrivateKeyParameters.Parameters.Oid); Asn1Encodable mLDsaPrivateKeyAsn = GetMLDsaPrivateKeyAsn1(mLDsaPrivateKeyParameters); byte[] publicKey2 = null; return new PrivateKeyInfo(privateKeyAlgorithm3, mLDsaPrivateKeyAsn, attributes, publicKey2); } MLKemPrivateKeyParameters mLKemPrivateKeyParameters = privateKey as MLKemPrivateKeyParameters; if (mLKemPrivateKeyParameters != null) { AlgorithmIdentifier privateKeyAlgorithm4 = new AlgorithmIdentifier(mLKemPrivateKeyParameters.Parameters.Oid); Asn1Encodable mLKemPrivateKeyAsn = GetMLKemPrivateKeyAsn1(mLKemPrivateKeyParameters); byte[] publicKey3 = null; return new PrivateKeyInfo(privateKeyAlgorithm4, mLKemPrivateKeyAsn, attributes, publicKey3); } SlhDsaPrivateKeyParameters slhDsaPrivateKeyParameters = privateKey as SlhDsaPrivateKeyParameters; if (slhDsaPrivateKeyParameters != null) { AlgorithmIdentifier privateKeyAlgorithm5 = new AlgorithmIdentifier(slhDsaPrivateKeyParameters.Parameters.Oid); DerBitString publicKey4 = null; return PrivateKeyInfo.Create(privateKeyAlgorithm5, new DerOctetString(slhDsaPrivateKeyParameters.GetEncoded()), attributes, publicKey4); } throw new ArgumentException("Class provided is not convertible: " + Platform.GetTypeName(privateKey)); } public static PrivateKeyInfo CreatePrivateKeyInfo(char[] passPhrase, EncryptedPrivateKeyInfo encInfo) { return CreatePrivateKeyInfo(passPhrase, false, encInfo); } public static PrivateKeyInfo CreatePrivateKeyInfo(char[] passPhrase, bool wrongPkcs12Zero, EncryptedPrivateKeyInfo encInfo) { AlgorithmIdentifier encryptionAlgorithm = encInfo.EncryptionAlgorithm; IBufferedCipher obj = PbeUtilities.CreateEngine(encryptionAlgorithm) as IBufferedCipher; if (obj == null) throw new Exception("Unknown encryption algorithm: " + encryptionAlgorithm.Algorithm?.ToString()); ICipherParameters parameters = PbeUtilities.GenerateCipherParameters(encryptionAlgorithm, passPhrase, wrongPkcs12Zero); obj.Init(false, parameters); return PrivateKeyInfo.GetInstance(obj.DoFinal(encInfo.GetEncryptedData())); } private static void ExtractBytes(byte[] encKey, int size, int offSet, BigInteger bI) { byte[] array = bI.ToByteArray(); if (array.Length < size) { byte[] array2 = new byte[size]; Array.Copy(array, 0, array2, array2.Length - array.Length, array.Length); array = array2; } for (int i = 0; i != size; i++) { encKey[offSet + i] = array[array.Length - 1 - i]; } } private static Asn1Encodable GetMLDsaPrivateKeyAsn1(MLDsaPrivateKeyParameters key) { switch (key.PreferredFormat) { case MLDsaPrivateKeyParameters.Format.EncodingOnly: return new DerOctetString(key.GetEncoded()); case MLDsaPrivateKeyParameters.Format.SeedOnly: return new DerTaggedObject(false, 0, new DerOctetString(key.GetSeed())); default: return new DerSequence(new DerOctetString(key.GetSeed()), new DerOctetString(key.GetEncoded())); } } private static Asn1Encodable GetMLKemPrivateKeyAsn1(MLKemPrivateKeyParameters key) { switch (key.PreferredFormat) { case MLKemPrivateKeyParameters.Format.EncodingOnly: return new DerOctetString(key.GetEncoded()); case MLKemPrivateKeyParameters.Format.SeedOnly: return new DerTaggedObject(false, 0, new DerOctetString(key.GetSeed())); default: return new DerSequence(new DerOctetString(key.GetSeed()), new DerOctetString(key.GetEncoded())); } } } }