我获得了一个公钥ID,并被要求使用此公钥ID来加密.txt文件。我可以找到关于如何执行此操作的参考,但是在使用Bouncycastle的C#语言中,而在Java中没有任何内容。
特定公钥ID由加密文件收件人提供。解密由加密文件接收者完成,因此我不关心任何解密或私钥ID,如果这些信息是必不可少的,这是我暂时不知道的。
我正在使用Java和非常新的加密技术,请指导我使用任何最新的Java应用程序示例或教程,使用给定的特定公钥ID而不是生成的公钥来加密文本文件。谢谢!
答案 0 :(得分:0)
您可以使用“原始”Bouncy Castle for Java或围绕API的几个包装器之一。要使用Bouncy Castle,您需要了解OpenPGP RFC(rfc4880)。
或者你可以使用现有的包装纸,例如Bouncy GPG:
final String original_message = "I love deadlines. I like the whooshing sound they make as they fly by. Douglas Adams";
// Most likely you will use one of the KeyringConfigs.... methods.
// These are wrappers for the test.
KeyringConfig keyringConfigOfSender = Configs
.keyringConfigFromResourceForSender();
ByteArrayOutputStream result = new ByteArrayOutputStream();
try (
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(result, 16384 * 1024);
final OutputStream outputStream = BouncyGPG
.encryptToStream()
.withConfig(keyringConfigOfSender)
.withStrongAlgorithms()
.toRecipients("recipient@example.com", "sender@example.com")
.andSignWith("sender@example.com")
.binaryOutput()
.andWriteTo(bufferedOutputStream);
// Maybe read a file or a webservice?
final ByteArrayInputStream is = new ByteArrayInputStream(original_message.getBytes())
) {
Streams.pipeAll(is, outputStream);
// It is very important that outputStream is closed before the result stream is read.
// The reason is that GPG writes the signature at the end of the stream.
// This is triggered by closing the stream.
// In this example outputStream is closed via the try-with-resources mechanism of Java
}
result.close();
byte[] chipertext = result.toByteArray();