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

EncryptedPrivateKeyInfoFactory

public sealed class EncryptedPrivateKeyInfoFactory
using Org.BouncyCastle.Asn1; using Org.BouncyCastle.Asn1.Pkcs; using Org.BouncyCastle.Asn1.X509; using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Security; using System; namespace Org.BouncyCastle.Pkcs { public sealed class EncryptedPrivateKeyInfoFactory { private EncryptedPrivateKeyInfoFactory() { } public static EncryptedPrivateKeyInfo CreateEncryptedPrivateKeyInfo(DerObjectIdentifier algorithm, char[] passPhrase, byte[] salt, int iterationCount, AsymmetricKeyParameter key) { return CreateEncryptedPrivateKeyInfo(algorithm.Id, passPhrase, salt, iterationCount, PrivateKeyInfoFactory.CreatePrivateKeyInfo(key)); } public static EncryptedPrivateKeyInfo CreateEncryptedPrivateKeyInfo(string algorithm, char[] passPhrase, byte[] salt, int iterationCount, AsymmetricKeyParameter key) { return CreateEncryptedPrivateKeyInfo(algorithm, passPhrase, salt, iterationCount, PrivateKeyInfoFactory.CreatePrivateKeyInfo(key)); } public static EncryptedPrivateKeyInfo CreateEncryptedPrivateKeyInfo(string algorithm, char[] passPhrase, byte[] salt, int iterationCount, PrivateKeyInfo keyInfo) { IBufferedCipher obj = PbeUtilities.CreateEngine(algorithm) as IBufferedCipher; if (obj == null) throw new Exception("Unknown encryption algorithm: " + algorithm); Asn1Encodable asn1Encodable = PbeUtilities.GenerateAlgorithmParameters(algorithm, salt, iterationCount); ICipherParameters parameters = PbeUtilities.GenerateCipherParameters(algorithm, passPhrase, asn1Encodable); obj.Init(true, parameters); byte[] contents = obj.DoFinal(keyInfo.GetEncoded()); AlgorithmIdentifier encryptionAlgorithm = new AlgorithmIdentifier(PbeUtilities.GetObjectIdentifier(algorithm), asn1Encodable); DerOctetString encryptedData = DerOctetString.WithContents(contents); return new EncryptedPrivateKeyInfo(encryptionAlgorithm, encryptedData); } public static EncryptedPrivateKeyInfo CreateEncryptedPrivateKeyInfo(DerObjectIdentifier cipherAlgorithm, DerObjectIdentifier prfAlgorithm, char[] passPhrase, byte[] salt, int iterationCount, SecureRandom random, AsymmetricKeyParameter key) { return CreateEncryptedPrivateKeyInfo(cipherAlgorithm, prfAlgorithm, passPhrase, salt, iterationCount, random, PrivateKeyInfoFactory.CreatePrivateKeyInfo(key)); } public static EncryptedPrivateKeyInfo CreateEncryptedPrivateKeyInfo(DerObjectIdentifier cipherAlgorithm, DerObjectIdentifier prfAlgorithm, char[] passPhrase, byte[] salt, int iterationCount, SecureRandom random, PrivateKeyInfo keyInfo) { IBufferedCipher cipher = CipherUtilities.GetCipher(cipherAlgorithm); if (cipher == null) throw new Exception("Unknown encryption algorithm: " + cipherAlgorithm?.ToString()); Asn1Encodable asn1Encodable = PbeUtilities.GenerateAlgorithmParameters(cipherAlgorithm, prfAlgorithm, salt, iterationCount, random); ICipherParameters parameters = PbeUtilities.GenerateCipherParameters(PkcsObjectIdentifiers.IdPbeS2, passPhrase, asn1Encodable); cipher.Init(true, parameters); byte[] contents = cipher.DoFinal(keyInfo.GetEncoded()); AlgorithmIdentifier encryptionAlgorithm = new AlgorithmIdentifier(PkcsObjectIdentifiers.IdPbeS2, asn1Encodable); DerOctetString encryptedData = DerOctetString.WithContents(contents); return new EncryptedPrivateKeyInfo(encryptionAlgorithm, encryptedData); } } }