title: Use BouncyCastle to generate Signed File
body: <p>I have a PDF file and I use this code to sign this file:</p>
  
  <pre><code>certificate = (X509Certificate) loadKeyStore(certificateFile, password).getCertificate(alias);
      privateKey = (PrivateKey) loadKeyStore(certificateFile, password).getKey(alias, alias.toCharArray());
      Security.addProvider(new BouncyCastleProvider());
      BufferedInputStream inFile = new BufferedInputStream(new FileInputStream(origem));
      byte[] dates = new byte[inFile.available()];
      entrada.read(dates);
      entrada.close();
      CMSSignedDataGenerator genetateSign = new CMSSignedDataGenerator();
      geradorAss.addSigner(privateKey, certificate, CMSSignedDataGenerator.DIGEST_SHA1);
      List certList = new ArrayList();
      certList.add(certificate);
      CertStore certs = CertStore.getInstance("Collection", new CollectionCertStoreParameters(certList));
      geradorAss.addCertificatesAndCRLs(certs);
      // Efetivamente assinar os dados de entrada  
      CMSProcessable content = new CMSProcessableByteArray(dates);
      String providerName;
      if (ks.getType().equalsIgnoreCase("JKS")) {
          providerName = BouncyCastleProvider.PROVIDER_NAME;
      } else {
          providerName = ks.getProvider().getName();
      }
      CMSSignedData signedDate = genetateSign.generate(content, providerName);
      signedDate = new CMSSignedData(content, signedDate.getEncoded());
  
      File f = Converter.converter("signedFile.pdf", signedDate.getEncoded());
  </code></pre>
  
  <p>But, the file f no open on reader. When I get the file f and run this code:</p>
  
  <pre><code>CMSSignedData data = new CMSSignedData(new FileInputStream(f));
  </code></pre>
  
  <p>Occur this error:</p>
  
  <pre><code>org.bouncycastle.cms.CMSException: Malformed content.
  </code></pre>
  
  <p>Someone can help me?</p>
  
  <p>Summarizing:</p>
  
  <p>I need to generate the final file after signing. For example, I have a test.pdf file, I want to sign and generate test_signed.pdf file. And this test_signed.pdf file must have the signature and should still be readable in your reader.</p>
  
  <p>I'm waiting...</p>