Open linonetwo opened 3 years ago
This is how I do it:
const bytes = crypto.AES.decrypt(
dataStr,
crypto.enc.Utf8.parse(realAESKey),
{
mode: crypto.mode.CBC,
padding: crypto.pad.Pkcs7,
iv: crypto.enc.Utf8.parse(realAESIV),
},
);
const decryptedStr = crypto.enc.Utf8.stringify(bytes);
return decryptedStr.toString();
And encrypt:
const key = generateRandomStr(32);
const text = JSON.stringify(content);
const encryptKey = crypto.enc.Utf8.parse(key);
const iv = generateRandomStr(16);
const encryptedStr = crypto.AES.encrypt(
crypto.enc.Utf8.parse(text),
encryptKey,
{
mode: crypto.mode.CBC,
padding: crypto.pad.Pkcs7,
iv: crypto.enc.Utf8.parse(iv),
},
);
And Java side can use Pkcs5 to inter-op with the result from JS.
Note that Java side may first use Base64 decode before decrypt dataString, but in JS we don't need to do so.
Thanks @linonetwo. You save my day.
This is how I do it:
const bytes = crypto.AES.decrypt( dataStr, crypto.enc.Utf8.parse(realAESKey), { mode: crypto.mode.CBC, padding: crypto.pad.Pkcs7, iv: crypto.enc.Utf8.parse(realAESIV), }, ); const decryptedStr = crypto.enc.Utf8.stringify(bytes); return decryptedStr.toString();
And encrypt:
const key = generateRandomStr(32); const text = JSON.stringify(content); const encryptKey = crypto.enc.Utf8.parse(key); const iv = generateRandomStr(16); const encryptedStr = crypto.AES.encrypt( crypto.enc.Utf8.parse(text), encryptKey, { mode: crypto.mode.CBC, padding: crypto.pad.Pkcs7, iv: crypto.enc.Utf8.parse(iv), }, );
And Java side can use Pkcs5 to inter-op with the result from JS.
好像和本来写法差不多,这样后端如果是pkcs5也能解出来吗
Seems it supports pkcs5, but only pkcs7 is available now...
Can you support 5?
https://github.com/brix/crypto-js/blob/90884e679206162183b979067209d51668e4751d/src/cipher-core.js#L369
This is very confusing that you didn't export the pkcs5, but If user search through issues, he will found this finally: