提问人:dim5b 提问时间:11/7/2023 更新时间:11/7/2023 访问量:14
当 armored 为 false 时,Camel PGP 解密失败
Camel PGP Decryption fails when armored is false
问:
我使用 Camel 创建了一个简单的演示,以便加密/解密基于 PGP 加密的文件。代码如下所示。
package com.example.demo;
import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.converter.crypto.PGPDataFormat;
import org.apache.camel.impl.DefaultCamelContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class PGPEncryptor {
static final Logger LOG = LoggerFactory.getLogger(PGPEncryptor.class);
final String originalPath = "pgp/original";
final String encryptedPath = "pgp/encrypted";
final String decryptedPath = "pgp/decrypted";
public static void main(String[] args) {
try {
PGPEncryptor pgp = new PGPEncryptor();
pgp.runEncryption();
pgp.runDecryption();
} catch (Exception e) {
LOG.error(e.getMessage(), e);
}
}
private void runEncryption() throws Exception {
CamelContext ctx = new DefaultCamelContext();
ctx.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
PGPDataFormat encryptFormat = new PGPDataFormat();
encryptFormat.setKeyFileName("file:keys/pubring.gpg");
encryptFormat.setKeyUserid("XXXXX");
encryptFormat.setArmored(true);
// TODO setting armored to false (which is default) fails?
from("file:" + originalPath + "?noop=true&charset=utf-8")
.marshal(encryptFormat)
.to("file:" + encryptedPath + "?charset=utf-8");
}
});
ctx.start();
// Maybe sleep a little here
Thread.sleep(4000);
ctx.stop();
try {
ctx.close();
} catch (Exception e) {
// do nothing
}
}
private void runDecryption() throws Exception {
CamelContext camelContext = new DefaultCamelContext();
camelContext.addRoutes(new RouteBuilder() {
public void configure() throws Exception {
PGPDataFormat decryptFormat = new PGPDataFormat();
decryptFormat.setKeyFileName("file:keys/secring.gpg");
decryptFormat.setKeyUserid("XXXXX");
decryptFormat.setPassword("XXXX");
decryptFormat.setArmored(false);
from("file:" + encryptedPath + "?noop=true&charset=utf-8")
.log("message ${headers}")
.unmarshal(decryptFormat)
.to("file:" + decryptedPath + "?charset=utf-8");
}
});
camelContext.start();
// Maybe sleep a little here
Thread.sleep(4000);
camelContext.stop();
try {
camelContext.close();
} catch (Exception e) {
// do nothing
}
}
}
如果设置为 true,则对原始文件夹中的文件进行加密解密。
如果 armored 设置为 false 或未设置,因为它是默认值,
解密失败,并显示:encryptFormat.setArmored(true);
如果文件是由其他人使用我的公钥签名的(即直接从我的加密文件夹中读取)
java.io.EOFException: premature end of stream in PartialInputStream
at org.bouncycastle.bcpg.BCPGInputStream$PartialInputStream.read(Unknown Source)
at java.base/java.io.BufferedInputStream.fill(BufferedInputStream.java:244)
at java.base/java.io.BufferedInputStream.read1(BufferedInputStream.java:284)
at java.base/java.io.BufferedInputStream.read(BufferedInputStream.java:343)
或者,如果文件是由演示代码使用 armor false 加密的
java.lang.IllegalArgumentException: The input message body has an invalid format. The PGP decryption/verification processor expects a sequence of PGP packets of the form (entries in brackets are optional and ellipses indicate repetition, comma represents sequential composition, and vertical bar separates alternatives): Public Key Encrypted Session Key ..., Symmetrically Encrypted Data | Sym. Encrypted and Integrity Protected Data, (Compressed Data,) (One Pass Signature ...,) Literal Data, (Signature ...,)
at org.apache.camel.converter.crypto.PGPKeyAccessDataFormat.getFormatException(PGPKeyAccessDataFormat.java:491)
at org.apache.camel.converter.crypto.PGPKeyAccessDataFormat.getDecryptedData(PGPKeyAccessDataFormat.java:437)
at org.apache.camel.converter.crypto.PGPKeyAccessDataFormat.unmarshal(PGPKeyAccessDataFormat.java:372)
at org.apache.camel.support.processor.UnmarshalProcessor.process(UnmarshalProcessor.java:76)
自从我的名为 pubring.kbx 的密钥箱文件以来,我已将密钥导出为旧格式
gpg --export > pubring.gpg
gpg --export-secret-keys > secring.gpg
我是否缺少其他配置?因为我的要求被 false 武装了。我正在使用最新的 BC 1.76 骆驼。
答: 暂无答案
评论