Пример #1
0
        /// <exception cref="Org.BouncyCastle.Security.GeneralSecurityException"/>
        /// <exception cref="System.IO.IOException"/>
        protected internal virtual void Sign(String src, String name, String dest, X509Certificate
                                             [] chain, ICipherParameters pk, String digestAlgorithm, PdfSigner.CryptoStandard
                                             subfilter, String reason, String location, Rectangle rectangleForNewField, bool
                                             setReuseAppearance)
        {
            PdfReader reader = new PdfReader(src);
            PdfSigner signer = new PdfSigner(reader, new FileStream(dest, FileMode.Create), false
                                             );
            // Creating the appearance
            PdfSignatureAppearance appearance = signer.GetSignatureAppearance().SetReason(reason
                                                                                          ).SetLocation(location).SetReuseAppearance(setReuseAppearance);

            if (rectangleForNewField != null)
            {
                appearance.SetPageRect(rectangleForNewField);
            }
            signer.SetFieldName(name);
            // Creating the signature
            IExternalSignature pks = new PrivateKeySignature(pk, digestAlgorithm);

            signer.SignDetached(pks, chain, null, null, null, 0, subfilter);
        }
Пример #2
0
        public byte[] GenerirajPotpisaniDokument(byte[] document, string documentName, string signingReason, string signingLocation)
        {
            try
            {
                string alias = "";
                List <Org.BouncyCastle.X509.X509Certificate> chain = new List <Org.BouncyCastle.X509.X509Certificate>();

                var location = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, AppDomain.CurrentDomain.RelativeSearchPath ?? "");
                //var destinationPath = $@"Data\{documentName}";
                //destinationPath = Path.Combine(location, destinationPath);
                var destinationPath = iConfig["DocumentDestinationPath"] + documentName;


                var certPath = @"C:\Users\Crega\Source\Repos\PdfSigner\PdfSigner\bin\Debug\netcoreapp3.0\certifikat.pfx";
                certPath = Path.Combine(location, certPath);
                var    cert       = File.ReadAllBytes(certPath);
                Stream certStream = new MemoryStream(cert);

                Pkcs12Store store = new Pkcs12Store(certStream, "12345".ToCharArray());

                // searching for private key
                foreach (string al in store.Aliases)
                {
                    if (store.IsKeyEntry(al) && store.GetKey(al).Key.IsPrivate)
                    {
                        alias = al;
                        break;
                    }
                }
                AsymmetricKeyEntry pk = store.GetKey(alias);
                foreach (X509CertificateEntry c in store.GetCertificateChain(alias))
                {
                    chain.Add(c.Certificate);
                }
                RsaPrivateCrtKeyParameters parameters = pk.Key as RsaPrivateCrtKeyParameters;

                Stream                     documentStream = new MemoryStream(document);
                PdfReader                  reader         = new PdfReader(documentStream);
                FileStream                 os             = new FileStream(destinationPath, FileMode.Create);
                StampingProperties         sp             = new StampingProperties();
                iText.Signatures.PdfSigner signer         = new iText.Signatures.PdfSigner(reader, os, sp);
                PdfSignatureAppearance     appearance     = signer.GetSignatureAppearance();
                appearance.SetReason(signingReason);
                appearance.SetLocation(signingLocation);
                appearance.SetContact("*****@*****.**");
                //appearance.SetPageRect(new iText.Kernel.Geom.Rectangle(36, 748, 144, 780));
                appearance.SetPageNumber(1);
                appearance.SetCertificate(chain[0]);

                // Creating the signature
                IExternalSignature pks = new PrivateKeySignature(parameters, DigestAlgorithms.SHA256);
                signer.SignDetached(pks, chain.ToArray(), null, null, null, 0, iText.Signatures.PdfSigner.CryptoStandard.CADES);
                //MakeSignature.SignDetached(appearance, pks, chain, null, null, null, 0, PdfSigner.CryptoStandard.CADES);
                os.Close();
                certStream.Close();
                var documentToReturn = File.ReadAllBytes(destinationPath);
                File.Delete(destinationPath);
                return(documentToReturn);
            }
            catch (Exception ex)
            {
            }
            return(null);
        }