【问题标题】:How transform this mcrypt php code to the same code in nodejs?如何将此 mcrypt php 代码转换为 nodejs 中的相同代码?
【发布时间】:2018-03-30 02:22:43
【问题描述】:

我需要转换这个 php 代码:

$cipher_alg = MCRYPT_TRIPLEDES;
$key = "thekey";
$iv = mcrypt_create_iv(mcrypt_get_iv_size($cipher_alg, MCRYPT_MODE_ECB), MCRYPT_RAND);
$encrypted_string = mcrypt_encrypt($cipher_alg, $key, $string, MCRYPT_MODE_ECB, $iv);

return base64_encode($encrypted_string);

到 nodejs。

我使用https://github.com/tugrul/node-mcrypt 进行了测试,但使用相同的字符串,加密的结果不一样:

代码 nodejs 测试:

let blowfishCfb = new MCrypt('tripledes', 'ecb');
let iv = blowfishCfb.generateIv();
blowfishCfb.validateKeySize(false);
blowfishCfb.validateIvSize(false);
blowfishCfb.open('thekey', iv);

let ciphertext = blowfishCfb.encrypt(text);

return Buffer.concat([iv, ciphertext]).toString('base64');

你能帮忙理解一下吗?

谢谢,

【问题讨论】:

  • 哇,四种糟糕的安全选择的组合:三重 DES、ECB 模式、Blowfish 和空填充!另请注意,ECB 模式使用 IV。

标签: php node.js mcrypt


【解决方案1】:

不要将 IV 和密码连接在一起:

let blowfishCfb = new MCrypt('tripledes', 'ecb');
let iv = blowfishCfb.generateIv();
blowfishCfb.validateKeySize(false);
blowfishCfb.validateIvSize(false);
blowfishCfb.open('thekey', iv);

let ciphertext = blowfishCfb.encrypt(text);

return ciphertext.toString('base64');

【讨论】:

  • ECB 模式不使用 IV,因此创建一个没有意义。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-19
  • 2019-05-26
  • 2018-06-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多