JSCH - Invalid private key

I guess that your key is not in OpenSSH key file format. JSch expects the private key to be in OpenSSH format.

You can use PuTTYgen to convert your private key to work with OpenSSH by following the steps described here:

  1. Press Load and select the Private Key that was created with PuTTYgen.
  2. Enter the passphrase to load the key.
  3. From the Conversions menu select export OpenSSH key
  4. Save the private key.

Perhaps not a solution for you, but I found this question when I searched for my issue.

I had accidentally given the path to the public keyfile when JSCH expected the private keyfile.


You can use PEMWriter to convert your private key to PEM format that will be accepted by JSch

The following example converts a key returned from Java KeyStore (JKS)

Key privateKey = KeyStore.getKey(privateKeyAlias, keyStorePassword);//get key from JKS
StringWriter stringWriter = new StringWriter();
PEMWriter pemWriter = new PEMWriter(stringWriter);
pemWriter.writeObject(privateKey);
pemWriter.close();

byte[] privateKeyPEM = stringWriter.toString().getBytes();

Tags:

Java

Jsch