qwer11121
(qwer11121)
November 26, 2019, 8:38am
1
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.
Hoc.Tran
(Y Hoc Tran Do)
January 12, 2021, 3:25am
2
taehoshino
(Tae Hoshino)
January 12, 2021, 4:32am
3
Hi @qwer11121 ! Welcome to the community
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!