From bcf8f25d21d82e3ca4478edd1606084a454bf1ae Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Sun, 13 Feb 2022 00:18:03 +0100 Subject: [PATCH] Use new packet format when armoring key material --- .../java/org/pgpainless/util/ArmorUtils.java | 25 ++++++++++++++++--- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/pgpainless-core/src/main/java/org/pgpainless/util/ArmorUtils.java b/pgpainless-core/src/main/java/org/pgpainless/util/ArmorUtils.java index 9d73635e..ff5b2b6d 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/util/ArmorUtils.java +++ b/pgpainless-core/src/main/java/org/pgpainless/util/ArmorUtils.java @@ -17,6 +17,7 @@ import java.util.regex.Pattern; import org.bouncycastle.bcpg.ArmoredInputStream; import org.bouncycastle.bcpg.ArmoredOutputStream; +import org.bouncycastle.bcpg.BCPGOutputStream; import org.bouncycastle.openpgp.PGPKeyRing; import org.bouncycastle.openpgp.PGPPublicKey; import org.bouncycastle.openpgp.PGPPublicKeyRing; @@ -47,22 +48,38 @@ public final class ArmorUtils { public static String toAsciiArmoredString(PGPSecretKey secretKey) throws IOException { MultiMap header = keyToHeader(secretKey.getPublicKey()); - return toAsciiArmoredString(secretKey.getEncoded(), header); + ByteArrayOutputStream bytes = new ByteArrayOutputStream(); + BCPGOutputStream bcpgOut = new BCPGOutputStream(bytes, true); + secretKey.encode(bcpgOut); + bcpgOut.close(); + return toAsciiArmoredString(bytes.toByteArray(), header); } public static String toAsciiArmoredString(PGPPublicKey publicKey) throws IOException { MultiMap header = keyToHeader(publicKey); - return toAsciiArmoredString(publicKey.getEncoded(), header); + ByteArrayOutputStream bytes = new ByteArrayOutputStream(); + BCPGOutputStream bcpgOut = new BCPGOutputStream(bytes, true); + publicKey.encode(bcpgOut); + bcpgOut.close(); + return toAsciiArmoredString(bytes.toByteArray(), header); } public static String toAsciiArmoredString(PGPSecretKeyRing secretKeys) throws IOException { MultiMap header = keysToHeader(secretKeys); - return toAsciiArmoredString(secretKeys.getEncoded(), header); + ByteArrayOutputStream bytes = new ByteArrayOutputStream(); + BCPGOutputStream bcpgOut = new BCPGOutputStream(bytes, true); + secretKeys.encode(bcpgOut); + bcpgOut.close(); + return toAsciiArmoredString(bytes.toByteArray(), header); } public static String toAsciiArmoredString(PGPPublicKeyRing publicKeys) throws IOException { MultiMap header = keysToHeader(publicKeys); - return toAsciiArmoredString(publicKeys.getEncoded(), header); + ByteArrayOutputStream bytes = new ByteArrayOutputStream(); + BCPGOutputStream bcpgOut = new BCPGOutputStream(bytes, true); + publicKeys.encode(bcpgOut); + bcpgOut.close(); + return toAsciiArmoredString(bytes.toByteArray(), header); } public static String toAsciiArmoredString(PGPSecretKeyRingCollection secretKeyRings) throws IOException {