I am currently facing issue with applying pgp encryption with public key on text content as string where recipient as UID(916A6C32).
Java code gives exception as :”org.bouncycastle.openpgp.PGPException: No (suitable) public key for encryption to 916A6C32 foundntat name.neuhalfen.projects.crypto.bouncycastle.openpgp.BuildEncryptionOutputStreamAPI$WithAlgorithmSuiteImpl$ToImpl.extractValidKey(BuildEncryptionOutputStreamAPI.java:414)ntat name.neuhalfen.projects.crypto.bouncycastle.openpgp.BuildEncryptionOutputStreamAPI$WithAlgorithmSuiteImpl$ToImpl.toRecipient(BuildEncryptionOutputStreamAPI.java:431″
Below code snippet works when pgp keys generated with recipients as email format and It is able to encrypt with public key with recipient as email format but it does not work with recipient as UID.
It is only issue with below java code where as command line works with command “gpg –output /tmp/otuputfile.txt.pgp –recipient 916A6C32 –encrypt pathTotextFile”
final var output = new ByteArrayOutputStream();
final var inputStream = new ByteArrayInputStream(input.getBytes(StandardCharsets.UTF_8));
final var keyringConfig = KeyringConfigs
.forGpgExportedKeys(KeyringConfigCallbacks.withUnprotectedKeys());
keyringConfig.addPublicKey(publicKey.getBytes(StandardCharsets.UTF_8));
try (
final var outputStream = BouncyGPG
.encryptToStream()
.withConfig(keyringConfig)
.withStrongAlgorithms()
.toRecipient(recipient)
.andDoNotSign()
.armorAsciiOutput()
.andWriteTo(output)
) {
Streams.pipeAll(inputStream, outputStream);
}
encryptedContent = output.toString(StandardCharsets.UTF_8);
I am using library : name.neuhalfen.projects.crypto.bouncycastle.openpgp
bouncy-gpg
2.3.0
I have done research but could not find any clue for this issue.
Any help much appreciated
thanks