I need to integrate with a third-party REST API. The body of my request (as well as the response) need to be encrypted using AES encryption using an AES key that was communicated to us. There are a few requirements:
- Use a Rijndael cipher
- Electronic Code Book mode (ECB)
- No built-in padding
Edit 1:
This is an extract from the Integration Guide I was provided:
All URL parameters must be encrypted using AES with a shared key. Additionally, we are able to support all widely used encryption algorithms. Standard AES encryption algorithm may be used by the client system to generate the secure token (Integration Token). The following AES cipher attributes should be used:
- Rijndael cipher
- Electronic Code Book mode (ECB)
- No built in padding (such as PKCS)
- Resultant encrypted buffer should be manually padded with spaces to achieve the total length a multiple of 32. That is: length(encrypted padded string) mod 32 = 0
For example, instantiate the cipher and initialize it using Java standard SunJCE cryptological library the code would contain: Cipher cipher = Cipher.getInstance("Rijndael/ECB/NoPadding", "SunJCE");
End of Edit 1
I tried to play a little bit with some snippets of code that I found mostly here and I wrote the following sequence:
var clearMessage = "The brown fox jumps over the laxy dog";
console.log("Clear message: ", clearMessage);
var encodingKey = CryptoJS.enc.Hex.parse("0123456789ABCDEF0123456789ABCDEF");
var encryptedMessage = CryptoJS.AES.encrypt(CryptoJS.enc.Utf8.parse(clearMessage), encodingKey, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.NoPadding});
console.log("Encrypted message: ", encryptedMessage.ciphertext.toString());
const decryptedMessage = CryptoJS.AES.decrypt(encryptedMessage.ciphertext.toString(), encodingKey, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.NoPadding});
clearMessage = decryptedMessage.toString(CryptoJS.enc.Utf8);
console.log("Decrypted message: ", clearMessage);
In the console of my browser, I see this output:
Clear message: The brown fox jumps over the laxy dog
Encrypted message: 56aaf639f44c106889aa4a765d2bf7c83a7860a379d776982991c7575eb63fd31f7c9ce3db
crypto-js.js:523 Uncaught Error: Malformed UTF-8 data
at Object.stringify (crypto-js.js:523:24)
at WordArray.init.toString (crypto-js.js:278:38)
at encryptDecrypt (index.html:157:39)
at HTMLButtonElement.onclick (index.html:46:60)
Spelling mistake aside ("laxy dog" ?!!?), what is wrong with my decrypting sequence? The error message is triggered on this line of code:
clearMessage = decryptedMessage.toString(CryptoJS.enc.Utf8);
Edit 2:
This is what chatgpt has generated for this question:
// Import the necessary libraries
var CryptoJS = require("crypto-js");
// Set the plaintext message and secret key
var message = "This is a secret message!";
var secretKey = "ThisIsASecretKey";
// Convert the key and message to WordArrays (required by CryptoJS)
var key = CryptoJS.enc.Utf8.parse(secretKey);
var plaintext = CryptoJS.enc.Utf8.parse(message);
// Encrypt the plaintext message using AES with Rijndael cipher, ECB mode, and no padding
var ciphertext = CryptoJS.AES.encrypt(plaintext, key, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.NoPadding,
cipher: CryptoJS.algo.Rijndael
});
// Print the encrypted ciphertext in Base64 format
console.log(ciphertext.toString());
// Decrypt the ciphertext message using AES with Rijndael cipher, ECB mode, and no padding
var decrypted = CryptoJS.AES.decrypt(ciphertext, key, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.NoPadding,
cipher: CryptoJS.algo.Rijndael
});
// Convert the decrypted message back to plaintext and print it
console.log(decrypted.toString(CryptoJS.enc.Utf8));
It gives me the exact same error when I try to decode the cipher.
End of Edit 2
Any ideas or suggestions?
TIA, Ed