You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Node.js实现与VB.NET兼容的3DES-ECB-PKCS7加解密

Node.js 对应3DES加解密实现方案

原代码错误原因

  • 算法选择错误:原VB代码使用*TripleDES(3DES)*加密,你的代码误用了AES-192算法,两者加密逻辑完全不兼容
  • 参数逻辑颠倒:原逻辑中密钥由Passphrase参数生成,加密对象是Message参数,你的代码中将Message作为密钥、Passphrase作为加密内容,逻辑完全反向
  • 缺少密钥生成步骤:原3DES密钥是Passphrase经过UTF8编码后计算MD5哈希得到的16字节值,你的代码中直接将输入字符串作为密钥,不符合规则

完整可运行代码

const crypto = require('crypto');

// 对应VB的EncryptString方法
function encrypt(message, passphrase) {
    // 计算Passphrase的MD5哈希作为3DES密钥
    const key = crypto.createHash('md5').update(passphrase, 'utf8').digest();
    // 创建3DES ECB模式加密实例,ECB模式不需要IV传null即可
    const cipher = crypto.createCipheriv('des-ede-ecb', key, null);
    // 加密并输出base64格式结果
    return cipher.update(message, 'utf8', 'base64') + cipher.final('base64');
}

// 对应VB的DecryptString方法
function decrypt(encryptedBase64, passphrase) {
    const key = crypto.createHash('md5').update(passphrase, 'utf8').digest();
    const decipher = crypto.createDecipheriv('des-ede-ecb', key, null);
    return decipher.update(encryptedBase64, 'base64', 'utf8') + decipher.final('utf8');
}

// 测试验证
console.log(encrypt('stack-overflow', 'Danielle')); // 输出:1c2wL+guV34oyVS0vkxwVA==
console.log(decrypt('1c2wL+guV34oyVS0vkxwVA==', 'Danielle')); // 输出:stack-overflow

该实现和原VB逻辑完全对齐,依赖Node.js原生crypto模块,无需安装第三方MD5包。

内容的提问来源于stack exchange,提问作者Danielle

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 13:09:04