V
V
Vadim Misbakh-Soloviev2014-10-26 00:35:42
Java
Vadim Misbakh-Soloviev, 2014-10-26 00:35:42

How to get RSAPublicKey from RSAPrivateKey in >=Android-4.3?

Hello!
My task is to get RSAPublicKey from an object of type RSAPrivateKey (or, to put it more simply, "pull out" a public key from a private one).
Prior to Android 4.3, this approach worked in my application:

RSAPrivateCrtKey private_key = (RSAPrivateCrtKey) KeyFactory.getInstance("RSA")
.generatePrivate(new PKCS8EncodedKeySpec(encoded_byte_array));

PublicKey public_key = KeyFactory.getInstance("RSA")
.generatePublic(new RSAPublicKeySpec(private_key.getModulus(),
private_key.getPublicExponent()));

Tell me, please, how to do it right now?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yaroslav Astafiev, 2014-11-20
@mva

They changed the API from version 4.2 to OpenSSL lib
There is a comprehensive solution here. In your case, the code will look like this:

KeyFactory fact = KeyFactory.getInstance("RSA");
Key pubKey = fact.generatePublic(new RSAPublicKeySpec(m, e));

Where m and e are the modulus and exponent, respectively

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question