英文:
bouncy-gpg BouncyCastle PGP GPG Java API NullPointerException when trying to encrypt with exported public key
问题
这里是你提供的代码部分的翻译:
/**
 * 使用 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$2$1.accept(ReferencePipeline.java:174)
	at java.util.ArrayList$Itr.forEachRemaining(ArrayList.java:891)
	at java.util.Collections$UnmodifiableCollection$1.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$7$1.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
  aggregatedKeyFlags |= keyFlags;
}
非常感谢您提前的帮助。
英文:
I am trying to use the bouncy-gpg library with BouncyCastle to do PGP encryption in my Java programs.  I keep getting the below NPE.
We had a shell script calling gpg on a Linux box and I wanted to move that logic into a Java app on Windows.  I exported the public key from the keyring on Linux and try to use it on Windows but I always get the below error.  I have tried all sorts of variations of different keys, formats and API options and I can't get encryption working with this one key.  It worked OK with a key pair I generated myself.
Here is the method I wrote based on https://github.com/neuhalje/bouncy-gpg/blob/master/examples/encrypt/src/main/java/name/neuhalfen/projects/crypto/bouncycastle/openpgp/example/EncryptMain.java
  /**
   * 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);
    }
And here is the stack trace of the exception:
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$2$1.accept(ReferencePipeline.java:174)
	at java.util.ArrayList$Itr.forEachRemaining(ArrayList.java:891)
	at java.util.Collections$UnmodifiableCollection$1.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$7$1.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) ...
And the code block in bouncy-gpg where the NPE is occurring.  The hashedSubPackets variable is null:
    while (directKeySignatures.hasNext()) {
      final PGPSignature signature = directKeySignatures.next();
      final PGPSignatureSubpacketVector hashedSubPackets = signature.getHashedSubPackets();
      final int keyFlags = hashedSubPackets.getKeyFlags(); // <- NPE HERE
      aggregatedKeyFlags |= keyFlags;
    }
Thanks very much in advance for any help.
答案1
得分: 0
最新的 bouncy-gpg 源代码已经修复了 https://github.com/neuhalje/bouncy-gpg/issues/48 的问题。我使用导出的公钥重新尝试了我的加密方法,成功了。
英文:
The latest bouncy-gpg source code with the fix for https://github.com/neuhalje/bouncy-gpg/issues/48 resolved this issue. I retried my encryption method with the exported public key and it worked.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论