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.

Leave a Reply

Your email address will not be published. Required fields are marked *

9  +    =  15

Skynin cache:
Array ( [cache mode] => capture [requestKey] => 94c64e9cb526b06a2accd5e0985ac48604.sc [skycache_disable_capture] => false )