This code is part of AES algorithm, does encryption and decryption, I have this method that return the encrypted value as [Hex], when I feed the encrypted value to be the input for decrypt method, I got this error: [Input length must be multiple of 16 when decrypting with padded cipher], any suggestion to fix it, please?
public static String encrypt(String Data) throws Exception {
Key key = generateKey();
Cipher c = Cipher.getInstance(ALGO);
c.init(Cipher.ENCRYPT_MODE, key);
byte[] encVal = c.doFinal(Data.getBytes());
// simply store the encoded byte array here
byte[] bytes = Base64.getEncoder().encode(encVal);
// loop over the bytes and append each byte as hex string
StringBuilder sb = new StringBuilder(bytes.length * 2);
for(byte b : bytes)
sb.append(String.format("%02x", b));
return sb.toString();
}
public static String decrypt(String encryptedData) throws Exception {
Key key = generateKey();
Cipher c = Cipher.getInstance(ALGO);
c.init(Cipher.DECRYPT_MODE, key);
byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData);
byte[] decValue = c.doFinal(decordedValue);
String decryptedValue = new String(decValue);
return decryptedValue;
}