private static void encryptFile(
      OutputStream out,
      String fileName,
      PGPPublicKey encKey,
      boolean armor,
      boolean withIntegrityCheck)
      throws IOException, NoSuchProviderException {
    if (armor) {
      out = new ArmoredOutputStream(out);
    }

    try {
      PGPEncryptedDataGenerator cPk =
          new PGPEncryptedDataGenerator(
              new JcePGPDataEncryptorBuilder(PGPEncryptedData.CAST5)
                  .setWithIntegrityPacket(withIntegrityCheck)
                  .setSecureRandom(new SecureRandom())
                  .setProvider("BC"));

      cPk.addMethod(new JcePublicKeyKeyEncryptionMethodGenerator(encKey).setProvider("BC"));

      OutputStream cOut = cPk.open(out, new byte[1 << 16]);

      PGPCompressedDataGenerator comData = new PGPCompressedDataGenerator(PGPCompressedData.ZIP);

      PGPUtil.writeFileToLiteralData(
          comData.open(cOut), PGPLiteralData.BINARY, new File(fileName), new byte[1 << 16]);

      comData.close();

      cOut.close();

      if (armor) {
        out.close();
      }
    } catch (PGPException e) {
      System.err.println(e);
      if (e.getUnderlyingException() != null) {
        e.getUnderlyingException().printStackTrace();
      }
    }
  }
  /** decrypt the passed in message stream */
  private static void decryptFile(
      InputStream in, InputStream keyIn, char[] passwd, String defaultFileName)
      throws IOException, NoSuchProviderException {
    in = PGPUtil.getDecoderStream(in);

    try {
      JcaPGPObjectFactory pgpF = new JcaPGPObjectFactory(in);
      PGPEncryptedDataList enc;

      Object o = pgpF.nextObject();
      //
      // the first object might be a PGP marker packet.
      //
      if (o instanceof PGPEncryptedDataList) {
        enc = (PGPEncryptedDataList) o;
      } else {
        enc = (PGPEncryptedDataList) pgpF.nextObject();
      }

      //
      // find the secret key
      //
      Iterator it = enc.getEncryptedDataObjects();
      PGPPrivateKey sKey = null;
      PGPPublicKeyEncryptedData pbe = null;
      PGPSecretKeyRingCollection pgpSec =
          new PGPSecretKeyRingCollection(
              PGPUtil.getDecoderStream(keyIn), new JcaKeyFingerprintCalculator());

      while (sKey == null && it.hasNext()) {
        pbe = (PGPPublicKeyEncryptedData) it.next();

        sKey = PGPExampleUtil.findSecretKey(pgpSec, pbe.getKeyID(), passwd);
      }

      if (sKey == null) {
        throw new IllegalArgumentException("secret key for message not found.");
      }

      InputStream clear =
          pbe.getDataStream(
              new JcePublicKeyDataDecryptorFactoryBuilder().setProvider("BC").build(sKey));

      JcaPGPObjectFactory plainFact = new JcaPGPObjectFactory(clear);

      PGPCompressedData cData = (PGPCompressedData) plainFact.nextObject();

      InputStream compressedStream = new BufferedInputStream(cData.getDataStream());
      JcaPGPObjectFactory pgpFact = new JcaPGPObjectFactory(compressedStream);

      Object message = pgpFact.nextObject();

      if (message instanceof PGPLiteralData) {
        PGPLiteralData ld = (PGPLiteralData) message;

        String outFileName = ld.getFileName();
        if (outFileName.length() == 0) {
          outFileName = defaultFileName;
        }

        InputStream unc = ld.getInputStream();
        OutputStream fOut = new BufferedOutputStream(new FileOutputStream(outFileName));

        Streams.pipeAll(unc, fOut);

        fOut.close();
      } else if (message instanceof PGPOnePassSignatureList) {
        throw new PGPException("encrypted message contains a signed message - not literal data.");
      } else {
        throw new PGPException("message is not a simple encrypted file - type unknown.");
      }

      if (pbe.isIntegrityProtected()) {
        if (!pbe.verify()) {
          System.err.println("message failed integrity check");
        } else {
          System.err.println("message integrity check passed");
        }
      } else {
        System.err.println("no message integrity check");
      }
    } catch (PGPException e) {
      System.err.println(e);
      if (e.getUnderlyingException() != null) {
        e.getUnderlyingException().printStackTrace();
      }
    }
  }