Convert SubjectPublicKeyInfo to Java PublicKey

argoth :

My app is using bouncycastle to decode certificates deeper then java native X509Certificate provide, actualy i have bouncy castle certificates ( org.bouncycastle.asn1.x509.Certificate).

My question is how to secure convert org.bouncycastle.asn1.x509.SubjectPublicKeyInfo to a java.security.Publickey. ?

i'd like to use the standard java signature method java.security.Signature instead BouncyCastle alternatives.

Signature sig = Signature.getInstance(signatureAlgorithm.getAlgorithm().getId());
        sig.initVerify(getPublicKey());
        sig.update(content);
        return sig.verify(signature);
argoth :

A way to convert the key is using a RSAKeyParameter and RSAPublicKeySpec 'to rebuild' the key using the Modulus and Exponent.

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);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=135519&siteId=1