我正在做一个nextJs(使用TypeScrip)项目,我正在尝试进行一些加密和解密。我使用的是NodeJS(@Types/NodeJS)的‘crypto’模块。但是,当我尝试使用‘Crypto.Public Encrypt’函数时,我收到了这个错误。
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 Property of Null(Reading‘2’)”。当我尝试打印PUBLIC_KEY和BUFFER时,我发现它们不是空的。我不知道为什么我会收到这个错误。
这是我的包裹。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"
}
4条答案
按热度按时间niwlg2el1#
将属性名称
key
更改为passphrase
https://nodejs.org/api/crypto.html#cryptopublicdecryptkey-bufferpw9qyyiw2#
我在Angular 上使用加密浏览器时遇到了这个错误,因为我的密钥格式显然是错误的。当我只是在字符串变量中使用.pem密钥格式时,我终于成功了。
y0u0uwnf3#
像这样添加您的公钥。
mzillmmw4#
模块加密未出现在您的
package.json
中。您可以尝试安装它,以及似乎也没有安装的类型。