使用iTextSharp的如何密码保护数字签名的PDF? [英] how to password protect the digitally signed pdf using iTextSharp?

查看:1267
本文介绍了使用iTextSharp的如何密码保护数字签名的PDF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我米使用C#,itextsharp.Now I M使用此code密码保护和数字sign.First我与password.Than我正在签署保护创造和签署PDF。

传送的PDF是不是要求密码同时打开?
谁能Tellme公司,为什么会出现这种情况?

谢谢..

 字符串passprotectedfile =文件名;使用(流输入=新的FileStream(signedfile,FileMode.Open,FileAccess.Read,
                                     FileShare.Read))
{
    使用(流输出=新的FileStream(passprotectedfile,FileMode.Create,
                                          FileAccess.Write,FileShare.None))
    {
        PdfReader读卡器=新PdfReader(输入);
        PdfEncryptor.Encrypt(读卡器,输出,真实,regno.ToString(),,
                             PdfWriter.ALLOW_SCREENREADERS);
    }
}

code我使用数字签名。

  PdfReader读卡器=新PdfReader(文件名,传递);
        流输出=新的FileStream(signedfile,FileMode.Create,FileAccess.Write,FileShare.None);
        PdfStamper压模= PdfStamper.CreateSignature(阅读器,输出'\\ 0');        矩形RECT =新的Rectangle(455,105,555,170);
        PdfSignatureAppearance外观= stamper.SignatureAppearance;
        appearance.SetVisibleSignature(RECT,1,符号);
        PdfSignature DIC =新PdfSignature(PdfName.ADOBE_PPKLITE,新PdfName(adobe.pkcs7.detached));
        PrivateKeySignature PKS =新PrivateKeySignature(PK,SHA-256);
        MakeSignature.SignDetached(外观,PKS,链条,NULL,NULL,NULL,0,真正的);
        返回的文件名;

那么我传送。

  Response.ContentType =应用程序/ PDF
            Response.AddHeader(内容处置,附件;文件名= tes2.pdf);
            Response.TransmitFile(signedfile);
            Response.Flush();            File.Delete(signedfile);
            File.Delete(newfile中);
            File.Delete(passprotectedfile);


解决方案

您正在使用一个所有者密码,允许iText的解密密码保护的PDF创建一个 PdfReader 实例。这就解释了为什么密码保护已经一去不复返了。你告诉iText的解密文件

如果您希望签署以及加密的文件,你需要在一个去做这两种操作,不按顺序!在 PdfStamper 类有不同的方法,使您可以设置加密。使用的模子对象的这些方法之一。

I m creating and signing pdf using c#,itextsharp.Now i m using this code for password protection and digital sign.First i am protecting with password.Than i am signing.

the transmitted pdf is not asking password while opening? Can someone tellme why is this happening?

Thanks..

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);
    }
}

Code i am using for digitally sign.

        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;

then i am transmitting.

            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);

解决方案

You're creating a PdfReader instance using an owner password that allows iText to decrypt the password protected PDF. That explains why the password protection is gone: you told iText to decrypt the file.

If you want a file that is signed as well as encrypted, you need to do both operations in one go, not sequentially! The PdfStamper class has different methods that allow you to set the encryption. Use one of these methods on the stamper object.

这篇关于使用iTextSharp的如何密码保护数字签名的PDF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆