您好,登錄后才能下訂單哦!
今天小編給大家分享一下Node怎么實現前后端數據傳輸加密解密的相關知識點,內容詳細,邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。
在前后端通信過程中,一些敏感信息,特別是用戶的賬號密碼,需要加密進行傳輸,如何選擇加密方式也是一門學問,這里倒也不過多偏題了。 通常來說B/S架構中用的比較多的傳輸數據加密是RSA加密,核心思想就是公鑰加密,私鑰解密。 公鑰,可以理解為可以公開的鑰匙,服務器將公鑰發給客戶端,客戶端用公鑰對數據進行加密傳輸,服務器收到客戶端傳來的密文,再用自己的配對的私鑰進行解密,即可獲得原始的數據。
大致流程如下圖所示:
Nodejs中有一個核心模塊——crypto
提供各種加密、解密相關API。以下示例是基于Node@12.15.0
版本進行編寫。
const crypto = require('crypto'); /** * 生成RSA公私鑰對 * @return {*} publicKey: 公鑰;privateKey: 私鑰 */ function genRSAKeyPaire() { const { publicKey, privateKey } = crypto.generateKeyPairSync('rsa', { modulusLength: 4096, publicKeyEncoding: { type: 'pkcs1', format: 'pem', }, privateKeyEncoding: { type: 'pkcs1', format: 'pem', }, }); return { publicKey, privateKey }; } // 打印生成的公私鑰對 console.log(genRSAKeyPaire());
輸出示例如下:
(示例公私鑰已被刪除了一部分,請勿用于實驗)
{ publicKey: '-----BEGIN RSA PUBLIC KEY-----\n' + 'MIICCgKCAgEAsitohTu9Jf2h+NPV4tNfFhPrlbStzXNM8wSEcskwtpwi6aZfgQC7\n' + '/A7M1hN8Zk8WgiZjy05AHinWPvXo70OWj8TminIAjB2wh0nDqm+IQqN7r20uzeJm\n' + 'GBf1KusGemChEiFwiad1h/OB9z9LC8zMYR/G+XAbFfcv8MxAMI9mgmS8t5+xeYm6\n' + 'EMiCQkQjfqpErhW3oESj8hrdJdOZbiK3l0TgYLyjZRQu6pHzFkmd9We3BY1qcXo1\n' + '2BtNKvqoH9QDJItsb3S9v2mOGl1rbItKlDrqY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NZe83qdf8/PH\n' + '8i983zpo3bASAE9wrBD1ApD/Bu2Ht+PwQcoEAEHp5/ue0IFXB7uw9UGqW+UVdwxn\n' + '2GCvk25NZsm01SPQK5ZO9wMNaLh4LTl9C13s7qMhJwXcXNjkjX79jNt/RD7gFZIK\n' + 'oXfgWn83QcZboS64Msdk1AIYMJzkF3ge3zZwaM8gEoYTgjuFQm4oB1/CFk7pyoRb\n' + 'rXMwv9nbiTMvFtfc52czzm7gUxkiB0A5AoIBAQCffC5rDhDGPiwJOft0PYNK/Ctk\n' + '3QZa2+t1ni0HYQhPok5OSgAOZwkZItGDGXdrvXe4+q/ttLLu6KhVaVRVoe+VzMpl\n' + 'WKp0RMBt999JS2XAipbguTQXrfsev0RNam0AFREUZdPNvrwLprQAwTl0iC2t4H6b\n' + 'RybgQU6RpORFDvpwmkBjJ9Q2p540LmN0NVHq6Axv+g4TI2XdXlw8T7VQbJGKvfuJ\n' + 'g7j4+f7J+KpN5rHudiEPIVOK8V7Ap8dxP+lwEhZjK1MvCJE+SXWTkrRcY/TX\n' + '-----END RSA PRIVATE KEY-----\n' }
2、公鑰加密
在有了公私鑰對的基礎上,使用公鑰進行加密,示例代碼如下:
const crypto = require('crypto'); /** * 生成公私鑰對 * @return {*} publicKey: 公鑰;privateKey: 私鑰 */ function genRSAKeyPaire() { const { publicKey, privateKey } = crypto.generateKeyPairSync('rsa', { modulusLength: 4096, publicKeyEncoding: { type: 'pkcs1', format: 'pem', }, privateKeyEncoding: { type: 'pkcs1', format: 'pem', }, }); return { publicKey, privateKey }; } const { publicKey } = genRSAKeyPaire(); /** * 使用公鑰進行加密 * @param {String} data * @param {String} publicKey * @return {String} 加密后的密文 */ function publicKeyEncrypt(data, publicKey) { return crypto.publicEncrypt(publicKey, Buffer.from(data)).toString('base64') } const entry = { name: 'zhangsan', password: '123456' }; // 將數據轉為字符串格式,并進行公鑰加密后打印 console.log(publicKeyEncrypt(JSON.stringify(entry), publicKey));
輸出內容為如下格式:
(數據已做刪改)
Ri0p8QFmnYe8Xo36DextK242o9pcdL0QFDo6gUxhzjwQD30UFlqJL57na445BebSp1VT1z94emJgrME7xTDzV1tshtmVNtarqCUCzZMF4uYAtZCQLJhCX3708g7lOFksiUvi6MlXCVVOIu2VyFsIS/6DeEWYNirPK6zEBw1e2V2jWoL+63+iGNyhtKFJI1ECGyMmXUWCMicUmgE/JiHJD7YXPKB9+WaB7Wglj5udBdd4fALUp7qIo8TWJZJkLUg5yMbe7kemNWk050Xi1KiEt3s8IAqoRB1qGghTmE/TW+M/jIblSSy3Urle1AYsOFUzh9wV/H+uD+UNdaCvlvfmdV8hTIjjLNy9r/GbuaI5N0TkaX/dk47iUuorZabPoINEnM8lYxcKPvgVJufMfSX5wLxgx60nt4cpz3T2IutO97sdocVbhsiSlpFLpIk88xd4=
3、私鑰解密
有了公鑰加密后的密文,再用私鑰進行解密,代碼如下:
const crypto = require('crypto'); /** * 生成公私鑰對 * @return {*} publicKey: 公鑰;privateKey: 私鑰 */ function genRSAKeyPaire() { const { publicKey, privateKey } = crypto.generateKeyPairSync('rsa', { modulusLength: 4096, publicKeyEncoding: { type: 'pkcs1', format: 'pem', }, privateKeyEncoding: { type: 'pkcs1', format: 'pem', }, }); return { publicKey, privateKey }; } const { publicKey, privateKey } = genRSAKeyPaire(); /** * 使用公鑰進行加密 * @param {String} data * @param {String} publicKey * @return {String} 加密后的密文 */ function publicKeyEncrypt(data, publicKey) { return crypto.publicEncrypt(publicKey, Buffer.from(data)).toString('base64') } const entry = { name: 'zhangsan', password: '123456' }; const encryptedData = publicKeyEncrypt(JSON.stringify(entry), publicKey); /** * 使用私鑰進行解密 * @param {String} encryptedData * @param {String} privateKey * @return {String} 解密后的明文 */ function privateKeyDecrypt(encryptedData, privateKey) { return crypto.privateDecrypt(privateKey, Buffer.from(encryptedData, 'base64')).toString(); } const originData = privateKeyDecrypt(encryptedData, privateKey); // 打印用私鑰解密后的數據 console.log(originData);
輸出結果:
{"name":"zhangsan","password":"123456"}
和我們上面定義的entry
數據內容一致,說明解密成功,但是解密后是一個字符串,如果原始數據是一個對象的話,別忘記了將其反序列化為對象。
以上就是“Node怎么實現前后端數據傳輸加密解密”這篇文章的所有內容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學習更多的知識,請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。