【问题标题】:TypeError: Cannot read properties of null (reading '2')TypeError:无法读取 null 的属性(读取“2”)
【发布时间】:2022-01-09 23:24:24
【问题描述】:

我正在做一个 nextJs(带有 typescript)项目,我正在尝试进行一些加密和解密。 我正在使用 nodejs(@types/nodejs)的“加密”模块。 但是当我尝试使用“crypto.publicEncrypt”函数时出现此错误。

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");
};

错误提示“TypeError: Cannot read properties of null (reading '2')”。当我尝试打印 public_key 和缓冲区时,我看到它们不为空。 我不知道为什么会出现这个错误。

这是我的 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"
}

【问题讨论】:

    标签: node.js typescript encryption next.js node-modules


    【解决方案1】:
      const encrypted = crypto.publicEncrypt({
        passphrase: public_key,
        padding: crypto.constants.RSA_PKCS1_PADDING,
      }, buffer);
    
    

    将属性名称 key 更改为 passphrase https://nodejs.org/api/crypto.html#cryptopublicdecryptkey-buffer

    【讨论】:

    • 更改属性名称后,现在显示“TypeError: Cannot read properties of undefined (reading 'toString')”。
    【解决方案2】:

    模块 crypto 没有出现在您的 package.json 中。您可以尝试安装它以及似乎也没有安装的类型。

    【讨论】:

    猜你喜欢
    • 2022-01-12
    • 2021-12-04
    • 2021-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-10
    • 2022-11-17
    • 2023-01-14
    相关资源
    最近更新 更多