SO I have this code
private static RSAPrivateKey buildRSAPrivateKey(String privateKey) {
PEMReader pemReader = new PEMReader(new StringReader(privateKey));
try {
KeyPair pair = (KeyPair) pemReader.readObject();
RSAPrivateKey result = (RSAPrivateKey)pair.getPrivate();
pemReader.close();
return result;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
It works fine using bouncycastle
, however I need this to function on Android, so I imported SpongyCastle
, however spongy castle does not have PEMReader
(apparently PEMReader
is in an older bouncycastle version)
How can I create an RSAPrivateKey
equivalent to the above code without using bouncycastle's PEMReader
?
I hope, at least this answer will help others. Because bouncy castle is totally deprecated its support for android P.
Security.insertProviderAt(new org.spongycastle.jce.provider.BouncyCastleProvider(), 1);
StringReader reader = new StringReader(privateKeySerialized); /*privateKeySerialized -> RSA key*/
PublicKey pKey = null;
try {
PemReader pemReader = new PemReader(reader);
PEMParser pemParser = new PEMParser(pemReader);
PEMKeyPair keyPair = (PEMKeyPair) pemParser.readObject();
JcaPEMKeyConverter converter = new JcaPEMKeyConverter();
pKey = converter.getPublicKey(keyPair.getPublicKeyInfo());
pemReader.close();
} catch (IOException i) {
i.printStackTrace();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With