using System; using System.Collections.Generic; using System.Text; using Org.BouncyCastle.Crypto; using iTextSharp.text.pdf; using Org.BouncyCastle.Pkcs; using System.IO; using Spring.Core.IO; namespace Sleis.Pdf { public class SimpleCert { public AsymmetricKeyParameter Akp { get; private set; } public Org.BouncyCastle.X509.X509Certificate[] Chain { get; private set; } public SimpleCert(IResource res, string password) { if (res == null || !res.File.Exists) { throw new FileNotFoundException("Input does not exist: " + res.File.FullName); } Stream fs = new FileStream(res.File.FullName, FileMode.Open, FileAccess.Read); Pkcs12Store pk12 = new Pkcs12Store(fs, password.ToCharArray()); //then Iterate throught certificate entries to find the private key entry string alias = null; foreach (string al in pk12.Aliases) { if (pk12.IsKeyEntry(al) && pk12.GetKey(al).Key.IsPrivate) { alias = al; break; } } fs.Close(); Akp = pk12.GetKey(alias).Key; X509CertificateEntry[] ce = pk12.GetCertificateChain(alias); Chain = new Org.BouncyCastle.X509.X509Certificate[ce.Length]; for (int k = 0; k < ce.Length; ++k) { Chain[k] = ce[k].Certificate; } } } }