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/n/349359.html
微信扫一扫
支付宝扫一扫