0

I am trying to encrypt and decrypt a file using PGP Encryption/Decryption methodology with Apache Camel.

Further I have installed Kleopatra to generate the private and public keys. Using Kleopatra i have generated my keys successfully. The secret key and public keys are in ".asc" extension.

Below is the piece of code i am using to encrypt the file

import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;

public class PGPENC {
    public static void main(String[] args) throws Exception {
        CamelContext camelContext = new DefaultCamelContext();

        camelContext.addRoutes(new RouteBuilder() {
            public void configure() throws Exception {

                String publicKeyFileName = "file:C:\\Users\\karthick\\Desktop\\PGP\\PGP\\Public_Key.asc";
                String keyUserid = "Karthick Sambanghi <karthick88it@gmail.com>";

                from("file:C:\\Users\\ITSS\\karthick\\PGP\\PGP\\IN?noop=true;delete=true").marshal()
                        .pgp(publicKeyFileName, keyUserid).to("file:C:\\Users\\ITSS\\Desktop\\PGP\\PGP\\OUT");

            }
        });

        camelContext.start();

        Thread.sleep(5000);
        camelContext.stop();
    }
}

Here the program executed successfully without any errors but the file is not being encrypted in OUT folder. Is there anyway to check the "camelContext" return statement whether it is success or failure ?

Below are the libraries used currently for executing the program

bcpg-jdk15on-1.52
bcprov-ext-jdk15on-1.57
camel-context-2.22.1
camel-core-2.22.1
camel-crypto-2.19.1
slf4j-api-1.7.25
slf4j-nop-1.7.25
President James K. Polk
  • 40,516
  • 21
  • 95
  • 125
Karthick88it
  • 601
  • 2
  • 12
  • 28
  • 2
    **Never** mix versions of camel dependencies, these combinations are not tested. You need to have `camel-crypto` with the same version as `camel-core`. Also not sure, what are you trying to do with combination `noop=true` and `delete=true`, these options are disjunktive. And the parameters should be separated with `&`, not `;`. And `slf4j-nop` is NOP, so any errors logged by camel are just thrown away and are never printed to console. – Bedla Aug 20 '19 at 20:59
  • 2
    Now I see this sample is copied from https://dzone.com/articles/pgp-encryption-and-decryption-with-apache-camel . Sorry to tell, but the article is wrong in many ways and you should find a better source - eg official documentation, or unit tests provided on github – Bedla Aug 20 '19 at 21:47
  • 1
    Thanks Bedla for your response. I have tried with same versions of library and still the issue persists. Finally I tried with another article from Github (https://github.com/bcgit/bc-java/blob/master/pg/src/main/java/org/bouncycastle/openpgp/examples/PBEFileProcessor.java) and it is working fine. – Karthick88it Aug 21 '19 at 11:28
  • are you able to encrypt or decrypt? – Rocky4Ever Jul 09 '20 at 18:09

1 Answers1

1

you can enable camel logging in the console using by adding in your programm org.apache.log4j.BasicConfigurator.configure().

Using that you could verify if the route started and consumed the file. so Executing your programm with adding some logs :

 CamelContext camelContext = new DefaultCamelContext();
    BasicConfigurator.configure();
    camelContext.addRoutes(new RouteBuilder() {

      public void configure() throws Exception {

        String publicKeyFileName = "file:C:\\LocalData\\Keys\\pgp_public.asc";
        String keyUserid = " ";

        from("file:C:\\Test\\Test\\IN")
            .log("file received")
            .marshal().pgp(publicKeyFileName, keyUserid)
        .to("file:C\\Test\\Test\\OUT");

      }
    });
    camelContext.start();
    Thread.sleep(30000);
    camelContext.stop();
  } 

I can notice that the route is starting, consuming files from the in folder and fails then with the exception :

Caused by: java.lang.NoSuchMethodError: org.bouncycastle.openpgp.PGPPublicKeyRingCollection.<init>(Ljava/io/InputStream;)V
    at org.apache.camel.converter.crypto.PGPDataFormatUtil.findPublicKey(PGPDataFormatUtil.java:64)
    at org.apache.camel.converter.crypto.PGPDataFormatUtil.findPublicKey(PGPDataFormatUtil.java:54)
    at org.apache.camel.converter.crypto.PGPDataFormat.marshal(PGPDataFormat.java:64)
    at org.apache.camel.processor.MarshalProcessor.process(MarshalProcessor.java:59)
    at org.apache.camel.util.AsyncProcessorConverterHelper$ProcessorToAsyncProcessorBridge.process(AsyncProcessorConverterHelper.java:61)

This is a bug in Camel documentation, please find here details: https://jira.apache.org/jira/browse/CAMEL-12574