sshj icon indicating copy to clipboard operation
sshj copied to clipboard

If this can generate public and private key pairs?

Open Laggar opened this issue 6 years ago • 4 comments
trafficstars

If this can generate public and private key pairs

Laggar avatar Dec 21 '18 06:12 Laggar

@Laggar Please explain what you want to do.

hierynomus avatar Jan 14 '19 14:01 hierynomus

Is there a convenience method to generate a public and private ssh key pair? Similar to the following one offered by JSch: http://www.jcraft.com/jsch/examples/KeyGen.java.html

grahamrb avatar Mar 20 '19 20:03 grahamrb

@grahamrb - you could have a look at this example which isn't strictly linked to SSHJ but works with it.

dhubbard-ic avatar Mar 21 '19 09:03 dhubbard-ic

You can generate a key pair with this snippet:

KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(2048);
KeyPair pair = keyGen.generateKeyPair();
keyProvider = new KeyPairWrapper(pair);
Buffer.PlainBuffer buf = new Buffer.PlainBuffer().putPublicKey(pair.getPublic());
publicKey = "ssh-rsa " + Base64.encodeBytes(buf.array(), buf.rpos(), buf.available()) + " [email protected]";

You can then authenticate using the keyProvider:

sshClient.auth("opc", List.of(new AuthPublickey(keyProvider)));

yawkat avatar Feb 23 '23 12:02 yawkat