简体   繁体   English

安全发送数据 NODEjs - REST API

[英]Send data securely NODEjs - REST API

I would like to send encrypted / secure data that I send to my REST API.我想发送我发送到我的 REST API 的加密/安全数据。 However, I encounter difficulties here and would be very happy about external help.但是,我在这里遇到了困难,很高兴得到外部帮助。 Please note that the code is simplified for better illustration!请注意,为了更好地说明,代码已简化!

Here is the basic structure of the REST API in order to be able to reveal errors / problems of understanding on my part:这是 REST API 的基本结构,以便能够揭示我的错误/理解问题:

Sending the data:发送数据:

First, I encrypt the relevant data:首先,我对相关数据进行加密:

 const crypto = require('crypto') const algorithm = 'aes-256-ctr' const secretKey = 'somesecret' const iv = crypto.randomBytes(16) const encrypt = (t) => { const cipher = crypto.createCipheriv(algorithm, secretKey, iv) const encrypted = Buffer.concat([cipher.update(t), cipher.final()]) return { iv: iv.toString('hex'), content: encrypted.toString('hex') } } const regdata = crypto.encrypt(password) //some stuff --> sending data

I then send the data, including the iv and the data to be encrypted, to my API.然后我将数据(包括 iv 和要加密的数据)发送到我的 API。

 function apicall(data, Method) { data = Object.assign(data, { apisecret: process.env.REACT_APP_APISECRET, cryptokey: process.env.REACT_APP_CRYPTOKEY }) const mydata = fetch(`https://somepublic.url:4400/defined/query/${JSON.stringify(data)}`, { method: Method }) const jdata = JSON.parse(tdata) //some stuff return jdata }

My API looks something like this:我的 API 看起来像这样:

 //require some stuff app.use(express.json()) app.use(function(req, res, next) { res.setHeader('Access-Control-Allow-Origin', '*'); res.setHeader('Access-Control-Allow-Methods', 'GET,POST,PUT,DELETE,OPTIONS'); res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Access-Control-Allow-Headers'); next(); }); // ** Select SQL Data app.get('/defined/query/:data', (req, res) => { const data = JSON.parse(req.params.data) if ((data.apisecret != process.env.NODE_APP_APISECRET) || (data.cryptokey != process.env.NODE_APP_CRYPTOKEY)) { throw "Authentication Required" } //do some stuff with encrypted Data .then(response => { res.status(200).send(response); }) .catch(error => { res.status(500).send(error); }) })

It all works, I just don't quite understand what encrypting the data does for me if it can be read out in the URL.一切正常,我只是不太明白加密数据对我有什么作用,如果它可以在 URL 中读出的话。 If someone uses my APP and reads the full URL with Whireshark & Co., they can如果有人使用我的 APP 并通过 Whireshark & Co. 阅读完整的 URL,他们可以

  1. read the "process.env.NODE_APP_APISECRET" named by me and thus has access to my API阅读我命名的“process.env.NODE_APP_APISECRET”,从而可以访问我的 API
  2. Although this does not have my "secret key", it can read out the "iv", which would also like to be prevented.虽然这里面没有我的“秘钥”,但它可以读出“iv”,这也是想防止的。

However, I have to send the "iv" in the URL, since the data is only decrypted afterwards.但是,我必须在 URL 中发送“iv”,因为数据只会在之后解密。

Did I make a mistake in my setup, or generally have a problem understanding the API interface?我是否在设置中犯了错误,或者在理解 API 接口时通常有问题?

You need to consider who you are trying to stop from intercepting the data.您需要考虑您试图阻止谁拦截数据。

If the goal is to stop third parties intercepting the data between the client and the server then use HTTPS , only use HTTPS, do not roll your own encryption on top of it.如果目标是阻止第三方拦截客户端和服务器之间的数据,则使用 HTTPS ,仅使用 HTTPS,不要在其上滚动您自己的加密。

If your goal is to let the client store data on the server without people who have access to the server being able to decrypt it then encrypt it on the client, and do not send the keys to the server.如果您的目标是让客户端将数据存储在服务器上,而有权访问服务器的人无法对其进行解密,那么请在客户端上对其进行加密,并且不要将密钥发送到服务器。

If your goal is to let multiple clients exchange data without the server being able to decrypt it, then generate keys on each client, share public keys between clients and keep private keys private.如果您的目标是让多个客户端在服务器无法解密的情况下交换数据,则在每个客户端上生成密钥,在客户端之间共享公钥并保持私钥私有。 Then each client should encrypt the data with the recipient's public key (which can only be decrypted by the recipient's private key).然后每个客户端都应该用接收者的公钥加密数据(只能用接收者的私钥解密)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM