【发布时间】:2020-06-26 04:29:55
【问题描述】:
这是我从互联网上获取的nodejs代码。
// Nodejs encryption with CTR
var crypto = require('crypto'),
algorithm = 'aes-128-ofb',
password = 'password12';
function encrypt(text){
var cipher = crypto.createCipher(algorithm,password)
var crypted = cipher.update(text,'utf8','hex')
crypted += cipher.final('hex');
return crypted;
}
function decrypt(text){
var decipher = crypto.createDecipher(algorithm,password)
var dec = decipher.update(text,'hex','utf8')
dec += decipher.final('utf8');
return dec;
}
var hw = encrypt("hello world")
// outputs hello world
console.log(decrypt(hw));
它工作正常
但是当我尝试在 php7 中使用 openssl_decrypt 解密它时,我得到了错误/不同的结果。
有人可以帮忙吗?
【问题讨论】:
-
你能添加你的php代码进行解密吗?
-
有一些库。 This 就是其中之一。
-
@Pete 是的,我正在尝试使用 openssl_decrypt OPENSSL_RAW_DATA 它返回凹凸随机符号..
-
@DavidJawHpan 好的,我会试一试
-
NodeJS代码可以修改吗?
crypto.createCipher已被弃用,并且还使用非标准(而且不是很安全)KDF 来派生密钥和 IV,这必须在 PHP 代码中实现(不过这不会很复杂)。因此,如果可以切换到crypto.createCipheriv,则应该这样做。这更安全,并且可以在此处直接指定密钥和 IV(或通过 reliable KDF 派生)。此外,移植到 PHP 更容易。此外,标题和发布的代码在模式(CTR vs OFB)方面是矛盾的。究竟是什么模式?
标签: php html node.js encryption aes