Home  >  Article  >  Web Front-end  >  A brief analysis of Node.js data encryption transmission

A brief analysis of Node.js data encryption transmission

高洛峰
高洛峰Original
2016-12-06 15:53:421388browse

Foreword

Data encrypted transmission, we often come across several methods. One is ciphertext transmission, and the other is plaintext transmission. Ciphertext transmission is to use a key to encrypt the data and use the public key to decrypt the data. The transmission channel can be https. It can be http. The premise of clear text transmission is to establish a secure transmission channel. Here, a certificate is used to protect the security of the channel, and then the data is transmitted in plain text.

The more professional ones can share it later, but here I will introduce the plain text transmission. If nodejs is used to establish a secure channel

Use two libraries, namely urllib and request. The certificate here only introduces the use of pfx files

The method of urllib library

const urllibRequest = (url, method, data, pfx, pass) => {
 return new Promise(function(resolve, reject) {
  let options = {
   data: data,
   method: method,
   pfx: pfx,
   passphrase: pass,
   rejectUnauthorized: false
  }
  urllib.request(url, options, function(err, data, res) {
   if (err) {
    return reject(err);
   }
   return resolve(data.toString());
  });
 });
}

request library method

const httpRequest = (url, method, data, pfx, pass) => {
 return new Promise((resolve, reject) => {
  let options = {
   url: url,
   method: method,
   form: data,
   headers: {
    'Content-type': 'application/x-www-form-urlencoded'
   },
   agentOptions: {
    pfx: pfx,
    passphrase: pass,
    rejectUnauthorized: false
   }
  };
  request(options, function(err, httpResponse, data) {
   if (err) {
    return reject(err);
   }
   return resolve(data);
  })
 });
}


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn