java 如何通过提供私钥来获得 RSA 公钥?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11345346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get a RSA PublicKey by giving a PrivateKey?
提问by DasDas
I am looking for a Java function that will get an RSA PrivateKey and will return the correct RSA PublicKey?
我正在寻找将获得 RSA PrivateKey 并返回正确 RSA PublicKey 的 Java 函数?
Alternatively, is there a function that will tell us if the RSA PrivateKey/PublicKey is valid?
或者,是否有一个函数可以告诉我们 RSA PrivateKey/PublicKey 是否有效?
回答by Petey B
If you have your private key as an RSAPrivateCrtKeyobject, you can get the public exponent as well as modulous.
如果您将私钥作为RSAPrivateCrtKey对象,则可以获得公共指数和模数。
Then you could create the public key like so:
然后你可以像这样创建公钥:
RSAPublicKeySpec publicKeySpec = new java.security.spec.RSAPublicKeySpec(modulus, exponent);
try {
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);
} catch (Exception e) {
e.printStackTrace();
}
回答by erickson
I can't think of any good reason you'd need this. But here it is:
我想不出你需要这个的任何充分理由。但这里是:
static boolean isValidRSAPair(KeyPair pair)
{
Key key = pair.getPrivate();
if (key instanceof RSAPrivateCrtKey) {
RSAPrivateCrtKey pvt = (RSAPrivateCrtKey) key;
BigInteger e = pvt.getPublicExponent();
RSAPublicKey pub = (RSAPublicKey) pair.getPublic();
return e.equals(pub.getPublicExponent()) &&
pvt.getModulus().equals(pub.getModulus());
}
else {
throw new IllegalArgumentException("Not a CRT RSA key.");
}
}
回答by Steffen Heil
As others have noted, if you have a RSA CRT KEY
, then you can extract the public key from that. However it is actually notpossible to retrieve a public key from a pure private key.
正如其他人所指出的,如果您有一个RSA CRT KEY
,那么您可以从中提取公钥。但是它实际上是不是可以检索从一个纯粹的私有密钥的公共密钥。
The reason for that is easy: When generating RSA keys, there is actually no difference between the private and the public key. One is choosen to be private, the remaining one is public then.
原因很简单:在生成 RSA 密钥时,私钥和公钥之间实际上没有区别。一个选择是私有的,剩下的一个是公共的。
So if you could compute the public key from a pure private key, you could by Definition compute the private key from the public key...
因此,如果您可以从纯私钥计算公钥,则可以通过定义从公钥计算私钥......
If you have both, you can actually easily test if they match:
如果两者都有,您实际上可以轻松测试它们是否匹配:
RSAPublicKey rsaPublicKey = (RSAPublicKey) publicKey;
RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) privateKey;
return rsaPublicKey.getModulus().equals( rsaPrivateKey.getModulus() )
&& BigInteger.valueOf( 2 ).modPow(
rsaPublicKey.getPublicExponent().multiply( rsaPrivateKey.getPrivateExponent() )
.subtract( BigInteger.ONE ),
rsaPublicKey.getModulus() ).equals( BigInteger.ONE );
回答by HRJ
If you have an object of type RSAPrivateKey
then you need to do two things:
如果你有一个类型的对象,RSAPrivateKey
那么你需要做两件事:
- Get the modulus. Easy:
privateKey.getModulus()
- Compute the public exponent. This is a little tricky but not impossible. See the definition of public exponent. Usually, the public exponent is
65537
.
After getting modulus and public exponent, you can follow PeteyB's answer.
获得模数和公共指数后,您可以按照 PeteyB 的答案进行操作。
回答by Sfynx
AFAIK you cannot derive the other key of an RSA key pair, given one key. That would be equivalent to breaking RSA.
AFAIK 给定一个密钥,您无法导出 RSA 密钥对的另一个密钥。这相当于破解 RSA。
For testing a pair, just encrypt something using one key and decrypt it using the other to see if you get the original result back.
为了测试一对,只需使用一个密钥加密某些东西并使用另一个密钥解密它,看看是否能得到原始结果。