we have a sender application (node.js) which sends to a JAVA application some symmetric encrypted XML strings with an AES 128 key.
When the JAVA application tries to decrypt the content with the provided key the following error will be thrown:
javax.crypto.BadPaddingException: Given final block not properly padded. Such issues can arise if a bad key is used during decryption.
at com.sun.crypto.provider.CipherCore.unpad(CipherCore.java:975) at
com.sun.crypto.provider.CipherCore.fillOutputBuffer(CipherCore.java:1056)
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:853) at
com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:446) at
javax.crypto.Cipher.doFinal(Cipher.java:2168)
public generateKey(): Buffer {
return crypto.generateKeySync('aes', { length: 128 }).export();
}
// Encrypt content with key
public encryptSymmetric(content: string, encryptionKey: Buffer): Buffer {
const iv = Buffer.from(crypto.randomBytes(this.IV_LENGTH)).toString('hex').slice(0, this.IV_LENGTH);
const cipher = crypto.createCipheriv('aes-128-cbc', encryptionKey, iv);
let encrypted = cipher.update(content);
encrypted = Buffer.concat([encrypted, cipher.final()]);
return encrypted;
//return iv + ':' + encrypted.toString('hex');
}
JAVA RECEIVER: Decrypt with key
private static byte[] decryptContent(SecretKeySpec key, byte[] content) throws GeneralSecurityException {
Cipher cipher = Cipher.getInstance('AES');
cipher.init(Cipher.DECRYPT_MODE, key);
return cipher.doFinal(content); // EXCEPTION
}
I have no idea to solve this specfi problem, at encryption level there many different optins like AES CBC or GCM, hope you can help me, we can change only the sender code!