簡體   English   中英

如何使用iTextSharp密碼保護數字簽名的pdf?

[英]how to password protect the digitally signed pdf using iTextSharp?

我使用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);

您正在使用所有者密碼創建PdfReader實例,該密碼允許iText解密受密碼保護的PDF。 這就解釋了為什么密碼保護消失了:你告訴iText解密文件。

如果您想要一個已簽名和加密的文件,您需要一次完成兩個操作,而不是按順序執行! PdfStamper類具有允許您設置加密的不同方法。 stamper對象上使用這些方法之一。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM