给定两个SSH2密钥,如何检查它们是否属于Java中的同一密钥对? [英] Given two SSH2 keys how do I check that they belong to the same key pair in Java?

查看:124
本文介绍了给定两个SSH2密钥,如何检查它们是否属于Java中的同一密钥对?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图找到一种方法来验证两个SSH2密钥,一个私有密钥和一个公共密钥,属于同一个密钥对。我使用 JSch 来加载和解析私钥。

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.

更新:可以显示如何从私钥(SSH2 RSA)重新生成公钥的代码片段可以解决问题。

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

推荐答案

你可以用 BouncyCastle 轻量级API。

You could do this with the BouncyCastle lightweight API.

例如:

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);

RSAKeyParameters 类表示实际密钥。

希望有所帮助!

这篇关于给定两个SSH2密钥,如何检查它们是否属于Java中的同一密钥对?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆