如何获取RSAPrivateKey的实例 [英] How do I get an instance of RSAPrivateKey

查看:506
本文介绍了如何获取RSAPrivateKey的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建 java实例。 c> e n .security.interfaces.RSAPrivateKey / code>(假设为BigIntegers)。 (这里 d 表示私人指数, e 公共指数和 n = pq RSA模数。)



我认为这将是非常简单,但我在文档或互联网一般找不到任何东西。 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屋!

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