I got an encrypting key in HEX format and its intialization vector (HEX), the length of both of them is 32, I got an encrypted phone number from the client side, and I should decrypt it using the same encryption key. Here is the method:
private static final String key = "*";
private static final String initVector = "*";
private static final String characterEncoding = "UTF-8";
private static final String cipherTransformation = "AES/CBC/PKCS5PADDING";
private static final String aesEncryptionAlgorithem = "AES";
public static String decrypt(String encryptedText) {
String decryptedText = "";
try {
Cipher cipher = Cipher.getInstance(cipherTransformation);
byte[] keyByte = key..getBytes(characterEncoding);
SecretKeySpec secretKey = new SecretKeySpec(keyByte, aesEncryptionAlgorithem);
IvParameterSpec ivparameterspec = new IvParameterSpec(initVector.getBytes());
cipher.init(Cipher.DECRYPT_MODE, secretKey, ivparameterspec);
Base64.Decoder decoder = Base64.getDecoder();
byte[] cipherText = decoder.decode(encryptedText.getBytes("UTF8"));
decryptedText = new String(cipher.doFinal(cipherText), "UTF-8");
} catch (Exception E) {
System.err.println("decrypt Exception : "+E.getMessage());
}
return decryptedText;
}
the length of the given key and Iv is 32, I got this exception: Encrypt Exception : Wrong IV length: must be 16 bytes long I think I should convert the key and the Iv to another format in order to have the wanted length.