Problem encrypting with PGP with elliptic curve

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Problem encrypting with PGP with elliptic curve

Heiko Mattes
Hello,

I'm trying to send a PGP encrypted email to the recepient of a public
PGP key. The key uses an elliptic curve. But when trying to encrypt
data, I always get an 'Invalid point encoding 0x40' error.

The public key seems to be fine, as when I'm loading it, I get a
seemingly valid master key with one sub key usable for encryption. But
when trying to use said encryption key, it doesn't work.

Below, I added the public key, the stack trace and also the code I use
for encryption, which works for 'normal' (non-elliptic curves) PGP keys.

I hope, somebody can help me.

Regards
Heiko Mattes


Public key:
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: SKS 1.1.6

mDMEXl1WjhYJKwYBBAHaRw8BAQdAoPlx4e6UlAd0tDq8SPjwNHqUciv+FybLYrPocBJ6Ze20
HlJvYiBEZW5uaXMgPHJvYmRAdGVsZWNvbTI2LmNoPoiQBBMWCAA4FiEEtDGzEElVJ9+SNbQu
ZgxU5RXBR+oFAl5dVo4CGwMFCwkIBwIGFQoJCAsCBBYCAwECHgECF4AACgkQZgxU5RXBR+oS
ywD/RowXCrbr8dj9uVpuVKe2FFN+SdlWk/xae0LlniAeJ6QBAK+SnvX2bVStf1XIUxupqheZ
zj+W8kojFNXPK9UBECwIuDgEXl1WjhIKKwYBBAGXVQEFAQEHQBC8TTeQKgW1ml2S/uMrYETD
w56ilf/FTTTdViCJjiVGAwEIB4h4BBgWCAAgFiEEtDGzEElVJ9+SNbQuZgxU5RXBR+oFAl5d
Vo4CGwwACgkQZgxU5RXBR+pY+QD/ap3BMh/ottU4nzEg7Vo2lF/IxsBTLKkKXaXxN4a19O0B
ALUL3OLNjjcGZzKaNkkg0MGjwg/S+1xod7+75Jk3CmMD
=GN3J
-----END PGP PUBLIC KEY BLOCK-----

Stack trace:
java.lang.IllegalArgumentException: Invalid point encoding 0x40
    at org.bouncycastle.math.ec.ECCurve.decodePoint(ECCurve.java:436)
    at
org.bouncycastle.openpgp.operator.bc.BcUtil.decodePoint(BcUtil.java:102)
    at
org.bouncycastle.openpgp.operator.bc.BcPublicKeyKeyEncryptionMethodGenerator.encryptSessionInfo(BcPublicKeyKeyEncryptionMethodGenerator.java:105)
    at
org.bouncycastle.openpgp.operator.PublicKeyKeyEncryptionMethodGenerator.generate(PublicKeyKeyEncryptionMethodGenerator.java:97)
    at
org.bouncycastle.openpgp.PGPEncryptedDataGenerator.open(PGPEncryptedDataGenerator.java:205)
    at
org.bouncycastle.openpgp.PGPEncryptedDataGenerator.open(PGPEncryptedDataGenerator.java:333)

Sample code I'm using for encryption:
    // bytes = some data to encrypt, algorithm = AES_128, asciiArmored =
true, zip = true, publicKeys = the above shown encryption key.
    private static final byte[] encrypt(final byte[] bytes, final int
algorithm, final boolean acsiiArmored, final boolean zip, final
PGPPublicKey... publicKeys) throws IOException, PGPException {
        final ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        try (OutputStream out = acsiiArmored ? new
ArmoredOutputStream(byteOut) : byteOut) {
            final BcPGPDataEncryptorBuilder encBuilder = new
BcPGPDataEncryptorBuilder(algorithm);
            encBuilder.setWithIntegrityPacket(true);
            final PGPEncryptedDataGenerator encDataGen = new
PGPEncryptedDataGenerator(encBuilder);
            for (final PGPPublicKey publicKey : publicKeys) {
                final BcPublicKeyKeyEncryptionMethodGenerator
encMethodGen = new BcPublicKeyKeyEncryptionMethodGenerator(publicKey);
                encDataGen.addMethod(encMethodGen);
            }
            try (OutputStream encDataGenOut = encDataGen.open(out, new
byte[1 << 16])) { // <--- Here the exception happens.
                if (zip) {
                    final PGPCompressedDataGenerator comDataGen = new
PGPCompressedDataGenerator(CompressionAlgorithmTags.ZIP);
                    final OutputStream comDataOut =
comDataGen.open(encDataGenOut);
                    final PGPLiteralDataGenerator lDataGen = new
PGPLiteralDataGenerator();
                    try (OutputStream lDataOut =
lDataGen.open(comDataOut, PGPLiteralData.BINARY, PGPLiteralData.CONSOLE,
PGPLiteralData.NOW, new byte[1024])) {
                        lDataOut.write(bytes);
                    }
                    comDataGen.close();
                } else {
                    final PGPLiteralDataGenerator lDataGen = new
PGPLiteralDataGenerator();
                    try (OutputStream lDataOut =
lDataGen.open(encDataGenOut, PGPLiteralData.BINARY,
PGPLiteralData.CONSOLE, PGPLiteralData.NOW, new byte[1024])) {
                        lDataOut.write(bytes);
                    }
                }
            }
        }

        return byteOut.toByteArray();
    }