【问题标题】:how to password protect the digitally signed pdf?如何密码保护数字签名的pdf?
【发布时间】:2016-11-12 03:07:00
【问题描述】:

我正在使用 c#、itextsharp 创建和签署 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);
    }
}

【问题讨论】:

    标签: c# asp.net pdf itextsharp


    【解决方案1】:

    数字签名的全部意义在于确保没有人篡改文件的内容。通过添加密码,您正在修改它,因此应用的数字签名不再有效,这就是错误消息告诉您的内容。您需要在修改 PDF 文件后对其进行签名。

    【讨论】:

    • 对不起..我不知道发生了什么..昨天我在密码保护后签署 pdf 文件时..但现在生成的 pdf 在打开时不要求任何密码..那是之后签署之前应用的密码保护不起作用..
    • 我确实在这里发布了我的代码..stackoverflow.com/questions/12541529/…
    【解决方案2】:
    private void Generatedigitalsignatureandpassprotected(string filepath, string filepass)
        {
            Document _document = new Document();
    
            _document.Title = "PdfDigitalSignature - Sample";
            _document.Author = "dbAutoTrack Ltd, USA";
            _document.Creator = "dbAutoTrack.PDFWriter";
            PDFDigitalSignature digitalSignature = new PDFDigitalSignature();
            //  digitalSignature.X509Certificate = new X509Certificate2(MapPath("../data/DigitalSignature.pfx"), "password", X509KeyStorageFlags.MachineKeySet |X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable););
            string executingFolder = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
            var digitalsignaturefilepath = "E:\\wwwroot\\NaveenWindowsAppFile\\WindowsFormsApplication1\\digitalsignaturefile\\DigitalSignature.pfx";
            digitalSignature.X509Certificate = new X509Certificate2(digitalsignaturefilepath, "password", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);
            digitalSignature.Date = DateTime.Now;
            digitalSignature.Location = "Location ";
            digitalSignature.Reason = "Reason ";
            digitalSignature.ContactInfo = "Convert Digital Code by Cstech";
            digitalSignature.DetachSignature = false;
            digitalSignature.RootTrusted = false;
            _document.DigitalSignature = digitalSignature;
            //password protection code//
            SecurityManager _securityManager = new SecurityManager();
            _securityManager.Encryption = Encryption.Use128BitKey;
            _securityManager.OwnerPassword = filepass;
            _securityManager.UserPassword = filepass;
            _securityManager.AllowCopy = true;
            _securityManager.AllowEdit = true;
            //end//
            //Assign SecurityManager to the Document.
            _document.SecurityManager = _securityManager;
            PDFFont _font1 = new PDFFont(StandardFonts.TimesRoman, FontStyle.Regular);
            dbAutoTrack.PDFWriter.Page page1 = new dbAutoTrack.PDFWriter.Page(PageSize.A4);
            PDFGraphics graphics1 = page1.Graphics;
    
            PdfSignatureField signatureField = graphics1.AddDigitalSignature("name", new RectangleF(50, 50, 150, 75), _font1, 8f);
            signatureField.DigitalSignature = digitalSignature;
            _document.Pages.Add(page1);
    
            using (FileStream outFile = new FileStream(filepath, FileMode.Create, FileAccess.ReadWrite))
            {
                _document.Generate(outFile);
            }
        }
    

    【讨论】:

    • 欢迎来到 SO!请在发布代码 sn-p 时添加一些解释。
    • 此外,当问题集中在 iTextSharp 上时,代码似乎没有使用 iTextSharp。
    • 操作开始有一个签名文件,他想用密码保护(如果不破坏签名,这是不可能的)。另一方面,答案是在同一步骤中完成。
    猜你喜欢
    • 1970-01-01
    • 2020-07-18
    • 2020-07-26
    • 2011-12-23
    • 1970-01-01
    • 2016-10-03
    • 1970-01-01
    • 2020-12-21
    相关资源
    最近更新 更多