简体   繁体   中英

PGP Decryption of *.bai.asc file using Apache Camel

I am trying to Encrypt/Decrypt files using Apache Camel.

The maven dependency:

    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-core</artifactId>
        <version>3.8.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-crypto</artifactId>
        <version>3.8.0</version>
    </dependency>

The class:

public class PGPDecryption {

private final static String FILE = "file:";

public static void decrypt(String secretKey, String keyUserid, String keyPassword, String source, String destination) throws Exception {
    CamelContext camelContext = new DefaultCamelContext();
    camelContext.addRoutes(new RouteBuilder() {
        public void configure() throws Exception {
            final String secretKeyFileName = FILE + secretKey;
            String sourceFile = FILE + source + "?delete=true";
            String destinationFile = FILE + destination;
            
            from(sourceFile).unmarshal().pgp(secretKeyFileName, keyUserid, keyPassword).to(destinationFile);
        }
    });
    camelContext.start();
    Thread.sleep(5000);
    camelContext.stop();
}

public static void encrypt(String publicKey, String keyUserid, String source, String destination) throws Exception {
    CamelContext camelContext = new DefaultCamelContext();
    camelContext.addRoutes(new RouteBuilder() {
        public void configure() throws Exception {
            final String publicKeyFileName = FILE + publicKey;
            PGPDataFormat pgp = new PGPDataFormat();
            pgp.setKeyFileName(publicKeyFileName);
            pgp.setKeyUserid(keyUserid);
            pgp.setArmored(true);
            from(FILE + source + "?delete=true").marshal(pgp).to(FILE + destination);
        }
    });
    camelContext.start();
    Thread.sleep(5000);
    camelContext.stop();
}

Encryption is working fine, but while trying to decrypt a * .bai.asc file, I am getting the below exception:

ERROR 2021-03-26 14:44:46,025 [Camel (camel-1) thread #0 - file://C:%5Capp%5Clocalfiles%5Cbofa2sap%5Clocalprocessing%5Cus] [processor: ; event: ] org.apache.camel.processor.errorhandler.DefaultErrorHandler: Failed delivery for (MessageId: 1A1BE2B73B9E33E-0000000000000000 on ExchangeId: 1A1BE2B73B9E33E-0000000000000000). Exhausted after delivery attempt: 1 caught: java.lang.NullPointerException

Message History (complete message history is disabled)
---------------------------------------------------------------------------------------------------------------------------------------
RouteId              ProcessorId          Processor                                                                        Elapsed (ms)
[route1            ] [route1            ] [from[file://C:%5Capp%5Clocalfiles%5Cbofa2sap%5Clocalprocessing%5Cus?delete=tru] [       898]
    ...
[route1            ] [unmarshal1        ] [unmarshal[org.apache.camel.model.dataformat.PGPDataFormat@760da6f2]           ] [         0]

Stacktrace
---------------------------------------------------------------------------------------------------------------------------------------

java.lang.NullPointerException: null
    at org.apache.camel.support.ResourceHelper.resolveMandatoryResourceAsInputStream(ResourceHelper.java:113) ~[camel-support-3.8.0.jar:3.8.0]
    at org.apache.camel.converter.crypto.PGPDataFormatUtil.determineKeyRingInputStream(PGPDataFormatUtil.java:167) ~[camel-crypto-3.8.0.jar:3.8.0]
    at org.apache.camel.converter.crypto.PGPDataFormatUtil.getPublicKeyRingCollection(PGPDataFormatUtil.java:76) ~[camel-crypto-3.8.0.jar:3.8.0]
    at org.apache.camel.converter.crypto.PGPDataFormat.getPublicKey(PGPDataFormat.java:260) ~[camel-crypto-3.8.0.jar:3.8.0]
    at org.apache.camel.converter.crypto.PGPKeyAccessDataFormat.getSignature(PGPKeyAccessDataFormat.java:520) ~[camel-crypto-3.8.0.jar:3.8.0]
    at org.apache.camel.converter.crypto.PGPKeyAccessDataFormat.unmarshal(PGPKeyAccessDataFormat.java:386) ~[camel-crypto-3.8.0.jar:3.8.0]
    at org.apache.camel.support.processor.UnmarshalProcessor.process(UnmarshalProcessor.java:64) ~[camel-support-3.8.0.jar:3.8.0]
    at org.apache.camel.processor.errorhandler.RedeliveryErrorHandler$SimpleTask.run(RedeliveryErrorHandler.java:395) [camel-core-processor-3.8.0.jar:3.8.0]
    at org.apache.camel.impl.engine.DefaultReactiveExecutor$Worker.schedule(DefaultReactiveExecutor.java:148) [camel-base-engine-3.8.0.jar:3.8.0]
    at org.apache.camel.impl.engine.DefaultReactiveExecutor.scheduleMain(DefaultReactiveExecutor.java:60) [camel-base-engine-3.8.0.jar:3.8.0]
    at org.apache.camel.processor.Pipeline.process(Pipeline.java:147) [camel-core-processor-3.8.0.jar:3.8.0]
    at org.apache.camel.impl.engine.CamelInternalProcessor.process(CamelInternalProcessor.java:312) [camel-base-engine-3.8.0.jar:3.8.0]
    at org.apache.camel.component.file.GenericFileConsumer.processExchange(GenericFileConsumer.java:486) [camel-file-3.8.0.jar:3.8.0]
    at org.apache.camel.component.file.GenericFileConsumer.processBatch(GenericFileConsumer.java:240) [camel-file-3.8.0.jar:3.8.0]
    at org.apache.camel.component.file.GenericFileConsumer.poll(GenericFileConsumer.java:201) [camel-file-3.8.0.jar:3.8.0]
    at org.apache.camel.support.ScheduledPollConsumer.doRun(ScheduledPollConsumer.java:190) [camel-support-3.8.0.jar:3.8.0]
    at org.apache.camel.support.ScheduledPollConsumer.run(ScheduledPollConsumer.java:107) [camel-support-3.8.0.jar:3.8.0]
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_221]
    at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308) [?:1.8.0_221]
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180) [?:1.8.0_221]
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294) [?:1.8.0_221]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [?:1.8.0_221]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [?:1.8.0_221]
    at java.lang.Thread.run(Thread.java:748) [?:1.8.0_221]
WARN  2021-03-26 14:44:46,034 [Camel (camel-1) thread #0 - file://C:%5Capp%5Clocalfiles%5Cbofa2sap%5Clocalprocessing%5Cus] [processor: ; event: ] org.apache.camel.component.file.GenericFileOnCompletion: Rollback file strategy: org.apache.camel.component.file.strategy.GenericFileDeleteProcessStrategy@3588a239 for file: GenericFile[C:\app\localfiles\bofa2sap\localprocessing\us\radial_us_pdr_20201020071428842.bai.asc]
ERROR 2021-03-26 14:44:46,680 [Camel (camel-1) thread #0 - file://C:%5Capp%5Clocalfiles%5Cbofa2sap%5Clocalprocessing%5Cus] [processor: ; event: ] org.apache.camel.processor.errorhandler.DefaultErrorHandler: Failed delivery for (MessageId: 1A1BE2B73B9E33E-0000000000000001 on ExchangeId: 1A1BE2B73B9E33E-0000000000000001). Exhausted after delivery attempt: 1 caught: java.lang.NullPointerException

I have also tried using the PGPDataFormat for decryption. But still I am getting the same exception. What am I missing in the code? Please suggest.

Apparently I needed to add the Filename of the keyring used for signing (Public Key). Here's how I have set the PGPDataFormat:

CamelContext camelContext = new DefaultCamelContext();
    camelContext.addRoutes(new RouteBuilder() {
        public void configure() throws Exception {
            final String publicKeyFileName = FILE + publicKey;
            final String secretKeyFileName = FILE + secretKey;
            final String sourceFile = FILE + source + "?delete=true";
            final String destinationFile = FILE + destination;
            
            PGPDataFormat pgpDataFormat = new PGPDataFormat();
            pgpDataFormat.setKeyFileName(secretKeyFileName);
            pgpDataFormat.setKeyUserid(keyUserid);
            pgpDataFormat.setPassword(keyPassword);
            pgpDataFormat.setArmored(true);
            pgpDataFormat.setSignatureKeyFileName(publicKeyFileName);
            
            from(sourceFile).unmarshal(pgpDataFormat).to(destinationFile);
        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM