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

Pkcs12Utilities

public class Pkcs12Utilities
using Org.BouncyCastle.Asn1; using Org.BouncyCastle.Asn1.Pkcs; using Org.BouncyCastle.Asn1.X509; using System; using System.IO; namespace Org.BouncyCastle.Pkcs { public class Pkcs12Utilities { public static byte[] ConvertToDefiniteLength(byte[] berPkcs12File) { return Pfx.GetInstance(berPkcs12File).GetEncoded("DER"); } public static byte[] ConvertToDefiniteLength(byte[] berPkcs12File, char[] passwd) { Pfx instance = Pfx.GetInstance(berPkcs12File); ContentInfo authSafe = instance.AuthSafe; Asn1Object asn1Object = Asn1Object.FromByteArray(Asn1OctetString.GetInstance(authSafe.Content).GetOctets()); authSafe = new ContentInfo(authSafe.ContentType, new DerOctetString(asn1Object.GetEncoded("DER"))); MacData macData = instance.MacData; try { int intValue = macData.IterationCount.IntValue; byte[] octets = Asn1OctetString.GetInstance(authSafe.Content).GetOctets(); byte[] digest = Pkcs12Store.CalculatePbeMac(macData.Mac.AlgorithmID.Algorithm, macData.GetSalt(), intValue, passwd, false, octets); macData = new MacData(new DigestInfo(new AlgorithmIdentifier(macData.Mac.AlgorithmID.Algorithm, DerNull.Instance), digest), macData.GetSalt(), intValue); } catch (Exception ex) { throw new IOException("error constructing MAC: " + ex.ToString()); } return new Pfx(authSafe, macData).GetEncoded("DER"); } } }