nodejs的crypto加密,设置认证数据标签,使用认证标签加密和解密,防止数据被串改

//直接上代码上说明

const crypto = require('crypto');

class Cipher {
//设置私有成员,#key随机的加密密码,#iv加密时所用的初始向量
    #key = crypto.randomBytes(32); // 256-bit key
    #iv = crypto.randomBytes(12); // 96-bit IV

    constructor(aad) {
    //设置附加的认证数据,任何字符
         this.aad = Buffer.from(aad); // 附加认证数据
    }
//加密
    encrypt(text) {
    //创建加密实例,经过身份验证的加密模式,就是认证数据(当前支持 GCM、CCM、OCB 和 chacha20-poly1305)
        const cipher = crypto.createCipheriv('aes-256-gcm', this.#key, this.#iv);
        cipher.setAAD(this.aad);

        let encrypted = cipher.update(text, 'utf8', 'hex');
        encrypted += cipher.final('hex');

        const authTag = cipher.getAuthTag(); // 获取认证标签
        return { encrypted, authTag }; // 返回密文和认证标签
    }
//解密
    decipher(encrypted, authTag) {
        const decipher = crypto.createDecipheriv('aes-256-gcm', this.#key, this.#iv);
        //解密也需要认证标签
        decipher.setAAD(this.aad);
        decipher.setAuthTag(authTag); // 设置认证标签

        try {
            let decrypted = decipher.update(encrypted, 'hex', 'utf8');
            decrypted += decipher.final('utf8');
            return decrypted;
        } catch (err) {
            console.error('Decryption failed:', err.message);
            return null;
        }
    }
}

// 使用示例
const c = new Cipher('metadata');
const { encrypted, authTag } = c.encrypt('hello'); // 加密
console.log('Encrypted:', encrypted);
console.log('Auth Tag:', authTag.toString('hex'));

const decrypted = c.decipher(encrypted, authTag); // 解密
console.log('Decrypted:', decrypted);
在Node.js中,Crypto模块是核心模块之一,用于加密解密数据。要深入学习如何使用Crypto模块进行数据加密解密,建议参阅《NodeJS中文文档:入门到精通》。这份文档提供了全面的指导和示例,帮助你理解加密算法和安全实践。 参考资源链接:[NodeJS中文文档:入门到精通](https://wenku.youkuaiyun.com/doc/53mfrd87nd?spm=1055.2569.3001.10343) 要在Node.js中使用Crypto模块进行数据加密解密,首先需要创建一个`crypto.Crypto`对象。你可以使用不同的加密算法,如`aes-256-cbc`。以下是一个简单的示例,展示如何使用Crypto模块进行加密解密操作: ```javascript const crypto = require('crypto'); // 创建加密密钥和初始化向量(IV) const key = crypto.randomBytes(32); const iv = crypto.randomBytes(16); // 加密函数 function encrypt(text, key, iv) { const cipher = crypto.createCipheriv('aes-256-cbc', key, iv); let encrypted = cipher.update(text); encrypted = Buffer.concat([encrypted, cipher.final()]); return encrypted.toString('hex'); } // 解密函数 function decrypt(encryptedText, key, iv) { const decipher = crypto.createDecipheriv('aes-256-cbc', key, iv); let decrypted = decipher.update(Buffer.from(encryptedText, 'hex')); decrypted = Buffer.concat([decrypted, decipher.final()]); return decrypted.toString(); } // 使用加密函数 const text = 'Hello, world!'; const encrypted = encrypt(text, key, iv); console.log('Encrypted:', encrypted); // 使用解密函数 const decrypted = decrypt(encrypted, key, iv); console.log('Decrypted:', decrypted); ``` 在这个示例中,我们定义了`encrypt`和`decrypt`函数,分别用于加密解密字符串。我们使用了随机生成的密钥和初始化向量(IV),这对于加密过程是必要的,以确保每次加密的结果都是不同的。加密函数将明文转换为十六进制字符串形式,而解密函数则将十六进制字符串转换回原始文本。 学习完这个示例后,你可以继续深入研究《NodeJS中文文档:入门到精通》,以了解更多的加密算法和高级加密技术。这份文档是构建安全Node.js应用的重要参考资料,它不仅提供了一个加密解密的简单示例,还涵盖了其他安全相关的模块和最佳实践。 参考资源链接:[NodeJS中文文档:入门到精通](https://wenku.youkuaiyun.com/doc/53mfrd87nd?spm=1055.2569.3001.10343)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值