Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pkcs5 #334

Open
linonetwo opened this issue Jan 24, 2021 · 4 comments
Open

Pkcs5 #334

linonetwo opened this issue Jan 24, 2021 · 4 comments

Comments

@linonetwo
Copy link

linonetwo commented Jan 24, 2021

Seems it supports pkcs5, but only pkcs7 is available now...

Can you support 5?

* PKCS #5/7 padding strategy.


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
Copy link
Author

linonetwo commented Jan 25, 2021

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
Copy link
Author

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

@xhirazi
Copy link

xhirazi commented Oct 27, 2021

Thanks @linonetwo. You save my day.

@adoin
Copy link

adoin commented Jul 22, 2023

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也能解出来吗

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants