Posts

Showing posts from March, 2022

AES encrypt-decrypt in Java

 public static String decrypt(byte[] data, byte[] key) throws NoSuchPaddingException, NoSuchAlgorithmException,             BadPaddingException, IllegalBlockSizeException, InvalidKeyException {         Cipher aes = Cipher.getInstance("AES/ECB/PKCS5Padding");         aes.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key,     "AES"));         return new String(Base64.getDecoder().decode(Base64.getEncoder().encodeToString((aes.doFinal(data))))); } public static String encrypt(byte[] data, byte[] key) throws NoSuchPaddingException, NoSuchAlgorithmException,             BadPaddingException, IllegalBlockSizeException, InvalidKeyException {         Cipher aes = Cipher.getInstance("AES/ECB/PKCS5Padding");         aes.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key,     "AES"));         return Ba...