Node.js+;通过express对数据进行加密签名和验证

Node.js+;通过express对数据进行加密签名和验证,node.js,express,Node.js,Express,我希望能够确保公钥和私钥有效。我将把公钥存储在服务器上,而不是用户上传的每个请求。但是,它将不被允许发送 js(服务器) const express=require('express')) 常量加密=需要(“加密”) const fetch=require(“节点获取”) const app=express() 常数端口=3002 app.get('/',异步(req,res)=>{ var res2=等待提取('http://localhost:3001/data') var-verifiab

我希望能够确保公钥和私钥有效。我将把公钥存储在服务器上,而不是用户上传的每个请求。但是,它将不被允许发送

js(服务器)

const express=require('express'))
常量加密=需要(“加密”)
const fetch=require(“节点获取”)
const app=express()
常数端口=3002
app.get('/',异步(req,res)=>{
var res2=等待提取('http://localhost:3001/data')
var-verifiableData=wait res2.text()
var res2=等待提取('http://localhost:3001/key')
var publicKey=wait res2.text()
var res2=等待提取('http://localhost:3001/sig')
var signature=wait res2.text()
const isverify=crypto.verify(
“sha256”,
缓冲区。从(可验证数据),
{
密钥:公钥,
填充:crypto.constants.RSA_PKCS1_PSS_padding,
},
签名
)
res.send(已验证)
})
应用程序侦听(端口,()=>{
console.log(`listing athttp://localhost:${port}`)
})
js(客户端)

const express=require('express'))
常量加密=需要(“加密”)
const app=express()
常数端口=3001
const{publicKey,privateKey}=crypto.generateKeyPairSync(“rsa”{
//RSA密钥的标准安全默认长度为2048位
模长:2048,
})
const verifiableData=“这需要验证”
常量签名=加密签名(“sha256”,缓冲区来自(可验证数据){
钥匙:私钥,
填充:crypto.constants.RSA_PKCS1_PSS_padding,
})
app.get('/data',(req,res)=>{
res.send(可验证数据)
})
app.get('/sig',(req,res)=>{
res.send(signature.toString(“base64”))
})
app.get('/key',(req,res)=>{
res.send(公钥)
})
应用程序侦听(端口,()=>{
console.log(`listing athttp://localhost:${port}`)
})
我知道我应该从服务器而不是客户端请求信息,但我只是想让它正常工作

我在带有签名的server.js代码中发现了这个错误

(node:19312) UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_ARG_TYPE]: The "signature" argument must 
be an instance of Buffer, TypedArray, or DataView. Received type string ('OFEush86vZIuFnTLFBiFt4Be...)
    at Object.verifyOneShot [as verify] (internal/crypto/sig.js:212:11)
    at C:\Users\user\Documents\coding\nodestuff\stesting\server\server.js:20:31
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:19312) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:19312) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise 
rejections that are not handled will terminate the Node.js process with a non-zero exit code.
将index.js更新为

app.get('/sig',(req,res)=>{
res.send(signature.toString(“base64”))
})
并将server.js

const isverify=crypto.verify(
“sha256”,
缓冲区。从(可验证数据),
{
密钥:公钥,
填充:crypto.constants.RSA_PKCS1_PSS_padding,
},
Buffer.from(签名'base64')
)
然后得到一个错误:

(节点:5580)未处理的Promisejection警告:错误:错误:0909006C:PEM例程:获取名称:无起始行
在Object.verifyOneShot[as verify](内部/crypto/sig.js:219:10)
在C:\Users\user\Documents\coding\nodestuff\stesting\server\server.js:20:31
在处理和拒绝时(内部/process/task_queues.js:97:5)
(节点:5580)未处理的PromisejectionWarning:未处理的承诺拒绝。此错误源于在没有catch块的异步函数中抛出,或者拒绝未使用.catch()处理的承诺。要在未处理的承诺拒绝时终止节点进程,请使用CLI标志“---unhandled rejections=strict”(请参阅https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (拒绝id:1)
(节点:5580)[DEP0018]弃用警告:未处理的承诺拒绝已弃用。将来,未处理的承诺拒绝将使用非零退出代码终止Node.js进程。

它在单个文件中工作得非常好,但在通过express API发送时就不起作用了。如何修复错误?

需要做的是公钥无效。这是因为它是一个
PublicKeyObject
,这意味着它不会与express一起发送,而将值保留为
{}

我不得不这么做

const{publicKey,privateKey}=crypto.generateKeyPairSync(“rsa”{
//RSA密钥的标准安全默认长度为2048位
公钥编码:{
类型:“spki”,
格式:“pem”
},
模长:2048,
})
以及在签名部分使用此选项

Buffer.from(签名'base64')