Skip to content

OpenSSHPrivateKeyUtil.encodePrivateKey does not work with ECDSA key pairs #2240

Closed

Description

@AlainKnaff

Following code does not work for ECDSA keys (with secp256r1):

        AsymmetricKeyParameter bprv =
            PrivateKeyFactory.createKey(key.getEncoded());
        encodedKey = OpenSSHPrivateKeyUtil.encodePrivateKey(bprv);

It produces a private key which cannot be used neither by openssh, nor by jssh.

However, Topaco's answer to https://stackoverflow.com/questions/79262968/how-to-convert-a-ecprivatekey-to-a-pem-encoded-openssh-format works all right.

OpenSSHPrivateKeyUtil.encodePrivateKey does work fine for Ed25519.

It doesn't work for RSA either, but I'm not sure whether it's supposed to, as RSA is supposed to be packed in a different PEM wrapper (which has -----BEGIN PRIVATE KEY----- instead of -----BEGIN OPENSSH PRIVATE KEY-----).

Activity

  1. dghgit commented on May 4, 2026

    @dghgit
    Contributor

    I think this is now fixed. Please try what's on https://www.bouncycastle.org/betas

  2. AlainKnaff commented on May 4, 2026

    @AlainKnaff
    Author

    Indeed, it works now for ECDSA with new ECGenParameterSpec("secp256r1"), thanks.

    It also works with RSA, by bracketing it with -----BEGIN RSA PRIVATE KEY----- and -----END RSA PRIVATE KEY----- rather than BEGIN OPENSSH PRIVATE KEY

    Many thanks,

    Alain

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions