Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.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中使用iTextSharp在PDF中添加多个数字签名#_Itext_Digital Signature - Fatal编程技术网

在C中使用iTextSharp在PDF中添加多个数字签名#

在C中使用iTextSharp在PDF中添加多个数字签名#,itext,digital-signature,Itext,Digital Signature,我使用iTextSharp Dll实现了数字签名,用单个签名对PDF文件进行签名。现在,我想在以前或已经数字签名的PDF中添加另一个数字签名,但在验证一个签名时出现错误 如何在一个PDF中添加多个数字签名并验证所有签名 我正在使用以下代码: PdfReader reader = new PdfReader(fileName); using (FileStream fout = new FileStream(SignedFileName, FileMode.Create, FileAccess.R

我使用iTextSharp Dll实现了数字签名,用单个签名对PDF文件进行签名。现在,我想在以前或已经数字签名的PDF中添加另一个数字签名,但在验证一个签名时出现错误

如何在一个PDF中添加多个数字签名并验证所有签名

我正在使用以下代码:

PdfReader reader = new PdfReader(fileName);
using (FileStream fout = new FileStream(SignedFileName, FileMode.Create, FileAccess.ReadWrite))
{
    // appearance
    PdfStamper stamper = PdfStamper.CreateSignature(reader, fout, '\0');
    PdfSignatureAppearance appearance = stamper.SignatureAppearance;
    //appearance.Reason = SignReason;
    //appearance.Location = SignLocation;
    appearance.SignDate = DateTime.Now.Date;
    appearance.SetVisibleSignature(new iTextSharp.text.Rectangle(xPos, yPos, xPos + 200, yPos + 100), PageNo, null);//.IsInvisible

    // Custom text and background image
    appearance.Image = iTextSharp.text.Image.GetInstance(SignatureImg);
    appearance.ImageScale = 0.6f;
    appearance.Image.Alignment = 300;
    appearance.Acro6Layers = true;

    StringBuilder buf = new StringBuilder();
    buf.Append("Digitally Signed by ");
    String name = SignerName;

    buf.Append(name).Append('\n');
    buf.Append("Date: ").Append(DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss zzz"));

    string text = buf.ToString();

    appearance.Layer2Text = text;

    //digital signature
    IExternalSignature es = new PrivateKeySignature(pk, "SHA-256");
    MakeSignature.SignDetached(appearance, es, new Org.BouncyCastle.X509.X509Certificate[] { pk12.GetCertificate(alias).Certificate }, null, null, null, 0, CryptoStandard.CMS);

    stamper.Close();

}

错误在这一行:

PdfStamper stamper = PdfStamper.CreateSignature(reader, fout, '\0');
将其更改为:

PdfStamper stamper = PdfStamper.CreateSignature(reader, fout, '\0', true);
说明:您没有在附加模式下对文档进行签名


在进一步检查代码时,我发现您还添加了一个图像。这可能很棘手。在追加模式下添加新签名解决了一个问题。根据您使用的iText版本,添加额外的内容可能会导致额外的问题。

您是否尝试过使用不同参数运行适用于初始签名的代码示例?就像字段的新名称,一个新的边界框。向我们展示您的代码!第一个签名是否允许额外的数字签名?(如果添加额外签名,则级别为“不允许更改”的作者签名将中断!)是否在附加模式下添加了第二个签名?(如果您更改原始PDF中的字节,第一个签名将中断。)您的问题不完整,因此将投反对票。请先阅读,如果还有不清楚的地方,请修正你的问题。“以前或已经数字签名”-你说的“以前签名”和“已经签名”是不同的意思吗?如果是,请按照@Bruno@mkl-之前签名或已经签名的意思解释改进问题时的区别。。。使用itextsharp.dll或其他数字签名实用程序签署的任何PDF文件签署的PDF。好的,我撤回了否决票。您的问题是由“用户未阅读文档”引起的。请参阅我的答案以了解如何解决此问题。我已添加了PdfStamper stamper=PdfStamper.CreateSignature(reader,fout,'\0',null,true);在我的代码和它已被验证的签名成功。但它错过了我为签名添加的图像。。。请建议。如果您没有添加图像,则不会添加图像。对于这样一个不完整的问题,我不确定你希望得到什么样的回答。