PHP - 解密加密字符串从Node.js的

问题描述 投票:1回答:2

我想解密使用PHP传递到服务器的加密字符串(在加密的NodeJS)。

我已经找到了完美的NodeJS加密/解密库:Cryptr。我创建我的JavaScript文件发送到我的服务器的请求,在它的加密字符串的连接。

现在基本上我想解密字符串。

以一看Cryptr source,似乎他们使用aes-256-ctr作为算法中的方法,并作为sha256加密方法。

我的NodeJS:https://runkit.com/embed/keu82yjhwyxj

加密字符串:1d510024ad0a5da624b76a2be72022bff3aaadfe8ac5e0b6c178b00333

然后,我这样做在PHP中:

<?php
$encrypted = "1d510024ad0a5da624b76a2be72022bff3aaadfe8ac5e0b6c178b00333";
$algorithm = "aes-256-ctr";
$secret_key = "myTotalySecretKey"; 
$iv = "";

$decrypted_data = openssl_decrypt(pack('H*', $encrypted), $algorithm, $secret_key, OPENSSL_RAW_DATA, $iv);
echo $decrypted_data;

但由于IV在Cryptr随机生成的,我将如何在PHP中生成的?

我怎样才能解密Cryptr加密使用PHP字符串,因此它返回“我爱披萨!”?


编辑:

相反包( 'H *',$加密)尝试$仅加密。另外,你需要解密唯一的数据,方法和关键。

<?php
$encrypted = "1d510024ad0a5da624b76a2be72022bff3aaadfe8ac5e0b6c178b00333";
$algorithm = "aes-256-ctr";
$secret_key = "myTotalySecretKey";

$decrypted_data = openssl_decrypt($encrypted, $algorithm, $secret_key);
echo $decrypted_data;
php node.js encryption
2个回答
2
投票

不要使用CRYPTR。这是不安全的。与开发商I have opened an issue,虽然我不知道怎么能不固定模块的完全重写。

Cryptr使用CTR加密模式。这种模式是专为特定的使用情况,并且不延展性抵抗攻击。如果任何加密消息的内容是已知的,所以可以认为消息转换成任何其他消息。例如,给定从使用样品加密字符串:

const cryptr = new Cryptr('myTotalySecretKey');

const encryptedString = cryptr.encrypt('bacon');
const decryptedString = cryptr.decrypt(encryptedString);

console.log(encryptedString); // "bcb23b81c4839d06644792878e569de4f251f08007"

(请注意,加密的字符串甚至不是相同的长度,什么是在模块的使用示出,这是他们的文档中的表观错误。)

如果没有密钥的知识,就可以修改该字符串,使之解密为“Hello”:

var tmp = Buffer.from(encryptedString, "hex");
var b1 = Buffer.from("bacon"), b2 = Buffer.from("hello");
for (var i = 0; i < b1.length; i++) {
    tmp[i + 16] ^= b1[i] ^ b2[i];
}
var ep = tmp.toString("hex");
console.log(ep); // "bcb23b81c4839d06644792878e569de4f855ff8306"

事实上:

var dp = cryptr.decrypt(ep);
console.log(dp); // "hello"

这是从加密角度来看,真正的大事。攻击者刚刚修改过境加密的消息,你有没有检测它的方式。

不要使用此模块。如果您需要携带加密,使用the Sodium library;有可用于两个节点和PHP绑定。


0
投票

你需要通过IV为您传递消息和安全密钥。

所以我做小测试

$encrypted = openssl_encrypt("test", "aes-256-ctr", "123", 0, "aaaaaaaaaaaaaaaa");
$algorithm = "aes-256-ctr";
$secret_key = "123"; 
$iv = "";

$decrypted_data = openssl_decrypt($encrypted, $algorithm, $secret_key, 0, "aaaaaaaaaaaaaaaa");
echo $decrypted_data;

和它的作品。是的,它是随机生成的,但你可以和必须通过它(如加密消息)到服务器。

我不知道是什么IV参数的目的,但PHP给你警告,如果IV参数是空的。

我刚刚发现这个GitHub的页面上Cryptr。它说:

IV是随机生成的,预先考虑结果

© www.soinside.com 2019 - 2024. All rights reserved.