encrypt
了解加密算法是一个很好的决定!加密算法主要分为对称加密算法和非对称加密算法两种类型。
- 对称加密算法:对称加密算法使用相同的密钥来加密和解密数据。常见的对称加密算法包括 AES、DES、3DES 等。对称加密算法适合于加密大量数据,加解密速度快。下面是一个使用 Node.js 实现 AES 加密和解密的示例代码:
const crypto = require('crypto');
// 加密函数
function encryptAES(text, key) {
const iv = crypto.randomBytes(16); // 生成随机的初始化向量
const cipher = crypto.createCipheriv('aes-256-cbc', Buffer.from(key), iv);
let encrypted = cipher.update(text, 'utf8', 'hex');
encrypted += cipher.final('hex');
return {
iv: iv.toString('hex'),
encryptedData: encrypted,
};
}
// 解密函数
function decryptAES(encryptedData, key, iv) {
const decipher = crypto.createDecipheriv('aes-256-cbc', Buffer.from(key), Buffer.from(iv, 'hex'));
let decrypted = decipher.update(encryptedData, 'hex', 'utf8');
decrypted += decipher.final('utf8');
return decrypted;
}
// 测试
const key = crypto.randomBytes(32); // 生成随机的密钥
const originalText = 'Hello, world!';
const encryptedData = encryptAES(originalText, key.toString('hex'));
const decryptedText = decryptAES(encryptedData.encryptedData, key.toString('hex'), encryptedData.iv);
console.log('Original Text:', originalText);
console.log('Encrypted Data:', encryptedData);
console.log('Decrypted Text:', decryptedText);
- 非对称加密算法:非对称加密算法使用一对密钥(公钥和私钥)来加密和解密数据。常见的非对称加密算法包括 RSA、DSA、ECC 等。非对称加密算法适合于安全通信,能够解决密钥分发和身份认证等问题。下面是一个使用 Node.js 实现 RSA 加密和解密的示例代码:
const crypto = require('crypto');
// 生成密钥对
const { publicKey, privateKey } = crypto.generateKeyPairSync('rsa', {
modulusLength: 2048,
publicKeyEncoding: { type: 'pkcs1', format: 'pem' },
privateKeyEncoding: { type: 'pkcs1', format: 'pem' },
});
// 加密函数
function encryptRSA(text, publicKey) {
return crypto.publicEncrypt(publicKey, Buffer.from(text, 'utf8')).toString('base64');
}
// 解密函数
function decryptRSA(encrypted, privateKey) {
return crypto.privateDecrypt(privateKey, Buffer.from(encrypted, 'base64')).toString('utf8');
}
// 测试
const originalText = 'Hello, world!';
const encryptedText = encryptRSA(originalText, publicKey);
const decryptedText = decryptRSA(encryptedText, privateKey);
console.log('Original Text:', originalText);
console.log('Encrypted Text:', encryptedText);
console.log('Decrypted Text:', decryptedText);
以上是使用 Node.js 实现对称加密算法(AES)和非对称加密算法(RSA)的示例代码。希望这些示例能够帮助你开始系统性学习加密算法!
哈希算法
是一种将任意长度的数据输入转换为固定长度的输出的算法。哈希算法主要用于数据完整性校验、数字签名和密码存储等领域。以下是一些常见的哈希算法:
MD5(Message Digest Algorithm 5)
- 输出长度:128位(16字节)
- 用途:数据完整性校验
- 缺点:由于其容易碰撞(产生相同哈希值的不同输入),不再被认为是安全的哈希算法
SHA-1(Secure Hash Algorithm 1)
- 输出长度:160位(20字节)
- 用途:早期用于数字签名、数据完整性校验
- 缺点:已被证明存在碰撞攻击,不再推荐用于安全敏感的应用
SHA-2(Secure Hash Algorithm 2)
- 输出长度:SHA-224、SHA-256、SHA-384、SHA-512(分别为224位、256位、384位和512位)
- 用途:广泛用于数字签名、证书、数据完整性校验等
- 安全性:目前被认为是安全的
SHA-3(Secure Hash Algorithm 3)
- 输出长度:SHA3-224、SHA3-256、SHA3-384、SHA3-512
- 用途:数据完整性校验、数字签名等
- 安全性:基于新的Keccak算法,具有很高的安全性
RIPEMD-160
- 输出长度:160位(20字节)
- 用途:类似SHA-1,但更少使用
- 安全性:目前还没有被广泛攻击,但SHA-2和SHA-3更为流行
bcrypt
- 基于:Blowfish加密算法
- 用途:密码哈希,具有内置的盐和工作因子调整机制
- 安全性:设计用于密码哈希,抵抗暴力破解和时间攻击
scrypt
- 用途:密码哈希,专为高强度计算而设计,消耗大量内存和CPU资源
- 安全性:适合于密码存储,能抵抗硬件加速攻击
Argon2
- 用途:密码哈希,赢得2015年密码哈希竞赛
- 安全性:当前被认为是最安全的密码哈希算法,具有多种配置选项(Argon2d、Argon2i、Argon2id)
下面是一些使用 Node.js crypto
模块实现常见哈希算法的示例代码:
const crypto = require('crypto');
// MD5
function hashMD5(data) {
return crypto.createHash('md5').update(data).digest('hex');
}
// SHA-1
function hashSHA1(data) {
return crypto.createHash('sha1').update(data).digest('hex');
}
// SHA-256
function hashSHA256(data) {
return crypto.createHash('sha256').update(data).digest('hex');
}
// SHA-512
function hashSHA512(data) {
return crypto.createHash('sha512').update(data).digest('hex');
}
// bcrypt (使用bcrypt包)
const bcrypt = require('bcrypt');
async function hashBcrypt(data) {
const salt = await bcrypt.genSalt(10);
return await bcrypt.hash(data, salt);
}
// 示例
const data = 'Hello, world!';
console.log('MD5:', hashMD5(data));
console.log('SHA-1:', hashSHA1(data));
console.log('SHA-256:', hashSHA256(data));
console.log('SHA-512:', hashSHA512(data));
// bcrypt 示例
hashBcrypt(data).then(hash => {
console.log('bcrypt:', hash);
});
这些示例展示了如何在 Node.js 中使用各种哈希算法。对于密码哈希,建议使用更安全的算法如 bcrypt、scrypt 或 Argon2。