0

I m creating and signing pdf using c#,itextsharp.Now i m using this code for password protection.

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);
    }
}
Giri
  • 931
  • 11
  • 31
  • 51

2 Answers2

4

The whole point of a digital signature is to ensure that nobody has tampered with the contents of the file. By adding a password you are modifying it so the digital signature that was applied is no longer valid and this is what the error message is telling you. You will need to resign the PDF file after modifying it.

Yojimbo
  • 23,288
  • 5
  • 44
  • 48
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • sorry..i dont know what happened..yesterday while i signing the pdf after the password protection it worked..but now the generated pdf is not asking for any password while opening..that is after signing the password protection that applied before is not working.. – Giri Sep 22 '12 at 06:16
  • i did post my code here..http://stackoverflow.com/questions/12541529/how-to-password-protect-the-digitally-signed-pdf-using-itextsharp – Giri Sep 22 '12 at 06:58
0
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);
        }
    }
VMai
  • 10,156
  • 9
  • 25
  • 34
  • 1
    Welcome to SO! Please, add some explanation always when you post a code snippet. – vefthym Jul 19 '14 at 06:54
  • 1
    Furthermore the code does not seem to use iTextSharp while the question is focused on iTextSharp. – mkl Jul 19 '14 at 07:02
  • And the op starts out having a signed file which he wants to password protect (which is not possible without breaking the signature). The answer, on the other hand, does both in the same step. – mkl Jul 19 '14 at 07:06