错误:解密期间出错(可能是错误的密钥)。原始错误:错误:这不是私钥
这是我的nodejs代码。我正在使用node-rsa。
const keyData = fs
.readFileSync("./docs/PublicKey/XXX_sandbox.pem")
.toString();
const NodeRSA = require("node-rsa");
var key = new NodeRSA();
key.importKey(keyData, "pkcs8-public");
const encrypted = key.encrypt("Test@1234", "base64");
console.log("encrypted: ", encrypted);
这是我的pem文件:
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArxd93uLDs8HTPqcSPpxZ
rf0Dc29r3iPp0a8filjAyeX4RAH6lWm9qFt26CcE8ESYtmo1sVtswvs7VH4Bjg/F
DlRpd+MnAlXuxChij8/vjyAwE71ucMrmZhxM8rOSfPML8fniZ8trr3I4R2o4xWh6
no/xTUtZ02/yUEXbphw3DEuefzHEQnEF+quGji9pvGnPO6Krmnri9H4WPY0ysPQQ
Qd82bUZCk9XdhSZcW/am8wBulYokITRMVHlbRXqu1pOFmQMO5oSpyZU3pXbsx+Ox
IOc4EDX0WMa9aH4+snt18WAXVGwF2B4fmBk7AtmkFzrTmbpmyVqA3KO2IjzMZPw0
hQIDAQAB
-----END PUBLIC KEY-----
我通过将
encrytionScheme
设置为“ pkcs1”解决了我的问题。查找下面的代码。使用node-rsa中的key.setOptions
。
var NodeRSA = require("node-rsa");
var key = new NodeRSA();
const keyData =
"-----BEGIN PUBLIC KEY-----\n" +
"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArxd93uLDs8HTPqcSPpxZ\n" +
"rf0Dc29r3iPp0a8filjAyeX4RAH6lWm9qFt24CcE8ESYtmo1sVtswvs7VH4Bjg/F\n" +
"DlRpd+MnAlXuxChij8/vjyAwE71ucMrmZhxM8rOSfPML8fniZ8trr3I4R2o4xWh6\n" +
"no/xTUtZ02/yUEXbphw3DEu9fzHEQnEF+quGji9pvGnPO6Krmnri9H4WPY0ysPQQ\n" +
"Qd82bUZCk9XdhSZcW/am8wBulYokITRMVHlbRXqu1pOFmQMO5oSpyZU3pXbsx+Ox\n" +
"IOc4EDX0WMa9aH4+snt18WAXVGwF2B4fmBk7AtmkFzrTmbpmyVqA3KO2IjzMZPw0\n" +
"hQIDAQAB\n" +
"-----END PUBLIC KEY-----";
//setOptions ecryptionScheme is default to pkcs1_oaep by setting this to pkcs1. I could able to solve my problem
key.setOptions({
encryptionScheme: "pkcs1"
});
key.importKey(keyData, "pkcs8-public");
const encrypted = key.encrypt("Test@1234", "base64");
res.send(encrypted);