在php vs nodejs中加密字符串



我正在尝试在php中加密字符串的(创建哈希(,并在nodejs中复制行为。我不担心字符串的安全性。这两个字符串都应匹配。

这是我拥有的PHP代码:

$key = 'supersecretkey';
$plaintext = "example string to encrypt";
$cipher = "rc4-hmac-md5";
$encryptedText = openssl_encrypt($plaintext, $cipher, $key);
echo $encryptedText . "n";

这是我的输出

> php crypto.php
cRRDH1KSTZmbWLx+h0Q/l17jfDeAsQb/GA==

这是我尝试过的nodejs:

var crypto = require('crypto'),
  algorithm = 'rc4-hmac-md5',
  password = 'supersecretkey';
function encrypt(text){
  var cipher = crypto.createCipher(algorithm,password)
  var crypted = cipher.update(text,'utf8','hex')
  crypted += cipher.final('hex');
  return crypted;
}
var hw = encrypt("example string to encrypt")
// outputs hello world
console.log(hw);

这是输出:

> node crypto.js
7ddd856a0227489b5cabda26e82eb99fb0c2ec9b6dfb477d43

为什么这些值不同?我如何使它们相同?

当我不熟悉这两个特定API时,我确实注意到您使用的是带有OpenSSL的初始化向量,但与Node.JS。我会尝试使用node.js crypto.createCiperhiv表单(https://nodejs.org/api/crypto.html#crypto_crypto_createcipheriv_algorithm_key_key_key_key_iv_options(,并确保使用相同的精确初始化初始化bytes bytes bytes bytes for Php and Node.js(您是您的node.js(为了创建一些任意的ASCII字符串,例如,"我的初始化向量",适当的大小(。

(初始化矢量是圆锥形密码的另一种配置输入,它是引入其他随机性/可变性的种子(

最新更新