Java: Encrypt and encode string with public key
Task: Write method to encrypt and encode string with public key
Implementation:
public byte[] encryptAndEncode(String data)
throws NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeyException, IllegalBlockSizeException,
BadPaddingException {
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, publicKey); // Use public key object.
return Base64.getEncoder().encode(cipher.doFinal(data.getBytes()));
}
Done.