Generate SSH Key Using java -


i tried generate ssh key using following code

        keypairgenerator keypairgenerator = keypairgenerator.getinstance("rsa");          keypairgenerator.initialize(2048);          keypair keypair=keypairgenerator.generatekeypair();           rsapublickey publickey=(rsapublickey)keypair.getpublic();          rsaprivatekey privatekey=(rsaprivatekey)keypair.getprivate();           string base64pubkey = base64.encodebase64string(publickey.getencoded());          bytearrayoutputstream byteos = new bytearrayoutputstream();          dataoutputstream dos = new dataoutputstream(byteos);          dos.writeint("ssh-rsa".getbytes().length);          dos.write("ssh-rsa".getbytes());          dos.writeint(publickey.getpublicexponent().tobytearray().length);          dos.write(publickey.getpublicexponent().tobytearray());          dos.writeint(publickey.getmodulus().tobytearray().length);          dos.write(publickey.getmodulus().tobytearray());          string publickeyencoded = new string(                                     base64.encodebase64(byteos.tobytearray()));          string key =  "ssh-rsa " + publickeyencoded + " ";          system.out.println("public key ------");          system.out.println(key);            system.out.println("------------------------------");          system.out.println("private key");          system.out.println(base64.encodebase64(privatekey.getencoded())); 

now, when store content of private key in file , trying validate putty says invalid format of private key.

could guys me out on this, how missing private key format putty not recognizing it.

putty unable recognize openssh formatted keys -- must first converted putty style using puttygen.

there debian/ubuntu package conversion:

apt-get install putty-tools puttygen openssh_formattted_key -o putty_formatted_key.ppk 

you might able investigate , figure out how keys converted, or run command process inside code.


on windows, puttygen gui available this:

https://devops.profitbricks.com/tutorials/use-ssh-keys-with-putty-on-windows/

download link: https://the.earth.li/~sgtatham/putty/latest/x86/puttygen.exe

if want use key generated directly, suggest use mingw32/mingw64, cygwin, etc. allow use ssh command in command prompt or other terminal-like window.


Comments