Home > database >  TypeError: Cannot read properties of null (reading '2')
TypeError: Cannot read properties of null (reading '2')

Time:12-04

I am working on a nextJs(with typescript) project where I'm trying to do some encryption and decryption. I'm using 'crypto' module of nodejs(@types/nodejs). But I'm getting this error when I'm trying to use 'crypto.publicEncrypt' function.

const crypto = require('crypto');
//import * as crypto from "crypto";

export const encryptSensitiveData = ({
  sensitive_data,
  public_key,
}: {
  sensitive_data: string;
  public_key: string;
}) => {
  const buffer = Buffer.from(sensitive_data, "utf8");


  const encrypted = crypto.publicEncrypt(
    {
      key: public_key,
      padding: crypto.constants.RSA_PKCS1_PADDING,
    },
    buffer
  );

  return encrypted.toString("base64");
};

The error says "TypeError: Cannot read properties of null (reading '2')". When I tried to print the public_key and buffer, I saw that they aren't null. I don't know why I'm getting this error.

This is my package.json

{
  "name": "ecommerce",
  "version": "1.0.0",
  "scripts": {
    "dev": "next",
    "build": "next build",
    "build-static-page": "next build && next export",
    "start": "next start -p $PORT",
    "type-check": "tsc",
    "heroku-postbuild": "npm run build"
  },
  "dependencies": {
    "@styled-system/theme-get": "^5.1.2",
    "@types/react-router-dom": "^5.3.1",
    "@types/uniqid": "^5.3.1",
    "axios": "^0.22.0",
    "chart.js": "^2.9.4",
    "clsx": "^1.1.1",
    "date-fns": "^2.21.1",
    "formik": "^2.2.6",
    "fs": "^0.0.1-security",
    "lodash": "^4.17.20",
    "next": "^10.1.3",
    "nprogress": "^0.2.0",
    "pure-react-carousel": "^1.27.6",
    "react": "^16.14.0",
    "react-alert": "^7.0.3",
    "react-chartjs-2": "^2.11.1",
    "react-countdown": "^2.3.2",
    "react-country-dropdown": "^1.0.4",
    "react-country-region-selector": "^3.4.0",
    "react-custom-scrollbars": "^4.2.1",
    "react-dom": "^16.14.0",
    "react-dropzone": "^11.2.4",
    "react-google-recaptcha": "^2.1.0",
    "react-icons": "^4.2.0",
    "react-nextjs-toast": "^1.2.5",
    "react-paginate": "^7.0.0",
    "react-redux": "^7.2.5",
    "react-router-dom": "^5.3.0",
    "react-scroll": "^1.8.2",
    "react-select": "^3.1.1",
    "react-svg": "^11.2.1",
    "react-toast-notifications": "^2.5.1",
    "redux": "^4.1.1",
    "redux-devtools-extension": "^2.13.9",
    "redux-thunk": "^2.3.0",
    "styled-components": "^5.2.1",
    "styled-system": "^5.1.5",
    "uniqid": "^5.4.0",
    "yup": "^0.32.8"
  },
  "devDependencies": {
    "@types/lodash": "^4.14.167",
    "@types/node": "^12.20.37",
    "@types/react": "^16.14.2",
    "@types/react-dom": "^16.9.10",
    "@types/react-redux": "^7.1.18",
    "@types/redux": "^3.6.0",
    "@types/redux-thunk": "^2.1.0",
    "@types/styled-components": "^5.1.7",
    "@types/styled-system": "^5.1.10",
    "babel-plugin-styled-components": "^1.12.0",
    "cross-env": "^7.0.3",
    "react-context-devtool": "^2.0.3",
    "typescript": "^4.2.4"
  },
  "browser": {
    "fs": false,
    "path": false,
    "os": false
  },
  "license": "MIT"
}

CodePudding user response:

The module crypto does not appear in your package.json. You can try installing it, along with the types, which do not appear to be installed either.

CodePudding user response:

  const encrypted = crypto.publicEncrypt({
    passphrase: public_key,
    padding: crypto.constants.RSA_PKCS1_PADDING,
  }, buffer);

change property name key to passphrase https://nodejs.org/api/crypto.html#cryptopublicdecryptkey-buffer

  • Related