Node.js 資料加密傳輸淺析_node.js

來源:互聯網
上載者:User

前言

資料加密傳輸,大家經常接觸幾個方式一個是密文傳輸,一個明文傳輸密文傳輸,就是用金鑰組資料加密,使用公開金鑰對資料解密,傳輸的通道可以是https的也可以是http的。明文傳輸,前提是建立一個安全的傳輸通道,這裡使用認證對通道的安全做了防護,然後傳輸資料,使用的是明文。

比較專業的 可以後面慢慢分享,不過這裡我就介紹下明文傳輸,如果是用nodejs建立安全通道

使用兩個庫,分別是urllib和request,這裡的認證只介紹使用pfx檔案

urllib庫的方式

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庫的方法

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);  }) });}

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的協助,如果有疑問大家可以留言交流。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.