brix / crypto-js

JavaScript library of crypto standards.
Other
15.77k stars 2.38k forks source link

Pkcs5 #334

Open linonetwo opened 3 years ago

linonetwo commented 3 years ago

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:

Apparently PKCS#5 is a subset of PKCS#7

linonetwo commented 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.

linonetwo commented 3 years ago

Note that Java side may first use Base64 decode before decrypt dataString, but in JS we don't need to do so.

xhirazi commented 2 years ago

Thanks @linonetwo. You save my day.

adoin commented 1 year 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.

好像和本来写法差不多,这样后端如果是pkcs5也能解出来吗