Skip to content

Why decrypt is not working in Nextjs on production ? TypeError: Cannot read properties of undefined (reading 'parse') #519

Description

@croissant1998

I use crypto-js for encrypt and decrypt some value but it's work fine on dev mode. I call function for encrypt on client component (after click button) and then decrypt in server component which value decrypt as a route params. I got error TypeError: Cannot read properties of undefined (reading 'parse') only on production mode. I'm not sure, I do something wrong.

import * as CryptoJS from "crypto-js";

const key = CryptoJS.enc.Utf8.parse(
  process.env.NEXT_PUBLIC_CRYPTO_JS_KEY
);
const iv = CryptoJS.enc.Utf8.parse(process.env.NEXT_PUBLIC_CRYPTO_JS_IV);

const aesEncryption = (data: string) => {
  try {
    const encrypted = CryptoJS.AES.encrypt(data, key, { iv });
    return encrypted.toString();
  } catch (error) {
    console.error(error);
    return "";
  }
};

const aesDecryption = (encryptedCode: string) => {
  try {
    const bytes = CryptoJS.AES.decrypt(encryptedCode, key, { iv });
    return bytes.toString(CryptoJS.enc.Utf8);
  } catch (error) {
    console.error(error);
    return "";
  }
};

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions