Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Given two SSH2 keys how do I check that they belong to the same key pair in Java?

I'm trying to find a way to validate that two SSH2 keys, one private and one public, belong to the same key pair. I have used JSch for loading and parsing the private key.

Update: A snippet that could show how to regenerate the public key from the private key (SSH2 RSA) would solve the problem.

like image 675
Andrei Savu Avatar asked Dec 16 '22 19:12

Andrei Savu


1 Answers

You could do this with the BouncyCastle lightweight API.

For example:

InputStream in = new FileInputStream("path/to/private/key");
AsymmetricKeyParameter privateKey = PrivateKeyFactory.createKey(in);
RSAPrivateCrtKeyParameters rsaPrivateKey = (RSAPrivateCrtKeyParameters)privateKey;
BigInteger modulus = rsaPrivateKey.getModulus();
BigInteger publicExponent = rsaPrivateKey.getPublicExponent();
RSAKeyParameters publicKeyParams = new RSAKeyParameters(false, modulus, publicExponent);

The RSAKeyParameters class represents the actual key.

Hope that helps!

like image 188
Cameron Skinner Avatar answered Apr 27 '23 00:04

Cameron Skinner