gpt4 book ai didi

java - bouncy caSTLe pgp - PartialInputStream 中的流过早结束

转载 作者:太空宇宙 更新时间:2023-11-04 10:42:02 37 4
gpt4 key购买 nike

我正在使用 bouncy caSTLe pgp 和 bouncy-gpg ( https://github.com/neuhalje/bouncy-gpg ),我想做非常简单的事情:

ByteArrayOutputStream cryptoBytes = new ByteArrayOutputStream();
try {

final OutputStream outputStream = BouncyGPG
.encryptToStream()
.withConfig(keyringConfig)
.withStrongAlgorithms()
.toRecipient(recipient)
.andDoNotSign()
.binaryOutput()
.andWriteTo(cryptoBytes);

Streams.pipeAll(input, outputStream);
outputStream.flush();
} catch (Exception e) {
e.printStackTrace();
}

ByteArrayOutputStream plainBytes = new ByteArrayOutputStream();
try {
final InputStream plaintextStream = BouncyGPG
.decryptAndVerifyStream()
.withConfig(keyringConfig)
.andIgnoreSignatures()
.fromEncryptedInputStream(new ByteArrayInputStream(cryptoBytes.toByteArray()));
Streams.pipeAll(plaintextStream, plainBytes);
} catch (Exception e) {
e.printStackTrace();
}

但是我在尝试解密时遇到异常:

java.io.EOFException: premature end of stream in PartialInputStream
at org.bouncycastle.bcpg.BCPGInputStream$PartialInputStream.read(Unknown Source)
at org.bouncycastle.bcpg.BCPGInputStream.read(Unknown Source)
at org.bouncycastle.bcpg.SymmetricEncIntegrityPacket.<init>(Unknown Source)

我没有使用任何字符串转换,所以我无法理解为什么字节数组长度存在问题

最佳答案

事实上,您必须关闭OutputStream。原因是GPG将签名写在流的末尾。这是通过关闭流来触发的。

try {

final OutputStream outputStream = BouncyGPG
.encryptToStream()
.withConfig(keyringConfig)
.withStrongAlgorithms()
.toRecipient(recipient)
.andDoNotSign()
.binaryOutput()
.andWriteTo(cryptoBytes);

Streams.pipeAll(input, outputStream);
outputStream.close(); // <<<----
} catch (Exception e) {
e.printStackTrace();
}

example在自述文件中使用了“try-with-resources”,它会自动关闭流。我澄清了项目中的 README,以使这一点更加清晰。

关于java - bouncy caSTLe pgp - PartialInputStream 中的流过早结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48870074/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com