bouncy-gpg BouncyCastle PGP GPG Java API 尝试使用导出的 public 密钥加密时出现 NullPointerException
bouncy-gpg BouncyCastle PGP GPG Java API NullPointerException when trying to encrypt with exported public key
我正在尝试将 bouncy-gpg 库与 BouncyCastle 结合使用,在我的 Java 程序中进行 PGP 加密。我不断收到以下 NPE。
我们有一个 shell 脚本在 Linux 框上调用 gpg
,我想将该逻辑移动到 Windows 上的 Java 应用程序中。我从 Linux 上的密钥环导出 public 密钥并尝试在 Windows 上使用它,但我总是收到以下错误。我已经尝试了各种不同的密钥、格式和 API 选项的变体,但我无法使用这个密钥进行加密。它使用我自己生成的密钥对工作正常。
写的方法
/**
* Encypt the output file using gpg
*/
public void encryptFile() {
Path sourceFile = Paths.get(this.filePath());
Path destFile = Paths.get(this.encryptedFilePath());
try {
BouncyGPG.registerProvider();
int bufferSize = 8 * 1024;
InMemoryKeyring keyringConfig = KeyringConfigs.forGpgExportedKeys(KeyringConfigCallbacks.withUnprotectedKeys());
try {
keyringConfig.addPublicKey(Files.readAllBytes(Paths.get("c:/path/to/my/exported.key"));
} catch ( Exception e ) {
throw new RuntimeException(e);
}
try (
OutputStream fileOutput = Files.newOutputStream(destFile);
BufferedOutputStream bufferedOut = new BufferedOutputStream(fileOutput, bufferSize);
OutputStream outputStream = BouncyGPG
.encryptToStream()
.withConfig(keyringConfig)
.withStrongAlgorithms()
.toRecipient(recipient)
.andDoNotSign()
.binaryOutput()
.andWriteTo(bufferedOut);
InputStream is = Files.newInputStream(sourceFile)
) {
Streams.pipeAll(is, outputStream);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
这里是异常的堆栈跟踪:
Caused by: java.lang.NullPointerException
at name.neuhalfen.projects.crypto.bouncycastle.openpgp.keys.generation.KeyFlag.extractPublicKeyFlags(KeyFlag.java:106)
at name.neuhalfen.projects.crypto.bouncycastle.openpgp.keys.callbacks.Rfc4880KeySelectionStrategy.isEncryptionKey(Rfc4880KeySelectionStrategy.java:228)
at java.util.stream.ReferencePipeline.accept(ReferencePipeline.java:174)
at java.util.ArrayList$Itr.forEachRemaining(ArrayList.java:891)
at java.util.Collections$UnmodifiableCollection.forEachRemaining(Collections.java:1049)
at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
at java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:580)
at java.util.stream.ReferencePipeline.accept(ReferencePipeline.java:270)
at java.util.HashMap$KeySpliterator.forEachRemaining(HashMap.java:1548)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.reduce(ReferencePipeline.java:479)
at name.neuhalfen.projects.crypto.bouncycastle.openpgp.keys.callbacks.Rfc4880KeySelectionStrategy.selectPublicKey(Rfc4880KeySelectionStrategy.java:156)
at name.neuhalfen.projects.crypto.bouncycastle.openpgp.BuildEncryptionOutputStreamAPI$WithAlgorithmSuiteImpl$ToImpl.extractValidKey(BuildEncryptionOutputStreamAPI.java:411)
at name.neuhalfen.projects.crypto.bouncycastle.openpgp.BuildEncryptionOutputStreamAPI$WithAlgorithmSuiteImpl$ToImpl.toRecipient(BuildEncryptionOutputStreamAPI.java:431) ...
以及发生 NPE 的 bouncy-gpg 中的代码块。 hashedSubPackets
变量为空:
while (directKeySignatures.hasNext()) {
final PGPSignature signature = directKeySignatures.next();
final PGPSignatureSubpacketVector hashedSubPackets = signature.getHashedSubPackets();
final int keyFlags = hashedSubPackets.getKeyFlags(); // <- NPE HERE
aggregatedKeyFlags |= keyFlags;
}
非常感谢您的帮助。
带有 https://github.com/neuhalje/bouncy-gpg/issues/48 修复程序的最新 bouncy-gpg 源代码解决了这个问题。我使用导出的 public 密钥重试了我的加密方法并且成功了。
我正在尝试将 bouncy-gpg 库与 BouncyCastle 结合使用,在我的 Java 程序中进行 PGP 加密。我不断收到以下 NPE。
我们有一个 shell 脚本在 Linux 框上调用 gpg
,我想将该逻辑移动到 Windows 上的 Java 应用程序中。我从 Linux 上的密钥环导出 public 密钥并尝试在 Windows 上使用它,但我总是收到以下错误。我已经尝试了各种不同的密钥、格式和 API 选项的变体,但我无法使用这个密钥进行加密。它使用我自己生成的密钥对工作正常。
/**
* Encypt the output file using gpg
*/
public void encryptFile() {
Path sourceFile = Paths.get(this.filePath());
Path destFile = Paths.get(this.encryptedFilePath());
try {
BouncyGPG.registerProvider();
int bufferSize = 8 * 1024;
InMemoryKeyring keyringConfig = KeyringConfigs.forGpgExportedKeys(KeyringConfigCallbacks.withUnprotectedKeys());
try {
keyringConfig.addPublicKey(Files.readAllBytes(Paths.get("c:/path/to/my/exported.key"));
} catch ( Exception e ) {
throw new RuntimeException(e);
}
try (
OutputStream fileOutput = Files.newOutputStream(destFile);
BufferedOutputStream bufferedOut = new BufferedOutputStream(fileOutput, bufferSize);
OutputStream outputStream = BouncyGPG
.encryptToStream()
.withConfig(keyringConfig)
.withStrongAlgorithms()
.toRecipient(recipient)
.andDoNotSign()
.binaryOutput()
.andWriteTo(bufferedOut);
InputStream is = Files.newInputStream(sourceFile)
) {
Streams.pipeAll(is, outputStream);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
这里是异常的堆栈跟踪:
Caused by: java.lang.NullPointerException
at name.neuhalfen.projects.crypto.bouncycastle.openpgp.keys.generation.KeyFlag.extractPublicKeyFlags(KeyFlag.java:106)
at name.neuhalfen.projects.crypto.bouncycastle.openpgp.keys.callbacks.Rfc4880KeySelectionStrategy.isEncryptionKey(Rfc4880KeySelectionStrategy.java:228)
at java.util.stream.ReferencePipeline.accept(ReferencePipeline.java:174)
at java.util.ArrayList$Itr.forEachRemaining(ArrayList.java:891)
at java.util.Collections$UnmodifiableCollection.forEachRemaining(Collections.java:1049)
at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
at java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:580)
at java.util.stream.ReferencePipeline.accept(ReferencePipeline.java:270)
at java.util.HashMap$KeySpliterator.forEachRemaining(HashMap.java:1548)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.reduce(ReferencePipeline.java:479)
at name.neuhalfen.projects.crypto.bouncycastle.openpgp.keys.callbacks.Rfc4880KeySelectionStrategy.selectPublicKey(Rfc4880KeySelectionStrategy.java:156)
at name.neuhalfen.projects.crypto.bouncycastle.openpgp.BuildEncryptionOutputStreamAPI$WithAlgorithmSuiteImpl$ToImpl.extractValidKey(BuildEncryptionOutputStreamAPI.java:411)
at name.neuhalfen.projects.crypto.bouncycastle.openpgp.BuildEncryptionOutputStreamAPI$WithAlgorithmSuiteImpl$ToImpl.toRecipient(BuildEncryptionOutputStreamAPI.java:431) ...
以及发生 NPE 的 bouncy-gpg 中的代码块。 hashedSubPackets
变量为空:
while (directKeySignatures.hasNext()) {
final PGPSignature signature = directKeySignatures.next();
final PGPSignatureSubpacketVector hashedSubPackets = signature.getHashedSubPackets();
final int keyFlags = hashedSubPackets.getKeyFlags(); // <- NPE HERE
aggregatedKeyFlags |= keyFlags;
}
非常感谢您的帮助。
带有 https://github.com/neuhalje/bouncy-gpg/issues/48 修复程序的最新 bouncy-gpg 源代码解决了这个问题。我使用导出的 public 密钥重试了我的加密方法并且成功了。