【问题标题】:how to password protect the digitally signed pdf using iTextSharp?如何使用 iTextSharp 对数字签名的 pdf 进行密码保护?
【发布时间】:2012-09-22 06:57:58
【问题描述】:

我正在使用 c#、itextsharp 创建和签名 pdf。现在我正在使用此代码进行密码保护和数字签名。首先我使用密码进行保护。然后我正在签名。

传输的pdf在打开时没有询问密码? 谁能告诉我为什么会这样?

谢谢..

string passprotectedfile = filename;

using (Stream input = new FileStream(signedfile, FileMode.Open, FileAccess.Read,
                                     FileShare.Read))
{
    using (Stream output = new FileStream(passprotectedfile, FileMode.Create, 
                                          FileAccess.Write, FileShare.None))
    {
        PdfReader reader = new PdfReader(input);
        PdfEncryptor.Encrypt(reader, output, true, regno.ToString(), "",
                             PdfWriter.ALLOW_SCREENREADERS);
    }
}

我用于数字签名的代码。

        PdfReader reader = new PdfReader(filename,pass);
        Stream output = new FileStream(signedfile, FileMode.Create, FileAccess.Write, FileShare.None);
        PdfStamper stamper = PdfStamper.CreateSignature(reader, output, '\0');

        Rectangle rect = new Rectangle(455, 105, 555, 170);
        PdfSignatureAppearance appearance = stamper.SignatureAppearance;
        appearance.SetVisibleSignature(rect, 1, "sign");
        PdfSignature dic = new PdfSignature(PdfName.ADOBE_PPKLITE, new PdfName("adobe.pkcs7.detached"));
        PrivateKeySignature pks = new PrivateKeySignature(pk, "SHA-256");
        MakeSignature.SignDetached(appearance, pks, chain, null, null, null, 0, true);
        return filename;

那么我正在传输。

            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=tes2.pdf");
            Response.TransmitFile(signedfile);
            Response.Flush();

            File.Delete(signedfile);
            File.Delete(newfile);
            File.Delete(passprotectedfile);

【问题讨论】:

  • 那么您面临的问题是什么?
  • 传输的pdf没有要求密码打开pdf?

标签: c# asp.net pdf digital-signature pfx


【解决方案1】:

您正在使用所有者密码创建PdfReader 实例,该密码允许 iText 解密受密码保护的 PDF。这就解释了为什么密码保护消失了:你告诉 iText 解密文件。

如果您想要一个经过签名和加密的文件,您需要同时执行这两项操作,而不是按顺序执行! PdfStamper 类具有允许您设置加密的不同方法。在stamper 对象上使用这些方法之一。

【讨论】:

    猜你喜欢
    • 2020-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-09
    • 2015-04-25
    • 1970-01-01
    • 2020-07-26
    • 2023-03-26
    相关资源
    最近更新 更多