1

我正在开发一个 C# 应用程序来验证 pdf 文档。我已经完成了认证过程,但现在我需要获取存储在智能卡中的签名图像并将其放入给定的 PDF 文件中。用于获取证书的代码是这样的:

public static X509Certificate2 GetCertificate()
    {
        X509Store st = new X509Store(StoreName.My, StoreLocation.CurrentUser);
        st.Open(OpenFlags.ReadOnly);
        X509Certificate2Collection col = st.Certificates;
        X509Certificate2 card = null;
        X509Certificate2Collection sel = X509Certificate2UI.SelectFromCollection(col, "Certificates", "Select one to sign", X509SelectionFlag.SingleSelection);
        if (sel.Count > 0)
        {
            X509Certificate2Enumerator en = sel.GetEnumerator();
            en.MoveNext();
            card = en.Current;
        }
        st.Close();
        return card;
    }

然后我将所选的应用到此处的文档中:

            if(card == null)
            {
            ChooseCertified:
                card = GetCertificate();

                if (card == null)
                {
                    DialogResult result = MessageBox.Show("Unable to identify certificates" +
                                           Environment.NewLine + "Would you like to try again?", "Certification error", MessageBoxButtons.RetryCancel, MessageBoxIcon.Question);

                    if (result.CompareTo(DialogResult.Retry) == 0)
                        goto ChooseCertified;
                    else
                        return 1;
                }
            }

            X509CertificateParser cp = new X509CertificateParser(card.RawData);
            org.bouncycastle.x509.X509Certificate[] chain = new org.bouncycastle.x509.X509Certificate[] { cp.ReadCertificate() };

            PdfReader reader = new PdfReader(original_pdf_file);

            PdfStamper stp = PdfStamper.CreateSignature(reader, new FileStream(result_pdf_file, FileMode.Create), '\0', null, true);

            PdfSignatureAppearance sap = stp.SignatureAppearance;

            sap.Layer2Text = "Document signed by " + Environment.NewLine +
                                card.GetNameInfo(X509NameType.SimpleName, false) + Environment.NewLine +
                                "Date: " + date.ToString("dd-MM-yyyy") + Environment.NewLine +
                                "Reason: " + REASON;        
            iTextSharp.text.Rectangle rct = reader.GetPageSizeWithRotation(1);
            float lowerx = 0, upperx = 0, lowery = 0, uppery = 0;
            if (valign == "top")
            {
                lowery = (rct.Height - height) - vert_margin;
                uppery = rct.Height - vert_margin;
            }
            else
            {
                lowery = vert_margin;
                uppery = vert_margin + height;
            }
            if (halign == "left")
            {
                lowerx = hor_margin;
                upperx = hor_margin + width;
            }
            else
            {
                lowerx = (rct.Width - width) - hor_margin;
                upperx = rct.Width - hor_margin;
            }
            sap.SetVisibleSignature(new iTextSharp.text.Rectangle(lowerx, lowery, upperx, uppery),1, null);
            sap.SignDate = date;
            sap.SetCrypto(null, chain, null, null);
            sap.Reason = REASON;
            sap.Location = LOCATION;
            iTextSharp.text.Font f = new iTextSharp.text.Font(iTextSharp.text.Font.HELVETICA, 7, 1, iTextSharp.text.Color.BLACK);
            f.SetStyle("Bold");
            sap.Layer2Font = f;
            sap.Acro6Layers = true;

            PdfSignature dic = new PdfSignature(PdfName.ADOBE_PPKMS, PdfName.ADBE_PKCS7_SHA1);
            dic.Date = new PdfDate(date);
            dic.Name = PdfPKCS7.GetSubjectFields(chain[0]).GetField("CN");
            dic.Reason = REASON;
            dic.Location = LOCATION;
            sap.CryptoDictionary = dic;
            int csize = 4000;
            Hashtable exc = new Hashtable();
            exc[PdfName.CONTENTS] = csize * 2 + 2;
            sap.PreClose(exc);

            HashAlgorithm sha = new SHA1CryptoServiceProvider();
            Stream s = sap.RangeStream;
            int read = 0;
            byte[] buff = new byte[8192];
            while ((read = s.Read(buff, 0, 8192)) > 0)
            {
                sha.TransformBlock(buff, 0, read, buff, 0);
            }
            sha.TransformFinalBlock(buff, 0, 0);

            byte[] pk = SignMsg(sha.Hash, card, false);

            byte[] outc = new byte[csize];

            PdfDictionary dic2 = new PdfDictionary();

            Array.Copy(pk, 0, outc, 0, pk.Length);

            dic2.Put(PdfName.CONTENTS, new PdfString(outc).SetHexWriting(true));
            sap.Close(dic2);

所以我现在需要的是一种获取存储在卡上的签名图像的方法,经过大量谷歌搜索后,我没有发现任何关于这件事的信息。如果有人可以帮助我,我真的很感激。

提前致谢 :)

4

0 回答 0