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

Snippet: TripleDes / Des encrypt & decrypt result are not same as crypto-js #3

Open
trasherdk opened this issue Aug 20, 2022 · 1 comment

Comments

@trasherdk
Copy link
Owner

CryptoES.TripleDES.encrypt(DATA, CryptoES.enc.Utf8.parse(KEY), { mode: CryptoES.mode.ECB, format: CryptoES.format.OpenSSL });

As title, seems crypto-es is unable to produce the same output in both encrypt and decrypt as crypto-js. The output seems incorrect.

Source: entronad#22 (comment)

Sample

DES decryption got different results, in CryptoJS I got the correct result, in CryptoES I need to split the encrypted HEX string to get the correct result.

With CryptoJS
https://codepen.io/brownsugar/pres/OJjJPYY?editors=0012

With CryptoES
https://codesandbox.io/s/cryptoes-des-decryption-zt4cs

Source: entronad#22 (comment)

@trasherdk trasherdk changed the title Example: TripleDes / Des encrypt & decrypt result are not same as crypto-js Snippet: TripleDes / Des encrypt & decrypt result are not same as crypto-js Apr 28, 2024
@trasherdk
Copy link
Owner Author

CryptoES Des

import CryptoES from "crypto-es";

// No need to split string in CryptoJS
// https://codepen.io/brownsugar/pres/OJjJPYY?editors=0012
function decrypt(string, key) {
  const chunkLength = string.length / 2;
  const strings = [string.slice(0, chunkLength), string.slice(chunkLength)];
  const values = strings.map((s) => decryptDES(s, key));
  return values.join("").replace(/\0/g, "");
}
function decryptDES(encrypted, keyString) {
  const ciphertext = CryptoES.enc.Hex.parse(encrypted);
  const key = CryptoES.enc.Utf8.parse(keyString);

  const decrypted = CryptoES.DES.decrypt(
    {
      ciphertext
    },
    key,
    {
      mode: CryptoES.mode.ECB,
      padding: CryptoES.pad.NoPadding
    }
  );

  return decrypted.toString(CryptoES.enc.Utf8);
}

const data = [
  "1;8D709DF37DD0F75702B98B13B48CCAC989421B49",
  "1;B6C052F5259BC340C489A1C391BFCB67AFDC7D1B",
  "1;65CE64E708401E956DF015E0BB9BE238B22AC4DD",
  "1;761F348ACE19C9C6B7EFAB062B737950C71C5B48",
  "1;38ADD29F4F24AEDE6D45A35A46DD5BD36E1DE3EA",
  "1;29810AD241B5293ABA4E04B79F7C9C960AD4D779",
  "1;965909504145BFEF3B5FDB01FD0B086787B1CC48",
  "1;1CFFF0630506B58C9254D3F0DB59561A8DFD37C7",
  "1;717A70C67008B436A6EFA38C2182C8BCF04E3C53",
  "1;1348011174AD38961DD969DAEF75D5920CC91403"
];

const result = data.map((d) => {
  const [, string] = d.split(";");
  const keyLength = 8;
  const key = string.slice(0, keyLength);
  const hexString = string.slice(keyLength);
  return decrypt(hexString, key);
});

console.log(result);

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

1 participant