title: Pgp sign+encrypt then decrypt+verify
body: <p>While going with sign encrypt then decrypt and verify, I keep getting unknown object in stream while verifying. Message integrity check passed but when I try to verify in the very next line after decrypt I'm getting the above said error.</p>
  
  <pre><code>    private static void encryptFile(
      String outFileName,
      OutputStream out,
      String fileName,
      PGPPublicKey encKey,
      String sKeyFileName,
      char[] passPhrase,
      boolean armor,
      boolean withIntegrityCheck)
      throws Exception
  {
      if (armor)
      {
          out = new ArmoredOutputStream(out);
      }
  
      try
      {
  
  
          byte[] bytes = PGPKeyUtil.compressFile(fileName, CompressionAlgorithmTags.ZIP);
  
  
          PGPEncryptedDataGenerator encGen = new PGPEncryptedDataGenerator(
              PGPEncryptedData.CAST5, withIntegrityCheck, new SecureRandom(), "BC");
          encGen.addMethod(encKey);
  
          OutputStream cOut = encGen.open(out, bytes.length);
  
          cOut.write(bytes);
          cOut.close();
          out.close();
  
          cOut.close();
  
  
          if (armor)
          {
              out.close();
          }
  
          encGen.close();
  
      }
      catch (PGPException e)
      {
          System.err.println(e);
          if (e.getUnderlyingException() != null)
          {
              e.getUnderlyingException().printStackTrace();
          }
      }
  }
  
  public static void signFile(
      String          fileName,
      InputStream keyIn,
      OutputStream    out,
      char[]          pass,
      boolean         armor)
      throws IOException, NoSuchAlgorithmException, NoSuchProviderException, PGPException, SignatureException
  {
      if (armor)
      {
          out = new ArmoredOutputStream(out);
      }
  
      PGPSecretKey pgpSec = readSecretKey(keyIn);
      PGPPrivateKey pgpPrivKey = pgpSec.extractPrivateKey(pass, "BC");
      PGPSignatureGenerator sGen = new PGPSignatureGenerator(pgpSec.getPublicKey().getAlgorithm(), PGPUtil.SHA1, "BC");
  
      sGen.initSign(PGPSignature.BINARY_DOCUMENT, pgpPrivKey);
  
      Iterator it = pgpSec.getPublicKey().getUserIDs();
      if (it.hasNext())
      {
          PGPSignatureSubpacketGenerator spGen = new PGPSignatureSubpacketGenerator();
  
          spGen.setSignerUserID(false, (String)it.next());
          sGen.setHashedSubpackets(spGen.generate());
      }
  
      PGPCompressedDataGenerator cGen = new PGPCompressedDataGenerator(
          PGPCompressedData.ZLIB);
  
      BCPGOutputStream bOut = new BCPGOutputStream(cGen.open(out));
  
      sGen.generateOnePassVersion(false).encode(bOut);
  
      File file = new File(fileName);
      PGPLiteralDataGenerator lGen = new PGPLiteralDataGenerator();
      OutputStream                lOut = lGen.open(bOut, PGPLiteralData.BINARY, file);
      FileInputStream fIn = new FileInputStream(file);
      int                         ch = 0;
  
      while ((ch = fIn.read()) &gt;= 0)
      {
          lOut.write(ch);
          sGen.update((byte)ch);
      }
  
      lGen.close();
  
  
      sGen.generate().encode(bOut);
  
      cGen.close();
      bOut.close();
      fIn.close();
      lOut.close();
  
      if (armor)
      {
          out.close();
      }
      System.out.println("Successfully signed");
  }
  </code></pre>
  
  <p>First I sign using signFile and then I encrypt the file using encrypt method.</p>