我正在使用 node-rsa 模块来解密密钥。
我使用下面的命令
从p12文件创建了一个私钥openssl pkcs12 -in xxx.p12 -nocerts -out privateKeyPkcs12.pem
它已经提供了rsa私钥,所以我试图通过使用这个rsa私钥来创建密钥
var NodeRSA = require('node-rsa');
var pemStr = '-----BEGIN RSA PRIVATE KEY-----\n' +
'ud5iSK4hAZo9DRi3/Xj7tYCb+AOv1rCAxWbM9KW959DaAYzO9eHRcmbO1Gusa93y\n' +
'2usgD3YYlfHAEvtMhW3KxDWXv0o/YE71pjltpn46qP0n2s78RwdbHONlXB0wk54Z\n' +
'1MqqZ8wxo/6mAu+ZqdCMe5rgOdMkXcHbFP+GFZQNSIoCrEQP098jhot9cYjyYNuW\n' +
'a7Y59gUEZE5s0jdrga/DcP+BUOfIoTZCf7gPqLQawjPXSp7Ddr5AxyVO8Phyp5yN\n' +
'hcdQV1Fvms5K7rsLvp0KFRz1iclrvBB5rYgawBSPGmrtP+AvRvjuDgfQiAPEA4SP\n' +
'cNx0oQ94q3jEPlhve86zhb1xDV4xnH8TKPYgpoafo6VC+w46FmoUh5/ZuKCsjBkV\n' +
'5gt3WyjsR5Jjgkv2pyH2rDfsB40DLvOg3GkgFo2gQ/UeXM3wQCjXRcG/rrlRYlj1\n' +
'9h+EjYaZE6vv+FpMLsYJc54bli/VjqyucEvvd0RuV15AEMI8cqwxlE3+ED2pv1Mk\n' +
'kLfONCAwylrvXBkapcp9P2qtRVLbyAzMXDJGDcSdxQFbDmmn8L/gOQs2+vgyvGet\n' +
'HxPC6gt1i6WEZHeQkSD68cs54JYmGZvu2bOTSCAMVOCKLv/Ed1LvSeowYZoHBXaK\n' +
'cRVqegBTWts5+DHWTwb8Nsrgz+5i6ZAFaJqGlrYjUApApDgAxTP5XQ47CxvtSg/T\n' +
'H25IJkY3Oz+7K18V1eslEk0bA44vYqeXia1ZLvLXRbtdSMlm0Fp7zTs7USVTJEgx\n' +
'RAGT8LnncwtZOFpEAYyIakI5lhe3aTvsDkW3pTXxDREKA2pmf0N7YAOw84tnoX8o\n' +
'w6hExfFrp2w82wRKft/ymoU5KUk6JSiHnuTu3JQPk/b9Vfb5zwRf5qXlLBERtO9U\n' +
'gctP2c3wvmF1pko8TfghKiTrAKi0Dft3Jrh50VDkmiFJCkupTDQ0EYyA4OV8PWui\n' +
'QhLFSGcBSGaik+7CMrh4TAiM59Hmh0Q3gKjmbvdYlx8gfZQzKWo71MkhDnbYTVnP\n' +
'6LU47Zo7ESFAPLFxBj0nTjJ8A7sFpQys/GK/fY29MnnR3LDT78zParNBKo56nCR4\n' +
'Sk8kL16Wx9hnTfEskHUKTmH9mph8hcv1S6/CQjQYVtLW13nlmGeAiJ/mf0sSzRhH\n' +
'mW5aCq3LAULIEpV0zRvpghZq4lc7RBbOudAgONrURWOgTgUxZ+N5PRMaesaeihpO\n' +
'gPY0LWyzb7CicK9y24oHO2uY87hinprusHaq/Rp4mVsYKRG0sk0/26bkbwn3sFMB\n' +
'LSSyGj32PD272bva1NE/4iWO+zppLKnroqUwrivmR3SPndvCX22x1qUiSY/R5UHp\n' +
'mcDWo9g9+nfPnXKmK2rwq82B4E+tqQZ68Iha95SaPYMt4pv+QPtaePxwSSpNPT9F\n' +
'WPwpEfKXa8wN9OAaGJBB9AM/Ry2aOTrekjNUxMzSqJdoBxsWxI0HrgVVaRQSk3VV\n' +
'juYMJdEjoSLY43jgZLEypA8ddmC55n/3Nwwyd6lkhvvkY+tvv6KL/zFnRQ81L0dP\n' +
'WTwvd33YSA6cCo4OEFixkls0lFq/MvesdXWZEyjNYzTlPYjqL9jIeik7VxQsw6sh\n' +
'-----END RSA PRIVATE KEY-----\n';
var key = new NodeRSA(pemStr);
console.log(key);
但我收到了这个错误
throw newInvalidAsn1Error('encoding too long');
^
InvalidAsn1Error: encoding too long
at module.exports.newInvalidAsn1Error (c:\Users\prabu\Desktop\project\node_modules\node-rsa\node_modules\asn1\lib\ber\errors.js:7:13)
at Reader.readLength (c:\Users\prabu\Desktop\project\node_modules\node-rsa\node_modules\asn1\lib\ber\reader.js:94:13)
at Reader.readSequence (c:\Users\prabu\Desktop\project\node_modules\node-rsa\node_modules\asn1\lib\ber\reader.js:127:16)
at NodeRSA.module.exports.NodeRSA.$loadFromPrivatePEM (c:\Users\prabu\Desktop\project\node_modules\node-rsa\src\NodeRSA.js:86:16)
at NodeRSA.module.exports.NodeRSA.loadFromPEM (c:\Users\prabu\Desktop\project\node_modules\node-rsa\src\NodeRSA.js:65:18)
at new NodeRSA (c:\Users\prabu\Desktop\project\node_modules\node-rsa\src\NodeRSA.js:35:18)
at Object.<anonymous> (c:\Users\prabu\Desktop\project\server.js:30:11)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
如何创建密钥?
答案 0 :(得分:1)
删除键字符串末尾的换行符(\ n)
'-----END RSA PRIVATE KEY-----\n';
到
'-----END RSA PRIVATE KEY-----';
答案 1 :(得分:1)
您使用pass
了吗?如果是,请尝试检查name
文件中是否有空行。
只保留字符串,不再有空行
答案 2 :(得分:0)
尝试指定密钥的格式标准。
通过添加其他参数来更改此行:
var key = new NodeRSA(pemStr);
收件人:
var key = new NodeRSA(pemStr, 'pkcs12');