将 SubjectPublicKeyInfo 转换为 Java PublicKey
Convert SubjectPublicKeyInfo to Java PublicKey
我的应用程序使用 bouncycastle 来解码比 java 原生 X509Certificate 提供更深的证书,实际上我有 bouncy castle 证书 (org.bouncycastle.asn1.x509.Certificate
)。
我的问题是如何将 org.bouncycastle.asn1.x509.SubjectPublicKeyInfo
安全地转换为 java.security.Publickey
。 ?
我想使用标准 java 签名方法 java.security.Signature
而不是 BouncyCastle 替代方法。
Signature sig = Signature.getInstance(signatureAlgorithm.getAlgorithm().getId());
sig.initVerify(getPublicKey());
sig.update(content);
return sig.verify(signature);
一种转换密钥的方法是使用 RSAKeyParameter 和 RSAPublicKeySpec 'to rebuild' 使用模数和指数的密钥。
RSAKeyParameters rsa = (RSAKeyParameters) PublicKeyFactory.createKey(certificate.getSubjectPublicKeyInfo());
RSAPublicKeySpec rsaSpec = new RSAPublicKeySpec(rsa.getModulus(), rsa.getExponent());
KeyFactory kf = KeyFactory.getInstance("RSA");
java.security.Publickey publicKey = kf.generatePublic(rsaSpec);
我的应用程序使用 bouncycastle 来解码比 java 原生 X509Certificate 提供更深的证书,实际上我有 bouncy castle 证书 (org.bouncycastle.asn1.x509.Certificate
)。
我的问题是如何将 org.bouncycastle.asn1.x509.SubjectPublicKeyInfo
安全地转换为 java.security.Publickey
。 ?
我想使用标准 java 签名方法 java.security.Signature
而不是 BouncyCastle 替代方法。
Signature sig = Signature.getInstance(signatureAlgorithm.getAlgorithm().getId());
sig.initVerify(getPublicKey());
sig.update(content);
return sig.verify(signature);
一种转换密钥的方法是使用 RSAKeyParameter 和 RSAPublicKeySpec 'to rebuild' 使用模数和指数的密钥。
RSAKeyParameters rsa = (RSAKeyParameters) PublicKeyFactory.createKey(certificate.getSubjectPublicKeyInfo());
RSAPublicKeySpec rsaSpec = new RSAPublicKeySpec(rsa.getModulus(), rsa.getExponent());
KeyFactory kf = KeyFactory.getInstance("RSA");
java.security.Publickey publicKey = kf.generatePublic(rsaSpec);