我正在做一个nextJs(使用typescript)项目,其中我试图做一些加密和解密。我使用的是nodejs(@types/nodejs)的'crypto‘模块。但是当我尝试使用'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和buffer时,我发现它们不是空的。我不知道为什么我会得到这个错误。
这是我的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"
}发布于 2021-12-03 15:56:12
const encrypted = crypto.publicEncrypt({
passphrase: public_key,
padding: crypto.constants.RSA_PKCS1_PADDING,
}, buffer);将属性名称key更改为passphrase https://nodejs.org/api/crypto.html#cryptopublicdecryptkey-buffer
发布于 2021-12-03 15:49:48
模块加密不会出现在您的package.json中。您可以尝试安装它,以及似乎也没有安装的类型。
https://stackoverflow.com/questions/70216913
复制相似问题