如何从PKCS10CertificationRequest使用新的Bouncy城​​堡库获取PublicKey? [英] How to get PublicKey from PKCS10CertificationRequest using new Bouncy Castle library?

查看:281
本文介绍了如何从PKCS10CertificationRequest使用新的Bouncy城​​堡库获取PublicKey?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在新版本的Bouncy Castle库中, PKCS10CertificationRequest 中有变化。在以前的版本中,可以使用 getPublicKey()方法从这样的请求中获得 PublicKey (参见old doc )。



现在这个方法消失了。如何从这样的请求中获得PublicKey?
getSubjectPublicKeyInfo()。parsePublicKey()但它返回 ASN1Primitive



我看到从SPKAC NetscapeCertRequest 我仍然可以通过调用 getPublicKey()

解决方案

在主提供程序包中有一个实用程序类,名为公共钥匙工厂。方法 createKey 返回您投射到的 AsymmetricKeyParameter 任何类型的公共密钥是合适的,例如

  SubjectPublicKeyInfo pkInfo = pkcs10CertReq.getSubjectPublicKeyInfo 
RSAKeyParameters rsa =(RSAKeyParameters)PublicKeyFactory.createKey(pkInfo);

编辑1:



创建 java.security.PublicKey 需要几个步骤:

  RSAPublicKeySpec rsaSpec = new RSAPublicKeySpec(rsa.getModulus(),rsa.getExponent()); 
KeyFactory kf = KeyFactory.getInstance(RSA);
PublicKey rsaPub = kf.generatePublic(rsaSpec);


In the new version of Bouncy Castle library there are changes in PKCS10CertificationRequest. In previous versions it was possible to get PublicKey from such request using getPublicKey() method (see old doc).

Now this method disappered. How can I get PublicKey from with from such request? There is getSubjectPublicKeyInfo().parsePublicKey() but it returns ASN1Primitive.

I see that from SPKAC NetscapeCertRequest I still can read PublicKey directly by calling getPublicKey().

解决方案

There is a utility class in the main provider package called PublicKeyFactory. The method createKey returns an AsymmetricKeyParameter which you cast to whatever type of public key is appropriate, e.g.

SubjectPublicKeyInfo pkInfo = pkcs10CertReq.getSubjectPublicKeyInfo();
RSAKeyParameters rsa = (RSAKeyParameters) PublicKeyFactory.createKey(pkInfo);

EDIT 1:

In addition, to create a java.security.PublicKey a few more steps are needed:

RSAPublicKeySpec rsaSpec = new RSAPublicKeySpec(rsa.getModulus(), rsa.getExponent());
KeyFactory kf = KeyFactory.getInstance("RSA");
PublicKey rsaPub = kf.generatePublic(rsaSpec);

这篇关于如何从PKCS10CertificationRequest使用新的Bouncy城​​堡库获取PublicKey?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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