title: Unable to verify digital signature using Apache PDFBOX
body: <p>I am a newbie in using Digital Signatures. In one of the projects we are using Apache PdfBox for processing digitally signed pdf files. While we could test all features, verification of signed pdf files is something we are unable to crack. We are using BouncyCastle as the provider. Below is the code:</p>
  <p>Get Digital Signature and Signed Content from pdf file:</p>
  <pre class="lang-java prettyprint-override"><code>byte[] signatureAsBytes = pdsignature.getContents(new FileInputStream(this.INPUT_FILE));
  byte[] signedContentAsBytes = pdsignature.getSignedContent(new FileInputStream(this.INPUT_FILE));
  </code></pre>
  <p>Digital Signature Verification:</p>
  <pre class="lang-java prettyprint-override"><code>Security.addProvider(new BouncyCastleProvider());
  Signature signer = Signature.getInstance(&quot;RSA&quot;,&quot;BC&quot;);
  
  //Get PublicKey from p7b file
  X509Certificate cert509=null;
  File file = new File(&quot;C:\\certificate_file.p7b&quot;);
  FileInputStream fis = new FileInputStream(file);
  CertificateFactory cf = CertificateFactory.getInstance(&quot;X.509&quot;);
  Collection c = cf.generateCertificates(fis);
  Iterator it = c.iterator();
  PublicKey pubkey;
  
  while (it.hasNext()) 
  {
     cert509 = (X509Certificate) it.next();
     pubkey = cert509.getPublicKey();
  }
  
  boolean VERIFIED=false;
  Security.addProvider(new BouncyCastleProvider());
  Signature signer = Signature.getInstance(&quot;RSA&quot;,&quot;BC&quot;);
  PublicKey key=this.getPublicKey(false);
  signer.initVerify(key);
  
  List&lt;PDSignature&gt; allsigs = this.PDFDOC.getSignatureDictionaries();
  Iterator&lt;PDSignature&gt; i = allsigs.iterator();
      
  while(i.hasNext())
  {
          PDSignature sig = (PDSignature) i.next();
          byte[] signatureAsBytes = sig.getContents(new FileInputStream(this.INPUT_FILE));
          byte[] signedContentAsBytes = sig.getSignedContent(new FileInputStream(this.INPUT_FILE));
          signer.update(signedContentAsBytes);
          VERIFIED=signer.verify(signatureAsBytes);
  }
      
  System.out.println(&quot;Verified=&quot;+VERIFIED);
  </code></pre>
  <p>Below are relevant extracts from the certificate in p7b format - I am using BouncyCastle as security provider:</p>
  <pre><code>  Signature Algorithm: SHA256withRSA, OID = 1.2.840.113549.1.1.11
    Key:  Sun RSA public key, 2048 bits
    Validity: [From: Tue Aug 06 12:26:47 IST 2013,
    To: Wed Aug 05 12:26:47 IST 2015]
    Algorithm: [SHA256withRSA]
  </code></pre>
  <p>With the above code I am always getting response as &quot;false&quot;. I have no idea how to fix the issue. Please help</p>