使用 Apache Camel 对 *.bai.asc 文件进行 PGP 解密

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

我正在尝试使用 Apache Camel Encrypt/Decrypt 文件。

maven 依赖:

    <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>

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();
}

加密工作正常,但在尝试解密 *.bai.asc 文件时,出现以下异常:

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.access1(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

我也尝试过使用 PGPDataFormat 进行解密。但我仍然遇到同样的例外。 我在代码中缺少什么?请提出建议。

显然我需要添加用于签名的密钥环的文件名(Public 密钥)。这是我设置 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);
        }
    });