i encrypt my string in JAVA like:
public String encrypt(String toEncrypt) {
try {
Cipher c = Cipher.getInstance("AES");
c.init(Cipher.ENCRYPT_MODE, aesKey);
byte[] encVal = c.doFinal(toEncrypt.getBytes());
return Base64.getEncoder().encodeToString(encVal);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
and I want to decrypt it in typescript:
public static decrypt(key, value){
key = btoa(key);
let decryptedData = CryptoJS.AES.decrypt(value, key, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});
return decryptedData.toString( CryptoJS.enc.Utf8 );
}
the decryptedData.toString( CryptoJS.enc.Utf8 ) returns the following Error: Malformed UTF-8 data
I followed along this tutorial
toEncrypt.getBytes()withtoEncrypt.getBytes("UTF-8")in your Java code? Your error mentions malformed UTF-8 data converting the decrypted bytes to UTF-8 text, so it's possible your source string wasn't converted to bytes using UTF-8 before being encrypted.