NODE中3DES加解密

1 加解密类

Encryption.js

const crypto = require('crypto');
class Encryption {
    // 秘钥
    secretKey = '12041C132C2B000098980220905b4f90';
    #secretKeySub1 = '';
    #secretKeySub2 = '';
    algorithms = { ecb: 'des-ecb' };
    /**
     * @param {String} secretKey        秘钥
     */
    constructor(secretKey) {
        if (typeof secretKey === 'string') this.secretKey = secretKey;
        this.#secretKeySub1 = this.secretKey.substring(0, 16);
        this.#secretKeySub2 = this.secretKey.substring(16, 32);
    }
    /**
     * crypto加密
     * @param {String} plainText        明文
     * @param {String} secretKeyText    密钥
     * @returns {String}                密文
     */
    #encrypt(plainText, secretKeyText) {
        const key = new Uint8Array(secretKeyText);
        const iv = new Uint8Array('');
        const txt = new Uint8Array(plainText);
        const cipher = crypto.createCipheriv(this.algorithms.ecb, key, iv);
        cipher.setAutoPadding(true);
        const ciph = cipher.update(txt);
        return ciph;
    }
    /**
     * crypto解密
     * @param {String} encryptText      密文
     * @param {String} secretKeyText    密钥
     * @returns {String}                明文
     */
    #decrypt(encryptText, secretKeyText) {
        const key = new Uint8Array(secretKeyText);
        const iv = new Uint8Array('');
        const txt = new Uint8Array(encryptText);
        const deCipher = crypto.createDecipheriv(this.algorithms.ecb, key, iv);
        deCipher.setAutoPadding(true);
        deCipher.update(txt);
        const ciph = deCipher.update(txt);
        return ciph;
    }
    /**
     * 3DES加密
     * @param {String} plainText    明文
     * @returns {String}            密文
     *
     * 3DES算法是指使用双长度(16字节)密钥K=(KL||KR)将8字节明文数据块进行3次DES加密/解密。如:
     * Y = DES( KL[DES-1( KR[DES( KL[X] )] )] )
     *
     * VOID 3DES(BYTE DoubleKeyStr[16], BYTE Data[8], BYTE Out[8])
     * {
     *     BYTE Buf1[8], Buf2[8];
     *     DES (&DoubleKeyStr[0], Data, Buf1);
     *     UDES(&DoubleKeyStr[8], Buf1, Buf2);
     *     DES (&DoubleKeyStr[0], Buf2, Out);
     * }
     */
    encrypt3Des(plainText) {
        let tmp1 = null;
        let tmp2 = null;
        tmp2 = this.#encrypt(
            Buffer.from(plainText, 'hex').toJSON().data,
            Buffer.from(this.#secretKeySub1, 'hex').toJSON().data
        );
        tmp1 = this.#decrypt(tmp2, Buffer.from(this.#secretKeySub2, 'hex').toJSON().data);
        tmp2 = this.#encrypt(tmp1, Buffer.from(this.#secretKeySub1, 'hex').toJSON().data);
        return Buffer.from(tmp2).toString('hex');
    }
    /**
     * 3DES解密
     * @param {String} encryptText  密文
     * @returns {String}            明文
     *
     * 解密方式为:
     * X = DES-1( KL[DES( KR[DES-1( KL[Y] )] )] )
     * 其中,DES( KL[X] )表示用密钥K对数据X进行DES加密,DES-1( KR[Y] )表示用密钥K对数据Y进行解密。
     * SessionKey的计算采用3DES算法,计算出单倍长度的密钥。表示法为:SK = Session(DK,DATA)
     */
    decrypt3Des(encryptText) {
        let tmp1 = null;
        let tmp2 = null;
        tmp2 = this.#decrypt(
            Buffer.from(encryptText, 'hex').toJSON().data,
            Buffer.from(this.#secretKeySub1, 'hex').toJSON().data
        );
        tmp1 = this.#encrypt(tmp2, Buffer.from(this.#secretKeySub2, 'hex').toJSON().data);
        tmp2 = this.#decrypt(tmp1, Buffer.from(this.#secretKeySub1, 'hex').toJSON().data);
        return Buffer.from(tmp2).toString('hex');
    }
}
module.exports = Encryption;

2. 使用/测试

test.js

const Encryption = require('./Encryption');
const des = new Encryption();
const plainText = '0000000000000000';
console.log('原文', plainText);
let encryptText = des.encrypt3Des(plainText);
console.log('3DES密文', encryptText);
let plainText1 = des.decrypt3Des(encryptText);
console.log('解密', plainText1);

结果:

$ node test
原文 0000000000000000
3DES密文 d3e5f3763f975d24
解密 0000000000000000

3.感谢前人栽树

极羽
《nodejs实现3des(2倍长)加密方式,与DES加密工具一致

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值