Java 如何从 byte[] 数组中恢复 RSA 公钥?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2411096/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 07:14:53  来源:igfitidea点击:

How to recover a RSA public key from a byte[] array?

javacryptographyrsa

提问by kiewic

I'm wondering if it's possible to recover a RSA public key that I have converted to byte array previously.

我想知道是否有可能恢复我之前转换为字节数组的 RSA 公钥。

byte[] keyBytes = publicKey.getEncoded();

Thanks for the help.

谢谢您的帮助。

采纳答案by Bozho

PublicKey publicKey = 
    KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(bytes));

For more info see this tutorial

有关更多信息,请参阅本教程

回答by Marko Kotar

For others who want to get private key instead of public key from byte array:

对于想要从字节数组中获取私钥而不是公钥的其他人:

PrivateKey privateKey = KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(privateKeyBytes));

回答by Adrien Louis Combecau

Great answer. Thanks for the link. Just for complete, I found this Converted secret key into bytes, how to convert it back to secrect key?

很好的答案。谢谢你的链接。为了完整起见,我发现这个Converted secret key into bytes,如何将其转换回secrect key?

SecretKey key2 = new SecretKeySpec(data, 0, data.length, "DES");

and just worked very well.

并且工作得很好。