How to implement AES ECB PKCS5Padding 128-bit data block encryption in pre-request script?

I need to use AES ECB encryption to generate a parameter in my URL, but the CryptoJS module can not get the right result. Here is my code:

var plainText = "abcdefg1234567890abc"
console.log(plainText)
var key = "abcdefmiddleware";
var result = CryptoJS.AES.encrypt(
      CryptoJS.enc.Utf8.parse(plainText),
      key,
         {
          keySize: 128,
          mode: CryptoJS.mode.ECB,
          padding: CryptoJS.pad.Pkcs7
        }
);
var b64 = result.ciphertext.toString(CryptoJS.enc.Base64);
console.log(b64)
pm.collectionVariables.set("signature",encodeURIComponent(b64));

The output changes every time I run the request, and the correct result should be
“W30nAtv/zP0+oOYMbN1fP7cpAmVWG3Zr63As6vBqA3c=”

Does anyone know how to solve this? Thanks.

you can using external lib : JSEncript … refer to : Guidline How to Using External Lib JSENScrypt in postman

Hi @qwer11121! Welcome to the community :wave:

The issue you are seeing appears to be expected according to this thread.
If you print out result.iv and result.salt, you can see the values change every time.

The following code with pre-determining the value of iv gives the same result every time.

const key = "abcdefmiddleware"
const keyutf = CryptoJS.enc.Utf8.parse(key)
console.log(`keyutf: ${keyutf}`)

const iv = CryptoJS.enc.Base64.parse(key)

const result = CryptoJS.AES.encrypt(
      'testing',
      keyutf,
      { iv }
)

console.log(result)
console.log(`iv: ${result.iv}`)

const b64 = result.ciphertext.toString()
console.log(`encrypted text: ${b64}`)

I hope this helps!