【发布时间】:2021-04-06 23:13:07
【问题描述】:
我使用node-rsa包加密数据如下
const crypto = require('crypto')
const NodeRSA = require('node-rsa')
const path = require("path")
const fs = require("fs")
const absolutePath = path.resolve('./public_key.pem')
const publicKey = fs.readFileSync(absolutePath, "utf8")
var key = new NodeRSA();
key.importKey(publicKey, 'pkcs8-public');
key.setOptions({environment: 'node', encryptionScheme: 'pkcs1'});
const result = key.encrypt('{"message": "hello"}', 'base64')
但是现在当我尝试解密结果如下:
key.decryptPublic(result, 'utf-8')
我收到以下错误消息:
错误:解密过程中出错(可能是不正确的密钥)。原来的 错误:错误:错误:0407008A:rsa 例程:RSA_padding_check_PKCS1_type_1:无效填充 在 NodeRSA.module.exports.NodeRSA.$$decryptKey (/home/runner/node_modules/node-rsa/src/NodeRSA.js:301:19) 在 NodeRSA.module.exports.NodeRSA.decryptPublic (/home/runner/node_modules/node-rsa/src/NodeRSA.js:267:21) 在 evalmachine.:16:17 在 Script.runInContext (vm.js:133:20) 在 Object.runInContext (vm.js:311:6) 在评估 (/run_dir/repl.js:133:14) 在读取流。 (/run_dir/repl.js:116:5) 在 ReadStream.emit (events.js:198:13) 在 addChunk (_stream_readable.js:288:12) 在 readableAddChunk (_stream_readable.js:269:11)
我该如何解决这个问题?
谢谢。
【问题讨论】:
-
我曾经写过一篇关于这个的文章(我已经忘记了我学到的一切),它可能会有所帮助:https://dev.to/subterrane/i-learned-enough-web-crypto-to-be-dangerous-5b5j
标签: javascript node.js cryptography node-rsa