【发布时间】:2019-12-07 04:15:01
【问题描述】:
我正在尝试在 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
为什么这些值不同,如何使它们相同?
【问题讨论】:
标签: php node.js encryption