Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用代码进行数字签名的PDF中缺少签名验证tic_C#_Pdf_Itext - Fatal编程技术网

C# 使用代码进行数字签名的PDF中缺少签名验证tic

C# 使用代码进行数字签名的PDF中缺少签名验证tic,c#,pdf,itext,C#,Pdf,Itext,我正在使用ItextSharp对PDF文件应用数字签名。使用下面的代码,我可以将签名应用于PDF文件,也可以验证签名,但PDF文件的签名框中缺少签名验证标记 代码iTextSharp-(运行时版本=v2.0.50727)(版本=5.5.10.0) 输出文件 在上图中,绿色的tic标记附在签名框上,表示签名有效 任何人都知道我在使用iTextSharp 5.5.10.0时为什么缺少验证tic,我在这里缺少什么。您要求的东西违反了最新的标准。请允许我引用PAdES-6 aka(参见第6章的介绍):

我正在使用ItextSharp对PDF文件应用数字签名。使用下面的代码,我可以将签名应用于PDF文件,也可以验证签名,但PDF文件的签名框中缺少签名验证标记

代码iTextSharp-(运行时版本=v2.0.50727)(版本=5.5.10.0) 输出文件 在上图中,绿色的tic标记附在签名框上,表示签名有效


任何人都知道我在使用
iTextSharp 5.5.10.0
时为什么缺少验证tic,我在这里缺少什么。

您要求的东西违反了最新的标准。请允许我引用PAdES-6 aka(参见第6章的介绍):

一致性签名处理程序不得显示签名结果 页面内容内的签名验证

注:一致性签名处理程序将使用离页显示来显示 验证结果

在PAdES-6标准中,粗体字也是粗体字。当官方标准使用“不应该”一词时,规范告诉您不应该做某事,但如果您无论如何都这样做了,您就没有违反标准。但是,当官方标准使用“不得”一词时;规范告诉你不能做什么,因为如果你做了,你就违反了标准

简而言之:您使用iTextSharp 3.1.0.0创建的签名不再是一致性签名。许多年前,它们还可以,但今天,这些签名不再有效。我们多次警告不要使用旧的iText和iTextSharp版本来签署PDF文档(请参阅其他内容:)

您现在似乎想要一个与当前标准(PAdES和ISO-32000-2)保持最新的iTextSharp版本,以创建根据这些标准无效的签名。我希望你明白这是完全错误的


我还希望您理解您共享的代码iTextSharp 3.1.0.0示例创建了一个使用不推荐算法的签名。如果该代码仍在使用中,您应该通知您的客户,他使用该代码签署的所有文档都不再安全。

此外,即使是旧标准ISO 32000-1也没有指定任何此类“验证标记”,它们是Adobe在没有PDF标准的旧时代使用的不推荐功能。另见。我很想在这里把这个问题标记为该问题的副本,尽管它是在PDFBox的上下文中提出的。谢谢@mkl为您提供了指向先前答案的指针,我将这个问题作为副本来处理。
using Org.BouncyCastle.Pkcs;
using System.IO;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.security;

public class DigitalSignaturePDF
{
    public void DigiSignPdf(string sourceDocument,
        string destinationPath,
        Stream privateKeyStream,
        string keyPassword,
        string reason,
        string location,
        bool isVisibleSignature)
    {
        Pkcs12Store pk12 = new Pkcs12Store(privateKeyStream, keyPassword.ToCharArray());
        privateKeyStream.Dispose();

        //then Iterate throught certificate entries to find the private key entry
        string alias = null;
        foreach (string tAlias in pk12.Aliases)
        {
            if (pk12.IsKeyEntry(tAlias))
            {
                alias = tAlias;
                break;
            }
        }
        var pk = pk12.GetKey(alias).Key;

        // reader and stamper
        PdfReader reader = new PdfReader(sourceDocument);

        using (FileStream fout = new FileStream(destinationPath, FileMode.Append, FileAccess.Write))
        {
            using (PdfStamper stamper = PdfStamper.CreateSignature(reader, fout, '\0'))
            {
                // appearance
                PdfSignatureAppearance appearance = stamper.SignatureAppearance;
                //appearance.Image = new iTextSharp.text.pdf.PdfImage();
                appearance.Reason = reason;
                appearance.Location = location;
                if (isVisibleSignature)
                {
                    appearance.SetVisibleSignature(new iTextSharp.text.Rectangle(20, 10, 170, 60), reader.NumberOfPages, null);
                }
                //Get all certificate for validation
                X509CertificateEntry[] ce = pk12.GetCertificateChain(alias);
                Org.BouncyCastle.X509.X509Certificate[] chain;
                chain = new Org.BouncyCastle.X509.X509Certificate[ce.Length];
                for (int k = 0; k < ce.Length; ++k)
                {
                    chain[k] = ce[k].Certificate;
                }

                // digital signature
                IExternalSignature es = new PrivateKeySignature(pk, "SHA-256");
                MakeSignature.SignDetached(appearance, es, chain, null, null, null, 0, CryptoStandard.CMS);

                stamper.Close();
            }
        }
        reader.Close();
        reader.Dispose();
    }
}
public void Sign(string SigReason, string SigContact, string SigLocation, bool visible)
    {
        PdfReader reader = new PdfReader(this.inputPDF);
        PdfStamper st = PdfStamper.CreateSignature(reader, new FileStream(this.outputPDF, FileMode.Create, FileAccess.Write), '\0', null, true);
        PdfSignatureAppearance sap = st.SignatureAppearance;
        sap.SetCrypto(this.myCert.Akp, this.myCert.Chain, null, PdfSignatureAppearance.SELF_SIGNED);
        sap.Reason = SigReason;
        sap.Contact = SigContact;
        sap.Location = SigLocation;
        sap.SetVisibleSignature(new iTextSharp.text.Rectangle(100, 100, 25, 15), 1, null);
        st.Close();
    }