PDF数字签名禁用CRL

时间:2018-08-29 12:08:15

标签: c#-4.0 itext

我正在使用itextsharp.dll编写示例pdf签名器,因为数字签名在USB设备上。

唯一有效的问题是有时签名会完全停止几个小时。

我发现签名过程进入数字发行者站点,以使CRL嵌入已签名的文档中,从而将其大小从100kb增加到1300kb +,并且由于从发行者站点获得CRL,签名文件也非常缓慢。

但是,如果我禁用Internet,则文件签名很快,大小增加仅为5-10kb。

所以我的问题恰恰是我该如何指示签名过程不联机以获取CRL,n只需对文档进行快速签名,而原始文档的大小却减少。

public void Sign(ICollection<X509Certificate> chain, X509Certificate2 pk,
                    String digestAlgorithm, CryptoStandard subfilter,
                    String reason, String location,
                    int estimatedSize)
    {

        IList<ICrlClient> crlList = new List<ICrlClient>();
        crlList.Add(new CrlClientOnline(chain));


        // Creating the reader and the stamper
        PdfReader reader = null;
        PdfStamper stamper = null;
        FileStream os = null;
        //int noofpdfs = 0;
        var watch = Stopwatch.StartNew();
        try
        {
            string[] srcfiles;
            if (lblSplitedFilePath.Text.Equals("Split By Page Directory"))
                srcfiles = Directory.GetFiles(tbsrcpath.Text, "*.pdf");
            else
                if (tbsrcpath.Text.ToUpper().Contains(@"\SPLIT"))
                srcfiles = Directory.GetFiles(tbsrcpath.Text, "*.pdf");
            else
                srcfiles = Directory.GetFiles(tbsrcpath.Text + @"\split\", "*.pdf");

            //int noofpdfs = srcfilePaths.Count();
            for (int i = 0; i < srcfiles.Count(); i++)
            {
                // Get FileName
                lblOutputFile.Text = tbtgtpath.Text + @"\" + Path.GetFileName(srcfiles[i]);
                lblOutputFile.Refresh();
                os = new FileStream(lblOutputFile.Text, FileMode.Create);
                reader = new PdfReader(srcfiles[i]);
                iTextSharp.text.Rectangle pdfbox = reader.GetPageSize(1);
                stamper = PdfStamper.CreateSignature(reader, os, '\0');
                // Creating the appearance
                PdfSignatureAppearance appearance = stamper.SignatureAppearance;
                if (!string.IsNullOrWhiteSpace(tbContact.Text))
                {
                    appearance.ReasonCaption = "Contact:";
                    appearance.Reason = tbContact.Text;// reason;
                }
                appearance.Location = location;


                //Adding Image to Sign
                if (cbAddImageSign.Checked)
                {
                    var image = iTextSharp.text.Image.GetInstance(tbSignImage.Text);
                    appearance.Acro6Layers = true;
                    appearance.SignatureGraphic = image;
                    appearance.SignatureRenderingMode = PdfSignatureAppearance.RenderingMode.GRAPHIC_AND_DESCRIPTION;
                }


                int llx, lly, urx, ury;
                int.TryParse(Tbllx.Text, out llx);
                int.TryParse(tblly.Text, out lly);
                int.TryParse(tburx.Text, out urx);
                int.TryParse(tbury.Text, out ury);
                pdfSigning.Properties.Settings.Default.llx = llx;
                pdfSigning.Properties.Settings.Default.lly = lly;
                pdfSigning.Properties.Settings.Default.urx = urx;
                pdfSigning.Properties.Settings.Default.ury = ury;
                appearance.SetVisibleSignature(new iTextSharp.text.Rectangle(llx, lly, urx, ury), 1, "sig");


                //Add Water mark
                if (!lblWaterMarkImagePath.Text.Equals("Pdf Water Mark Image Path"))
                {
                    var wmimage = iTextSharp.text.Image.GetInstance(lblWaterMarkImagePath.Text);
                    wmimage.SetAbsolutePosition(0, 0);
                    wmimage.ScaleToFit(100, 100);
                    for (var j = 0; j < reader.NumberOfPages; j++)
                    {
                        var content = stamper.GetUnderContent(j + 1);
                        content.AddImage(wmimage);
                    }
                }
                //appearance.setRenderingMode(PdfSignatureAppearance.RenderingMode.GRAPHIC);
                // Creating the signature

                try
                {
                    IExternalSignature pks = new X509Certificate2Signature(pk, digestAlgorithm);
                    MakeSignature.SignDetached(appearance, pks, chain, crlList, null, null, estimatedSize,
                                               subfilter);

                }
                catch (CryptographicException ex)
                {

                    MessageBox.Show(ex.ToString());
                }

                //noofpdfs++;
                if (cbPrintOnSign.Checked)
                {
                    switch (tbPrintMethod.Text)
                    {
                        case "2":
                            SendFileToPrinter(lblOutputFile.Text, printpdf2printer);
                            break;
                        default:
                            SendToPrinter(lblOutputFile.Text);
                            break;
                    }

                }
                if (cbDeletePdfPostSign.Checked)
                {
                    File.Delete(srcfiles[i]);
                }
                dgvPrintFiles.Rows.Add(srcfiles[i].ToString());

            }
            lblOutputFile.Text += @" Siging Over:Signed " + srcfiles.Count().ToString() + " Files";
        }
        finally
        {
            if (reader != null)
                reader.Close();
            if (stamper != null)
                stamper.Close();
            if (os != null)
                os.Close();
        }
        watch.Stop();
        var elapsedMs = watch.ElapsedMilliseconds;
        MessageBox.Show("Signing Time:" + elapsedMs / 1000 + " Second");
    }

0 个答案:

没有答案