Node.js的Crypto庫是一個內置模塊,提供了加密和解密的工具。本文將會從多個維度詳細介紹Crypto庫的使用方法。
一、加密算法
Node.js的Crypto庫支持多種常見的加密算法,例如MD5、SHA-1、SHA-256、AES、RSA等。其中,對稱算法和非對稱算法的應用場景不同。
1、對稱加密算法
對稱加密算法指加密和解密使用相同的密鑰。其中,最常見的對稱加密算法有AES、DES和3DES。這種算法的優點是加解密速度快,適合加密大數據。下面是一個AES對稱加密的示例代碼:
const crypto = require('crypto'); const key = 'my-secret-key'; const plaintext = 'hello world'; const cipher = crypto.createCipher('aes-256-cbc', key); let encrypted = cipher.update(plaintext, 'utf8', 'hex'); encrypted += cipher.final('hex'); console.log(encrypted);
參考文獻:https://nodejs.org/api/crypto.html#crypto_class_cipher
2、非對稱加密算法
非對稱加密算法指加密和解密使用不同的密鑰。其中,最常見的非對稱加密算法有RSA。這種算法的優點是加密強度高,安全性好,但是加解密速度較慢。
下面是一個使用RSA非對稱加密的示例代碼:
const crypto = require('crypto'); const { publicKey, privateKey } = crypto.generateKeyPairSync('rsa', { modulusLength: 2048, publicKeyEncoding: { type: 'spki', format: 'pem', }, privateKeyEncoding: { type: 'pkcs8', format: 'pem', }, }); const plaintext = 'hello world'; const encrypted = crypto.publicEncrypt(publicKey, Buffer.from(plaintext)); console.log(encrypted.toString('base64'));
參考文獻:https://nodejs.org/api/crypto.html#crypto_class_cryptokeypair
二、Hash算法
Hash算法又稱散列算法,是一種將任意長度數據映射為固定長度哈希值的算法。常見的Hash算法有MD5、SHA-1、SHA-256等。Node.js的Crypto庫提供了這些算法的實現。
1、MD5算法
MD5算法是最廣泛使用的Hash算法之一,生成128-bit的哈希值。下面是一個MD5哈希的示例代碼:
const crypto = require('crypto'); const hash = crypto.createHash('md5'); hash.update('hello world'); console.log(hash.digest('hex'));
參考文獻:https://nodejs.org/api/crypto.html#crypto_crypto_createhash_algorithm_options
2、SHA-1算法
SHA-1算法生成160-bit的哈希值,相對於MD5更加安全。下面是一個SHA-1哈希的示例代碼:
const crypto = require('crypto'); const hash = crypto.createHash('sha1'); hash.update('hello world'); console.log(hash.digest('hex'));
參考文獻:https://nodejs.org/api/crypto.html#crypto_crypto_createhash_algorithm_options
三、加解密實戰
本節將結合對稱加密和非對稱加密實現一個真實的加解密應用。
1、生成密鑰
本例中,使用RSA非對稱加密算法和AES對稱加密算法。首先,我們需要生成RSA密鑰對,並把公鑰存儲到文件中。
const fs = require('fs'); const crypto = require('crypto'); const { publicKey, privateKey } = crypto.generateKeyPairSync('rsa', { modulusLength: 2048, }); fs.writeFileSync('./public.pem', publicKey); console.log('public key saved');
參考文獻:https://nodejs.org/api/crypto.html#crypto_crypto_generatekeypairsync_type_options
2、數據加密
接下來,我們使用AES對稱加密算法,使用生成的RSA公鑰對AES密鑰進行加密,再用AES密鑰對數據進行加密。
const fs = require('fs'); const crypto = require('crypto'); const publicKey = fs.readFileSync('./public.pem'); function encrypt(message, key) { const iv = Buffer.alloc(16, 0); const cipher = crypto.createCipheriv('aes-256-cbc', key, iv); let encrypted = cipher.update(message, 'utf8', 'hex'); encrypted += cipher.final('hex'); return encrypted; } const plaintext = 'hello world'; const key = crypto.randomBytes(32); const encryptedKey = crypto.publicEncrypt(publicKey, key).toString('hex'); const encryptedData = encrypt(plaintext, key); console.log('key:', key.toString('hex')); console.log('encryptedKey:', encryptedKey); console.log('encryptedData:', encryptedData);
參考文獻:https://nodejs.org/api/crypto.html#crypto_crypto_createcipheriv_algorithm_key_iv_options
3、數據解密
最後,我們用生成的RSA私鑰對AES密鑰進行解密,再用解密後的密鑰對數據進行解密。
const fs = require('fs'); const crypto = require('crypto'); const publicKey = fs.readFileSync('./public.pem'); const privateKey = fs.readFileSync('./private.pem'); function encrypt(message, key) { const iv = Buffer.alloc(16, 0); const cipher = crypto.createCipheriv('aes-256-cbc', key, iv); let encrypted = cipher.update(message, 'utf8', 'hex'); encrypted += cipher.final('hex'); return encrypted; } function decrypt(encrypted, key) { const iv = Buffer.alloc(16, 0); const decipher = crypto.createDecipheriv('aes-256-cbc', key, iv); let decrypted = decipher.update(encrypted, 'hex', 'utf8'); decrypted += decipher.final('utf8'); return decrypted; } const plaintext = 'hello world'; const key = crypto.randomBytes(32); const encryptedKey = crypto.publicEncrypt(publicKey, key).toString('hex'); const encryptedData = encrypt(plaintext, key); const decryptedKey = crypto.privateDecrypt(privateKey, Buffer.from(encryptedKey, 'hex')); const decryptedData = decrypt(encryptedData, decryptedKey); console.log('key:', decryptedKey.toString('hex')); console.log('data:', decryptedData);
參考文獻:https://nodejs.org/api/crypto.html#crypto_crypto_privatedecrypt_privatekey_buffer
四、總結
本文結合對稱加密和非對稱加密,以及Hash算法,詳細介紹了Node.js的Crypto庫,包括加密、解密、Hash等方面的知識點。希望讀者能夠通過本文對Node.js的Crypto庫有更深入的了解,並在實際應用中靈活運用。
原創文章,作者:AAWCR,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/349359.html