Convert opensh public key to ssh2 format (RFC 4716)

The main question is simple. Split openssh public key into rfc 4716 compatible format. The only catch, however, is that it should be in java .

Using ssh-keygen , this is just one line:

 ssh-keygen -e -f openssh_key.pub 

Unfortunately, I could not find other sources to do this in Java. Even any algorithm or steps necessary for the conversion are not mentioned. They all revolve around using ssh-keygen . Of course, I can use java.exec to invoke the command, but this is the worst case scenario.


An example of an openssh key (saved it in code format to save spaces / newlines as created):

 ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDwxgE7D3HYLYddNHLMFK8OfpRwwUSgxiB8fbecvkCUEktSpWikvsWTyCnl5p3uSmsGg/F1lwVPXuuVlQ4VZlYqMuEBEMRF9ADdXWWNxjO/Hd7688ow7ocncxl0xKXsH5Fc9GHvE8yfUh94F8Qm9x8M8Uux+XsNEvPG8KI/QUJWndIsHv+m//3nbEEqUTAlzsyY0mjHW/dPORhXcB5WeGH+cBRAhcp5JGKAq26TOsuNY8H+nrlxX6z03xbUN28HHdXv6uKZfpnVpl6tM0khxbh7F+tLYWeUIZ+nYaDBPINv8Mkd6Duqe/GOLtgVUIR76Adijok4w5oaKlTq27xzMurl kaushik@kaushik-HP 

Designed using ssh-keygen:

 ---- BEGIN SSH2 PUBLIC KEY ---- Comment: "2048-bit RSA, converted by kaushik@kaushik-HP from OpenSSH" AAAAB3NzaC1yc2EAAAADAQABAAABAQDwxgE7D3HYLYddNHLMFK8OfpRwwUSgxiB8fbecvk CUEktSpWikvsWTyCnl5p3uSmsGg/F1lwVPXuuVlQ4VZlYqMuEBEMRF9ADdXWWNxjO/Hd76 88ow7ocncxl0xKXsH5Fc9GHvE8yfUh94F8Qm9x8M8Uux+XsNEvPG8KI/QUJWndIsHv+m// 3nbEEqUTAlzsyY0mjHW/dPORhXcB5WeGH+cBRAhcp5JGKAq26TOsuNY8H+nrlxX6z03xbU N28HHdXv6uKZfpnVpl6tM0khxbh7F+tLYWeUIZ+nYaDBPINv8Mkd6Duqe/GOLtgVUIR76A dijok4w5oaKlTq27xzMurl ---- END SSH2 PUBLIC KEY ---- 

UPDATE: I created a default conversion implementation for those who have similar needs.

+7
java openssh
source share
2 answers

Base64 data in two formats are identical - you do not need to do anything. All you need to do to convert between these formats is add / remove line breaks (70 characters) and change the title / trailer.

Note that both formats have a comment - kaushik@kaushik-HP in the OpenSSH key and a line starting with Comment: in the PEM key. Both of them are completely optional and do not need to be converted.

+2
source share

Well, I donโ€™t know if I should laugh or cry, because I finally found the exact functionality that I need, and also a lot more in the JSch Library . This was, of course, after I wrote my own implementation for the conversion. Hence my dilemma about laughing / screaming. In the meantime, happy, I learned something new in this process.

A small example of using the JSch KeyPair class provided:

Creating a private-public key (RSA) pair:

 JSch jSch = new JSch(); KeyPair keyPair = KeyPair.genKeyPair(jSch, KeyPair.RSA); keyPair.writePrivateKey("privateKey"); //store private key in file - 'privateKey' keyPair.writePublicKey("publicKey"); //store public key in file - 'publickKey' 

Convert the public key to RFC 4716 format.

 keyPair.writeSECSHPublicKey("ssh2Key"); //store ssh2 public key in file - 'ssh2Key' 
0
source share

All Articles