给定两个 SSH2 密钥,我如何检查它们是否属于 Java 中的同一密钥对?

发布于 2024-10-07 08:01:45 字数 181 浏览 4 评论 0原文

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

素手挽清风 2024-10-14 08:01:45

您可以使用 BouncyCastle 轻量级 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 类表示实际密钥。

希望有帮助!

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!

岁吢 2024-10-14 08:01:45

我使用 Java 安全性(普通 J2SE)编写了这个 util 方法:

public static boolean validateRSAKeyPair(RSAPrivateCrtKey privateKey, RSAPublicKey publicKey) {
    BigInteger n = publicKey.getModulus();
    BigInteger e = publicKey.getPublicExponent();
    BigInteger d = privateKey.getPrivateExponent();
    BigInteger p = privateKey.getPrimeP();
    BigInteger q = privateKey.getPrimeQ();

    BigInteger pq = p.multiply(q);//shold equal to n
    BigInteger eulerMod = p.add(NEGATIVE_ONE).multiply(q.add(NEGATIVE_ONE));// φ(n)=(p-1)*(q-1)
    BigInteger mod = d.multiply(e).mod(eulerMod);// (d*e) mod φ(n), should be 1
    return n.equals(pq) && BigInteger.ONE.equals(mod);
}

它通过 RSA 算法的参数验证 RSA 密钥对。

I've wrote this util method using Java security (plain J2SE):

public static boolean validateRSAKeyPair(RSAPrivateCrtKey privateKey, RSAPublicKey publicKey) {
    BigInteger n = publicKey.getModulus();
    BigInteger e = publicKey.getPublicExponent();
    BigInteger d = privateKey.getPrivateExponent();
    BigInteger p = privateKey.getPrimeP();
    BigInteger q = privateKey.getPrimeQ();

    BigInteger pq = p.multiply(q);//shold equal to n
    BigInteger eulerMod = p.add(NEGATIVE_ONE).multiply(q.add(NEGATIVE_ONE));// φ(n)=(p-1)*(q-1)
    BigInteger mod = d.multiply(e).mod(eulerMod);// (d*e) mod φ(n), should be 1
    return n.equals(pq) && BigInteger.ONE.equals(mod);
}

It verifies RSA key pair by parameters of RSA algorithm.

平定天下 2024-10-14 08:01:45

也许这就是您正在寻找的: How do you test a public /private DSA 密钥对?

更新:对于纯 Java 解决方案,请查看标准化 Java 加密扩展 (JCE):http://download.oracle.com/javase/1.4.2/docs/guide/security/jce/JCERefGuide.html# KeyGenerator

更新 2:这是来自 RSA 的代码示例(名为“go”的方法生成密钥对):http://www.rsa.com/products/bsafe/documentation/cryptoj35html/doc/dev_guide/group__CJ__SAMPLES__RSANOPAD__JCE.html

更新 3:这是处理解析 JCE 公钥的链接:我们如何将字符串从 PEM 转换为 DER 格式

Maybe this is what you're looking for: How do you test a public/private DSA keypair?

Update: For a pure Java solution, take a look at the standardized Java Cryptography Extension (JCE): http://download.oracle.com/javase/1.4.2/docs/guide/security/jce/JCERefGuide.html#KeyGenerator

Update 2: Here's a code sample from RSA (the method named "go" generates a key-pair): http://www.rsa.com/products/bsafe/documentation/cryptoj35html/doc/dev_guide/group__CJ__SAMPLES__RSANOPAD__JCE.html

Update 3: And here's a link that deals with parsing a public key for JCE: How do we convert a String from PEM to DER format

羁〃客ぐ 2024-10-14 08:01:45

我知道这是作弊,但您可以调用类似的过程吗:

ssh-keygen -f ~/.ssh/id_rsa -y > ~/.ssh/id_rsa.pub

请参阅 this

I know is cheating but can you invoke a process like :

ssh-keygen -f ~/.ssh/id_rsa -y > ~/.ssh/id_rsa.pub

See this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文