Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

240
Views
JavaScript encrypted xml throws error in Java application decryption: Given final block not properly padded. Such issues can arise if a bad key is

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 specific problem, at encryption level there many different options like AES CBC or GCM. I hope you can help me. We can change only the sender code!

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!