如何获取RSAPrivateKey的实例 [英] How do I get an instance of RSAPrivateKey
问题描述
我认为这将是非常简单,但我在文档或互联网一般找不到任何东西。 p>
如果这是任何帮助,我已经安装了BouncyCastle。
编辑澄清:我正在寻找一个实现接口的类,并采取 d
, e
和/或 n
作为构造函数(或工厂函数的参数等)的参数,而不是创建一个新的随机密钥或从一些PKCS *格式的文件中读取密钥。
这里是如何构造一个给定的私人指数和模数(所有这是私钥所需要的) :
KeyFactory keyFactory = KeyFactory.getInstance(RSA);
RSAPrivateKeySpec privateKeySpec =新RSAPrivateKeySpec(
新的BigInteger(57791d5430d593164082036ad8b29fb157791d5430d593164082036ad8b29fb157791d5430d593164082036ad8b29fb157791d5430d593164082036ad8b29fb1,16),
新的BigInteger(57791d5430d593164082036ad8b29fb157791d5430d593164082036ad8b29fb157791d5430d593164082036ad8b29fb157791d5430d593164082036ad8b29fb1,16)
);
RSAPrivateKey privateKey =(RSAPrivateKey)keyFactory.generatePrivate(privateKeySpec);
How do I create an instance of java.security.interfaces.RSAPrivateKey given RSA's d
, e
and n
(let's assume as BigIntegers). (Here d
denotes the private exponent, e
the public exponent and n=pq
the RSA modulus.)
I thought this would be really simple but I can't find anything in the documentation or the internet in general.
If that it is any help, I already have BouncyCastle installed.
Edit to clarify: I'm looking for a class that implements the interface and takes d
, e
and/or n
as arguments to the constructor (or argument to a factory function, etc.) as opposed to creating a new, random key or reading the key from a file in some PKCS* format.
Well, here is how you can construct one given the private exponent and modulus ( all that is required for the private key ):
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
RSAPrivateKeySpec privateKeySpec = new RSAPrivateKeySpec(
new BigInteger("57791d5430d593164082036ad8b29fb157791d5430d593164082036ad8b29fb157791d5430d593164082036ad8b29fb157791d5430d593164082036ad8b29fb1", 16),
new BigInteger("57791d5430d593164082036ad8b29fb157791d5430d593164082036ad8b29fb157791d5430d593164082036ad8b29fb157791d5430d593164082036ad8b29fb1", 16)
);
RSAPrivateKey privateKey = (RSAPrivateKey) keyFactory.generatePrivate(privateKeySpec);
这篇关于如何获取RSAPrivateKey的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!