I have public key sent to me. I want to use it in encryption. But key is in .pub format. So its just a plain text, not a certificate. How do I convert this public key to an object like PublicKey key = ....; In file I see: -----BEGIN PUBLIC KEY----- MIIBIjANB......... HwIDAQAB -----END PUBLIC KEY----- Later I want to use this code:
enter code here
Private static byte[] encrypt(String text, PublicKey key) {
byte[] cipherText = null;
try {
final Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, key);
cipherText = cipher.doFinal(text.getBytes());
} catch (Exception e) {
e.printStackTrace();
}
return cipherText;
}
Please someone tell me how to use .pub file to have a public key in program code.