2012-09-21 60 views
0

我使用c#創建和簽署pdf,itextsharp.Now我使用這個密碼保護代碼。如何密碼保護數字簽名的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); 
    } 
} 

回答

2

數字簽名的整點是要確保沒有人與文件的內容被篡改。通過添加密碼,您正在修改密碼,以便應用的數字簽名不再有效,這就是錯誤消息告訴您的。修改後您將需要退出PDF文件。

+0

感謝....現在的作品.. – Giri

+0

sorry..i不知道是什麼happened..yesterday而我簽署PDF它worked..but現在生成的PDF不要求輸入密碼保護後任何密碼,同時打開..那是在簽署之前申請的密碼保護不起作用.. – Giri

+0

我沒有在這裏發佈我的代碼..http://stackoverflow.com/questions/12541529/how-to-password-protect- the-digitally-signed-pdf-using-itextsharp – Giri

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

歡迎來到SO!請在發佈代碼段時添加一些解釋。 – vefthym

+1

此外,代碼似乎並未使用iTextSharp,而問題則集中在iTextSharp上。 – mkl

+0

然後開始有一個他想要密碼保護的簽名文件(如果不打破簽名,這是不可能的)。另一方面,答案在兩個步驟中都起作用。 – mkl