private static byte[] compress(final String fileName, Date fileDate, final byte[] plainText)
      throws IOException {
    final ByteArrayOutputStream buf = new ByteArrayOutputStream();
    final PGPCompressedDataGenerator comdg;
    final int len = plainText.length;
    if (fileDate == null) {
      fileDate = PGPLiteralData.NOW;
    }

    comdg = new PGPCompressedDataGenerator(PGPCompressedData.ZIP);
    final OutputStream out =
        new PGPLiteralDataGenerator()
            .open(comdg.open(buf), PGPLiteralData.BINARY, fileName, len, fileDate);
    out.write(plainText);
    out.close();
    comdg.close();
    return buf.toByteArray();
  }
  @Test
  public void testExceptionDecryptorIncorrectInputFormatSymmetricEncryptedData() throws Exception {

    byte[] payload = "Not Correct Format".getBytes("UTF-8");
    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    PGPEncryptedDataGenerator encGen =
        new PGPEncryptedDataGenerator(
            new JcePGPDataEncryptorBuilder(SymmetricKeyAlgorithmTags.CAST5)
                .setSecureRandom(new SecureRandom())
                .setProvider(getProvider()));

    encGen.addMethod(new JcePBEKeyEncryptionMethodGenerator("pw".toCharArray()));

    OutputStream encOut = encGen.open(bos, new byte[1024]);
    PGPCompressedDataGenerator comData =
        new PGPCompressedDataGenerator(CompressionAlgorithmTags.ZIP);
    OutputStream comOut = new BufferedOutputStream(comData.open(encOut));
    PGPLiteralDataGenerator litData = new PGPLiteralDataGenerator();
    OutputStream litOut =
        litData.open(
            comOut, PGPLiteralData.BINARY, PGPLiteralData.CONSOLE, new Date(), new byte[1024]);
    litOut.write(payload);
    litOut.flush();
    litOut.close();
    comOut.close();
    encOut.close();
    MockEndpoint mock = getMockEndpoint("mock:exception");
    mock.expectedMessageCount(1);
    template.sendBody("direct:subkeyUnmarshal", bos.toByteArray());
    assertMockEndpointsSatisfied();

    checkThrownException(
        mock,
        IllegalArgumentException.class,
        null,
        "The input message body has an invalid format.");
  }
  public TestResult perform() {
    try {
      String file = null;
      KeyFactory fact = KeyFactory.getInstance("DSA", "BC");
      PGPPublicKey pubKey = null;
      PrivateKey privKey = null;

      PGPUtil.setDefaultProvider("BC");

      //
      // Read the public key
      //
      PGPObjectFactory pgpFact = new PGPObjectFactory(testPubKeyRing);

      PGPPublicKeyRing pgpPub = (PGPPublicKeyRing) pgpFact.nextObject();

      pubKey = pgpPub.getPublicKey();

      //
      // Read the private key
      //
      PGPSecretKeyRing sKey = new PGPSecretKeyRing(testPrivKeyRing);
      PGPPrivateKey pgpPrivKey = sKey.getSecretKey().extractPrivateKey(pass, "BC");

      //
      // signature generation
      //
      String data = "hello world!";
      ByteArrayOutputStream bOut = new ByteArrayOutputStream();
      ByteArrayInputStream testIn = new ByteArrayInputStream(data.getBytes());
      PGPSignatureGenerator sGen = new PGPSignatureGenerator(PGPPublicKey.DSA, PGPUtil.SHA1, "BC");

      sGen.initSign(PGPSignature.BINARY_DOCUMENT, pgpPrivKey);

      PGPCompressedDataGenerator cGen = new PGPCompressedDataGenerator(PGPCompressedData.ZIP);

      BCPGOutputStream bcOut = new BCPGOutputStream(cGen.open(bOut));

      sGen.generateOnePassVersion(false).encode(bcOut);

      PGPLiteralDataGenerator lGen = new PGPLiteralDataGenerator();
      OutputStream lOut =
          lGen.open(bcOut, PGPLiteralData.BINARY, "_CONSOLE", data.getBytes().length, new Date());
      int ch;

      while ((ch = testIn.read()) >= 0) {
        lOut.write(ch);
        sGen.update((byte) ch);
      }

      sGen.generate().encode(bcOut);

      lGen.close();

      cGen.close();

      //
      // verify generated signature
      //
      pgpFact = new PGPObjectFactory(bOut.toByteArray());

      PGPCompressedData c1 = (PGPCompressedData) pgpFact.nextObject();

      pgpFact = new PGPObjectFactory(c1.getDataStream());

      PGPOnePassSignatureList p1 = (PGPOnePassSignatureList) pgpFact.nextObject();

      PGPOnePassSignature ops = p1.get(0);

      PGPLiteralData p2 = (PGPLiteralData) pgpFact.nextObject();

      InputStream dIn = p2.getInputStream();

      ops.initVerify(pubKey, "BC");

      while ((ch = dIn.read()) >= 0) {
        ops.update((byte) ch);
      }

      PGPSignatureList p3 = (PGPSignatureList) pgpFact.nextObject();

      if (!ops.verify(p3.get(0))) {
        return new SimpleTestResult(false, getName() + ": Failed generated signature check");
      }

      //
      // test encryption
      //

      //
      // find a key sutiable for encryption
      //
      long pgpKeyID = 0;
      PublicKey pKey = null;

      Iterator it = pgpPub.getPublicKeys();
      while (it.hasNext()) {
        PGPPublicKey pgpKey = (PGPPublicKey) it.next();

        if (pgpKey.getAlgorithm() == PGPPublicKey.ELGAMAL_ENCRYPT
            || pgpKey.getAlgorithm() == PGPPublicKey.ELGAMAL_GENERAL) {
          pKey = pgpKey.getKey("BC");
          pgpKeyID = pgpKey.getKeyID();

          //
          // verify the key
          //

        }
      }

      Cipher c = Cipher.getInstance("ElGamal/None/PKCS1Padding", "BC");

      c.init(Cipher.ENCRYPT_MODE, pKey);

      byte[] in = "hello world".getBytes();

      byte[] out = c.doFinal(in);

      pgpPrivKey = sKey.getSecretKey(pgpKeyID).extractPrivateKey(pass, "BC");

      c.init(Cipher.DECRYPT_MODE, pgpPrivKey.getKey());

      out = c.doFinal(out);

      if (notEqual(in, out)) {
        return new SimpleTestResult(false, getName() + ": decryption failed.");
      }

      //
      // encrypted message
      //
      byte[] text = {
        (byte) 'h',
        (byte) 'e',
        (byte) 'l',
        (byte) 'l',
        (byte) 'o',
        (byte) ' ',
        (byte) 'w',
        (byte) 'o',
        (byte) 'r',
        (byte) 'l',
        (byte) 'd',
        (byte) '!',
        (byte) '\n'
      };

      PGPObjectFactory pgpF = new PGPObjectFactory(encMessage);

      PGPEncryptedDataList encList = (PGPEncryptedDataList) pgpF.nextObject();

      PGPPublicKeyEncryptedData encP = (PGPPublicKeyEncryptedData) encList.get(0);

      InputStream clear = encP.getDataStream(pgpPrivKey, "BC");

      pgpFact = new PGPObjectFactory(clear);

      c1 = (PGPCompressedData) pgpFact.nextObject();

      pgpFact = new PGPObjectFactory(c1.getDataStream());

      PGPLiteralData ld = (PGPLiteralData) pgpFact.nextObject();

      bOut = new ByteArrayOutputStream();

      if (!ld.getFileName().equals("test.txt")) {
        throw new RuntimeException("wrong filename in packet");
      }

      InputStream inLd = ld.getDataStream();

      while ((ch = inLd.read()) >= 0) {
        bOut.write(ch);
      }

      if (notEqual(bOut.toByteArray(), text)) {
        return new SimpleTestResult(false, getName() + ": wrong plain text in decrypted packet");
      }

      //
      // signed and encrypted message
      //
      pgpF = new PGPObjectFactory(signedAndEncMessage);

      encList = (PGPEncryptedDataList) pgpF.nextObject();

      encP = (PGPPublicKeyEncryptedData) encList.get(0);

      clear = encP.getDataStream(pgpPrivKey, "BC");

      pgpFact = new PGPObjectFactory(clear);

      c1 = (PGPCompressedData) pgpFact.nextObject();

      pgpFact = new PGPObjectFactory(c1.getDataStream());

      p1 = (PGPOnePassSignatureList) pgpFact.nextObject();

      ops = p1.get(0);

      ld = (PGPLiteralData) pgpFact.nextObject();

      bOut = new ByteArrayOutputStream();

      if (!ld.getFileName().equals("test.txt")) {
        throw new RuntimeException("wrong filename in packet");
      }

      inLd = ld.getDataStream();

      //
      // note: we use the DSA public key here.
      //
      ops.initVerify(pgpPub.getPublicKey(), "BC");

      while ((ch = inLd.read()) >= 0) {
        ops.update((byte) ch);
        bOut.write(ch);
      }

      p3 = (PGPSignatureList) pgpFact.nextObject();

      if (!ops.verify(p3.get(0))) {
        return new SimpleTestResult(false, getName() + ": Failed signature check");
      }

      if (notEqual(bOut.toByteArray(), text)) {
        return new SimpleTestResult(false, getName() + ": wrong plain text in decrypted packet");
      }

      //
      // encrypt
      //
      ByteArrayOutputStream cbOut = new ByteArrayOutputStream();
      PGPEncryptedDataGenerator cPk =
          new PGPEncryptedDataGenerator(
              SymmetricKeyAlgorithmTags.TRIPLE_DES, new SecureRandom(), "BC");
      PGPPublicKey puK = sKey.getSecretKey(pgpKeyID).getPublicKey();

      cPk.addMethod(puK);

      OutputStream cOut = cPk.open(cbOut, bOut.toByteArray().length);

      cOut.write(text);

      cOut.close();

      pgpF = new PGPObjectFactory(cbOut.toByteArray());

      encList = (PGPEncryptedDataList) pgpF.nextObject();

      encP = (PGPPublicKeyEncryptedData) encList.get(0);

      pgpPrivKey = sKey.getSecretKey(pgpKeyID).extractPrivateKey(pass, "BC");

      clear = encP.getDataStream(pgpPrivKey, "BC");

      bOut.reset();

      while ((ch = clear.read()) >= 0) {
        bOut.write(ch);
      }

      out = bOut.toByteArray();

      if (notEqual(out, text)) {
        return new SimpleTestResult(false, getName() + ": wrong plain text in generated packet");
      }

      return new SimpleTestResult(true, getName() + ": Okay");
    } catch (Exception e) {
      e.printStackTrace();
      if (e instanceof PGPException) {
        ((PGPException) e).getUnderlyingException().printStackTrace();
      }
      return new SimpleTestResult(false, getName() + ": exception - " + e.toString());
    }
  }