cryptography - Public key from private key in java is incorrect -


i have private key , need calculate public key in java. following answer get public key private in java came following code:

string pemstring = "full private key string here"; pemstring = pemstring.replace("-----begin rsa private key-----\n", ""); pemstring = pemstring.replace("-----end rsa private key-----", ""); pemstring = pemstring.replace("\n", ""); //without exception: illegal base64 character logger.info(pemstring);  byte[] decoded = base64.getdecoder().decode(pemstring); keyfactory kf = keyfactory.getinstance("rsa");  pkcs8encodedkeyspec keyspec = new pkcs8encodedkeyspec(decoded); privatekey privatekey = kf.generateprivate(keyspec); rsaprivatecrtkey privk = (rsaprivatecrtkey)privatekey;  rsapublickeyspec publickeyspec = new rsapublickeyspec(privk.getmodulus(), privk.getpublicexponent()); publickey pubkey = kf.generatepublic(publickeyspec); string pubstring = base64.getencoder().encodetostring(pubkey.getencoded());  logger.info(pubstring); 

but if compare output of code public key ec2 stores in .ssh/authorized keys not same. doing wrong?

i want achieve ssh-keygen -y

the key generating in x.509 format. while key in .ssh/authorized_keys2 in ssh format. can use ssh-keygen utility conversion:

ssh-keygen -i -m pem -f key.pem > ssh.pub 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -