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

PrivateKeyFactory

public static class PrivateKeyFactory
using Org.BouncyCastle.Asn1; using Org.BouncyCastle.Asn1.Cryptlib; using Org.BouncyCastle.Asn1.CryptoPro; using Org.BouncyCastle.Asn1.EdEC; using Org.BouncyCastle.Asn1.Gnu; 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.Pkcs; using System; using System.IO; namespace Org.BouncyCastle.Security { public static class PrivateKeyFactory { public static AsymmetricKeyParameter CreateKey(byte[] privateKeyInfoData) { return CreateKey(PrivateKeyInfo.GetInstance(Asn1Object.FromByteArray(privateKeyInfoData))); } public static AsymmetricKeyParameter CreateKey(Stream inStr) { return CreateKey(PrivateKeyInfo.GetInstance(Asn1Object.FromStream(inStr))); } public static AsymmetricKeyParameter CreateKey(PrivateKeyInfo keyInfo) { AlgorithmIdentifier privateKeyAlgorithm = keyInfo.PrivateKeyAlgorithm; DerObjectIdentifier algorithm = privateKeyAlgorithm.Algorithm; if (algorithm.Equals(PkcsObjectIdentifiers.RsaEncryption) || algorithm.Equals(X509ObjectIdentifiers.IdEARsa) || algorithm.Equals(PkcsObjectIdentifiers.IdRsassaPss) || algorithm.Equals(PkcsObjectIdentifiers.IdRsaesOaep)) { RsaPrivateKeyStructure instance = RsaPrivateKeyStructure.GetInstance(keyInfo.ParsePrivateKey()); return new RsaPrivateCrtKeyParameters(instance.Modulus, instance.PublicExponent, instance.PrivateExponent, instance.Prime1, instance.Prime2, instance.Exponent1, instance.Exponent2, instance.Coefficient); } if (algorithm.Equals(PkcsObjectIdentifiers.DhKeyAgreement)) { DHParameter instance2 = DHParameter.GetInstance(privateKeyAlgorithm.Parameters); DerInteger obj = (DerInteger)keyInfo.ParsePrivateKey(); return new DHPrivateKeyParameters(parameters: new DHParameters(l: instance2.L?.IntValue ?? 0, p: instance2.P, g: instance2.G, q: null), x: obj.Value, algorithmOid: algorithm); } if (algorithm.Equals(OiwObjectIdentifiers.ElGamalAlgorithm)) { ElGamalParameter instance3 = ElGamalParameter.GetInstance(privateKeyAlgorithm.Parameters); return new ElGamalPrivateKeyParameters(((DerInteger)keyInfo.ParsePrivateKey()).Value, new ElGamalParameters(instance3.P, instance3.G)); } if (algorithm.Equals(X9ObjectIdentifiers.IdDsa)) { DerInteger obj2 = (DerInteger)keyInfo.ParsePrivateKey(); Asn1Encodable parameters2 = privateKeyAlgorithm.Parameters; DsaParameters parameters3 = null; if (parameters2 != null) { DsaParameter instance4 = DsaParameter.GetInstance(parameters2.ToAsn1Object()); parameters3 = new DsaParameters(instance4.P, instance4.Q, instance4.G); } return new DsaPrivateKeyParameters(obj2.Value, parameters3); } if (algorithm.Equals(X9ObjectIdentifiers.IdECPublicKey)) { ECPrivateKeyStructure instance5 = ECPrivateKeyStructure.GetInstance(keyInfo.ParsePrivateKey()); X962Parameters instance6 = X962Parameters.GetInstance(privateKeyAlgorithm.Parameters.ToAsn1Object()); if (instance6.IsNamedCurve) return new ECPrivateKeyParameters("EC", instance5.GetKey(), DerObjectIdentifier.GetInstance(instance6.Parameters)); X9ECParameters instance7 = X9ECParameters.GetInstance(instance6.Parameters); return new ECPrivateKeyParameters("EC", instance5.GetKey(), new ECDomainParameters(instance7)); } if (algorithm.Equals(CryptoProObjectIdentifiers.GostR3410x2001) || algorithm.Equals(RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512) || algorithm.Equals(RosstandartObjectIdentifiers.id_tc26_gost_3410_12_256)) { Asn1Object asn1Object = privateKeyAlgorithm.Parameters.ToAsn1Object(); Gost3410PublicKeyAlgParameters instance8 = Gost3410PublicKeyAlgParameters.GetInstance(asn1Object); Asn1Sequence asn1Sequence = asn1Object as Asn1Sequence; ECGost3410Parameters dp; BigInteger d; if (asn1Sequence != null && (asn1Sequence.Count == 2 || asn1Sequence.Count == 3)) { X9ECParameters byOid = ECGost3410NamedCurves.GetByOid(instance8.PublicKeyParamSet); if (byOid == null) throw new ArgumentException("Unrecognized curve OID for GostR3410x2001 private key"); dp = new ECGost3410Parameters(new ECNamedDomainParameters(instance8.PublicKeyParamSet, byOid), instance8.PublicKeyParamSet, instance8.DigestParamSet, instance8.EncryptionParamSet); int privateKeyLength = keyInfo.PrivateKeyLength; if (privateKeyLength == 32 || privateKeyLength == 64) d = new BigInteger(1, keyInfo.PrivateKey.GetOctets(), false); else { Asn1Object asn1Object2 = keyInfo.ParsePrivateKey(); DerInteger derInteger = asn1Object2 as DerInteger; if (derInteger != null) d = derInteger.PositiveValue; else { byte[] octets = Asn1OctetString.GetInstance(asn1Object2).GetOctets(); d = new BigInteger(1, octets, false); } } } else { X962Parameters instance9 = X962Parameters.GetInstance(asn1Object); if (instance9.IsNamedCurve) { DerObjectIdentifier instance10 = DerObjectIdentifier.GetInstance(instance9.Parameters); X9ECParameters byOid2 = ECNamedCurveTable.GetByOid(instance10); if (byOid2 == null) throw new ArgumentException("Unrecognized curve OID for GostR3410x2001 private key"); dp = new ECGost3410Parameters(new ECNamedDomainParameters(instance10, byOid2), instance8.PublicKeyParamSet, instance8.DigestParamSet, instance8.EncryptionParamSet); } else if (instance9.IsImplicitlyCA) { dp = null; } else { X9ECParameters instance11 = X9ECParameters.GetInstance(instance9.Parameters); dp = new ECGost3410Parameters(new ECNamedDomainParameters(algorithm, instance11), instance8.PublicKeyParamSet, instance8.DigestParamSet, instance8.EncryptionParamSet); } Asn1Object asn1Object3 = keyInfo.ParsePrivateKey(); DerInteger derInteger2 = asn1Object3 as DerInteger; d = ((derInteger2 == null) ? ECPrivateKeyStructure.GetInstance(asn1Object3).GetKey() : derInteger2.Value); } return new ECPrivateKeyParameters(d, new ECGost3410Parameters(dp, instance8.PublicKeyParamSet, instance8.DigestParamSet, instance8.EncryptionParamSet)); } if (algorithm.Equals(CryptoProObjectIdentifiers.GostR3410x94)) { Gost3410PublicKeyAlgParameters instance12 = Gost3410PublicKeyAlgParameters.GetInstance(privateKeyAlgorithm.Parameters); Asn1Object asn1Object4 = keyInfo.ParsePrivateKey(); BigInteger x = (!(asn1Object4 is DerInteger)) ? new BigInteger(1, Asn1OctetString.GetInstance(asn1Object4).GetOctets(), false) : DerInteger.GetInstance(asn1Object4).PositiveValue; return new Gost3410PrivateKeyParameters(x, instance12.PublicKeyParamSet); } if (algorithm.Equals(EdECObjectIdentifiers.id_X25519) || algorithm.Equals(CryptlibObjectIdentifiers.curvey25519)) { if (X25519PrivateKeyParameters.KeySize == keyInfo.PrivateKeyLength) return new X25519PrivateKeyParameters(keyInfo.PrivateKey.GetOctets()); return new X25519PrivateKeyParameters(GetRawKey(keyInfo)); } if (algorithm.Equals(EdECObjectIdentifiers.id_X448)) { if (X448PrivateKeyParameters.KeySize == keyInfo.PrivateKeyLength) return new X448PrivateKeyParameters(keyInfo.PrivateKey.GetOctets()); return new X448PrivateKeyParameters(GetRawKey(keyInfo)); } if (algorithm.Equals(EdECObjectIdentifiers.id_Ed25519) || algorithm.Equals(GnuObjectIdentifiers.Ed25519)) return new Ed25519PrivateKeyParameters(GetRawKey(keyInfo)); if (algorithm.Equals(EdECObjectIdentifiers.id_Ed448)) return new Ed448PrivateKeyParameters(GetRawKey(keyInfo)); if (algorithm.Equals(RosstandartObjectIdentifiers.id_tc26_gost_3410_12_256) || algorithm.Equals(RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512) || algorithm.Equals(RosstandartObjectIdentifiers.id_tc26_agreement_gost_3410_12_256) || algorithm.Equals(RosstandartObjectIdentifiers.id_tc26_agreement_gost_3410_12_512)) { Gost3410PublicKeyAlgParameters instance13 = Gost3410PublicKeyAlgParameters.GetInstance(keyInfo.PrivateKeyAlgorithm.Parameters); Asn1Object asn1Object5 = keyInfo.PrivateKeyAlgorithm.Parameters.ToAsn1Object(); ECGost3410Parameters dp2; BigInteger d2; if (asn1Object5 is Asn1Sequence && (Asn1Sequence.GetInstance(asn1Object5).Count == 2 || Asn1Sequence.GetInstance(asn1Object5).Count == 3)) { X9ECParameters byOid3 = ECGost3410NamedCurves.GetByOid(instance13.PublicKeyParamSet); dp2 = new ECGost3410Parameters(new ECNamedDomainParameters(instance13.PublicKeyParamSet, byOid3), instance13.PublicKeyParamSet, instance13.DigestParamSet, instance13.EncryptionParamSet); int privateKeyLength2 = keyInfo.PrivateKeyLength; if (privateKeyLength2 == 32 || privateKeyLength2 == 64) d2 = new BigInteger(1, keyInfo.PrivateKey.GetOctets(), false); else { Asn1Encodable asn1Encodable = keyInfo.ParsePrivateKey(); if (asn1Encodable is DerInteger) d2 = DerInteger.GetInstance(asn1Encodable).PositiveValue; else { byte[] octets2 = Asn1OctetString.GetInstance(asn1Encodable).GetOctets(); d2 = new BigInteger(1, octets2, false); } } } else { X962Parameters instance14 = X962Parameters.GetInstance(keyInfo.PrivateKeyAlgorithm.Parameters); if (instance14.IsNamedCurve) { DerObjectIdentifier instance15 = DerObjectIdentifier.GetInstance(instance14.Parameters); X9ECParameters x2 = ECKeyPairGenerator.FindECCurveByOid(instance15); dp2 = new ECGost3410Parameters(new ECNamedDomainParameters(instance15, x2), instance13.PublicKeyParamSet, instance13.DigestParamSet, instance13.EncryptionParamSet); } else if (instance14.IsImplicitlyCA) { dp2 = null; } else { X9ECParameters instance16 = X9ECParameters.GetInstance(instance14.Parameters); dp2 = new ECGost3410Parameters(new ECNamedDomainParameters(algorithm, instance16), instance13.PublicKeyParamSet, instance13.DigestParamSet, instance13.EncryptionParamSet); } Asn1Encodable asn1Encodable2 = keyInfo.ParsePrivateKey(); d2 = ((!(asn1Encodable2 is DerInteger)) ? ECPrivateKeyStructure.GetInstance(asn1Encodable2).GetKey() : DerInteger.GetInstance(asn1Encodable2).Value); } return new ECPrivateKeyParameters(d2, new ECGost3410Parameters(dp2, instance13.PublicKeyParamSet, instance13.DigestParamSet, instance13.EncryptionParamSet)); } if (MLDsaParameters.ByOid.TryGetValue(algorithm, out MLDsaParameters value)) { Asn1OctetString privateKey = keyInfo.PrivateKey; int octetsLength = privateKey.GetOctetsLength(); MLDsaParameterSet parameterSet = value.ParameterSet; if (octetsLength == parameterSet.SeedLength) return MLDsaPrivateKeyParameters.FromSeed(value, privateKey.GetOctets()); if (octetsLength == parameterSet.PrivateKeyLength) return MLDsaPrivateKeyParameters.FromEncoding(value, privateKey.GetOctets()); throw new ArgumentException("invalid " + value.Name + " private key"); } if (MLKemParameters.ByOid.TryGetValue(algorithm, out MLKemParameters value2)) { Asn1OctetString privateKey2 = keyInfo.PrivateKey; int octetsLength2 = privateKey2.GetOctetsLength(); MLKemParameterSet parameterSet2 = value2.ParameterSet; if (octetsLength2 == parameterSet2.SeedLength) return MLKemPrivateKeyParameters.FromSeed(value2, privateKey2.GetOctets()); if (octetsLength2 == parameterSet2.PrivateKeyLength) return MLKemPrivateKeyParameters.FromEncoding(value2, privateKey2.GetOctets()); throw new ArgumentException("invalid " + value2.Name + " private key"); } if (SlhDsaParameters.ByOid.TryGetValue(algorithm, out SlhDsaParameters value3)) { Asn1OctetString privateKey3 = keyInfo.PrivateKey; int octetsLength3 = privateKey3.GetOctetsLength(); SlhDsaParameterSet parameterSet3 = value3.ParameterSet; if (octetsLength3 == parameterSet3.PrivateKeyLength) return SlhDsaPrivateKeyParameters.FromEncoding(value3, privateKey3.GetOctets()); throw new ArgumentException("invalid " + value3.Name + " private key"); } throw new SecurityUtilityException("algorithm identifier in private key not recognised"); } private static byte[] GetRawKey(PrivateKeyInfo keyInfo) { return Asn1OctetString.GetInstance(keyInfo.ParsePrivateKey()).GetOctets(); } public static AsymmetricKeyParameter DecryptKey(char[] passPhrase, EncryptedPrivateKeyInfo encInfo) { return CreateKey(PrivateKeyInfoFactory.CreatePrivateKeyInfo(passPhrase, encInfo)); } public static AsymmetricKeyParameter DecryptKey(char[] passPhrase, byte[] encryptedPrivateKeyInfoData) { return DecryptKey(passPhrase, Asn1Object.FromByteArray(encryptedPrivateKeyInfoData)); } public static AsymmetricKeyParameter DecryptKey(char[] passPhrase, Stream encryptedPrivateKeyInfoStream) { return DecryptKey(passPhrase, Asn1Object.FromStream(encryptedPrivateKeyInfoStream)); } private static AsymmetricKeyParameter DecryptKey(char[] passPhrase, Asn1Object asn1Object) { return DecryptKey(passPhrase, EncryptedPrivateKeyInfo.GetInstance(asn1Object)); } public static byte[] EncryptKey(DerObjectIdentifier algorithm, char[] passPhrase, byte[] salt, int iterationCount, AsymmetricKeyParameter key) { return EncryptedPrivateKeyInfoFactory.CreateEncryptedPrivateKeyInfo(algorithm, passPhrase, salt, iterationCount, key).GetEncoded(); } public static byte[] EncryptKey(string algorithm, char[] passPhrase, byte[] salt, int iterationCount, AsymmetricKeyParameter key) { return EncryptedPrivateKeyInfoFactory.CreateEncryptedPrivateKeyInfo(algorithm, passPhrase, salt, iterationCount, key).GetEncoded(); } } }