I have function for decrypting in Java as such
public String decrypt() throws Exception {
SecretKey secretKey = getSecretKey("o9szYIOq1rRMiouNhNvaq96lqUvCekxR");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
return new String(cipher.doFinal(base64Decode("ASDASDADS")));
}
public SecretKey getSecretKey(String secretKey) throws Exception {
byte[] decodeSecretKey = base64Decode(secretKey);
return new SecretKeySpec(decodeSecretKey, 0, decodeSecretKey.length, "AES");
}
I tried to encrypt the data using CryptoJS as such
function aesEncrypt(data, secretKey) {
var encrypted = CryptoJS.AES.encrypt(data, secretKey, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});
var ciphertext = encrypted.ciphertext.toString();
var wordArray = CryptoJS.enc.Utf8.parse(ciphertext);
var base64 = CryptoJS.enc.Base64.stringify(wordArray);
return base64
}
//call the function
aesEncrypt('Test' , 'o9szYIOq1rRMiouNhNvaq96lqUvCekxR');
When I send the request to JAVA API I get
Given final block not properly padded. Such issues can arise if a bad key is used during decryption
I even tried with forge library. It does not work either.
function aesEncrypt(data, secretKey) {
var cipher = forge.cipher.createCipher('AES-ECB', secretKey)
cipher.start()
cipher.update(forge.util.createBuffer(data))
cipher.finish()
return forge.util.encode64(cipher.output.data)
}
The difference I see between the two is from forge, when I output the data i.e console.log(cipher.output.data) . I get
uJ@^$¿EÅKÖé1ÙN¢cÖúpxÇÅÂëv¥qè9Ï/¨§È5æý»¸,À¿ "y§¯:ßñ[®