PDF数字签名:使用iTextSharp自定义外观

时间:2016-08-16 03:23:51

标签: c# itext digital-signature

我正在尝试使用iTextSharp 5.5.9签署pdf文件。自定义外观时,我收到了一个难以理解的错误。我在这里做了什么

签名方法:

public void Sign1(String src, String name, String dest, ICollection<X509Certificate> chain, ICipherParameters pk, String digestAlgorithm, CryptoStandard subfilter, String reason, String location)
        {
            // Creating the reader and the stamper
            PdfReader reader = new PdfReader(src);
            FileStream os = new FileStream(dest, FileMode.Create);
            PdfStamper stamper = PdfStamper.CreateSignature(reader, os, '\0');
            // Creating the appearance
            PdfSignatureAppearance appearance = stamper.SignatureAppearance;
            appearance.Reason = reason;
            appearance.Location = location;
            appearance.SetVisibleSignature(name);
            // Custom text and custom font
            appearance.Layer2Text = "This document was signed by ABC";
            appearance.Layer2Font = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN);
            // Creating the signature
            IExternalSignature pks = new PrivateKeySignature(pk, digestAlgorithm);
            MakeSignature.SignDetached(appearance, pks, chain, null, null, null, 0, subfilter);
        }

和我签名的方式

String KEYSTORE = "C:/Users/user/Desktop/temp.pfx";
char[] PASSWORD = "blabla".ToCharArray();
String SRC = "C:/Users/user/abc.pdf";
String DEST = "C:/Users/user/Desktop/aaa.pdf";
 Pkcs12Store store = new Pkcs12Store(new FileStream(KEYSTORE, FileMode.Open), PASSWORD);
            String alias = "";
            ICollection<X509Certificate> chain = new List<X509Certificate>();
            // 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;
Sign1(SRC, "Signature1", String.Format(DEST, 1), chain, parameters, DigestAlgorithms.SHA256, CryptoStandard.CMS, "Customize apprearance", "Blabla");

我收到的消息: message

如何解决这个问题?

0 个答案:

没有答案