【问题标题】:Sodium Crypto box seal open not working in PHP钠加密盒密封打开在 PHP 中不起作用
【发布时间】:2019-03-19 19:04:26
【问题描述】:

所以我试图让 libsodium 的 sodium_crypto_box_sealsodium_crypto_box_seal_open 工作,但由于某种原因,打开失败,我不知道为什么。

因此,在我所有的尝试中,我构建了一个测试系统,其中包含一个 PHP 文件,用于测试它如何跨服务器工作。

<pre>
<?php
/*** Client Sending ***/
// saved argument
$remotePublic = "DXOCV4BU6ptxt2IwKZaP23S4CjLESfLE+ng1tMS3tg4=";

// create out key for this message
$key = sodium_crypto_box_keypair();

// encrypt our message using the remotePublic
$sealed = sodium_crypto_box_seal("This is a test", base64_decode($remotePublic));
$send = json_encode((object)array("pub" => base64_encode(sodium_crypto_box_publickey($key)), "msg" => base64_encode($sealed)));
echo "Sending : {$send} \r\n";

/*** Server Setup ***/
$payload = json_decode($send);
$apps = 
array (
  'test' => 
  array (
    'S' => 'lv/dT3YC+Am1MCllkHeA2r3D25HW0zPjRrqzR8sepv4=',
    'P' => 'DXOCV4BU6ptxt2IwKZaP23S4CjLESfLE+ng1tMS3tg4=',
  ),
);

/*** Server Opening ***/
$msg = $payload->msg;
$key = sodium_crypto_box_keypair_from_secretkey_and_publickey(base64_decode($apps['test']['S']), base64_decode($apps['test']['P']));
$opened = sodium_crypto_box_seal_open(base64_decode($msg), $key);
echo "Opened : {$opened} \r\n";

/*** Server Responding ***/
$sealedResp = base64_encode(sodium_crypto_box_seal("We Got your message '{$opened}'", base64_decode($payload->pub)));
echo "Responding : {$sealedResp}\r\n";

/*** Client Receiving ***/
$received = sodium_crypto_box_seal_open(base64_decode($sealedResp), $key);
echo "Received : {$received}\r\n";

/*** Sanity Checking ***/
if($received == "We Got your message 'This is a test'"){
    echo "Test Successfull.\r\n";
}else{
    echo "Test Failed got '{$received}' is not \"We Got your message 'This is a test'\"\r\n";
}
?>
</pre>

输出是:

Sending : {"pub":"DS2uolF5lXZ1E3rw0V2WHELAKj6+vRKnxGPQFlhTEFU=","msg":"VVYfphc2RnQL2E8A0oOdc6E\/+iUgWO1rPd3rfodjLhE+slEWsivB6QiaLiMuQ31XMP\/1\/s+t+CSHu8QukoY="} 
Opened : This is a test 
Responding : cvDN9aT9Xj7DPRhYZFGOR4auFnAcI3qlwVBBRY4mN28JmagaR8ZR9gt6W5C0xyt06AdrQR+sZFcyb500rx6iDTEC4n/H77cUM81vy2WfV8m5iRgp
Received : 
Test Failed got '' is not "We Got your message 'This is a test'"

【问题讨论】:

  • 查看sodium_crypto_box_seal和朋友的返回值:失败时可以返回false。此外,检查您的日志(如果需要,打开您的 log_level)。这至少会缩小它开始偏离轨道的地方。
  • 错误日志设置为 E_ALL 并且 display_errors 设置为 true,所以我会查看是否有错误,如果它是 false,我不会在 base64 编码输出中看到任何数据。 ``的base64仍然是``

标签: php public-key-encryption libsodium


【解决方案1】:

这里有两个问题。

首先——在“服务器打开”下的这一步:

$opened = sodium_crypto_box_seal_open($msg, $key);

$msg 仍然是 Base64 编码,因此尝试解密它会失败。

第二——包含在$send"pub"字段中的公钥是由sodium_crypto_box_keypair()生成的随机密钥对的公钥,不是相同的公钥如$remotePublic$apps 中的一对。稍后在应用程序中调用 sodium_crypto_box_keypair_from_secretkey_and_publickey() 会覆盖此密钥,从而使原始消息无法恢复。

【讨论】:

  • 第二个,是的,公钥是服务器每次响应的随机生成。但这将客户端固定到服务器,但没有将服务器固定到客户端
  • 你希望它完成什么?如果您为每个响应生成一个新的密钥对并将其丢弃,您将无法读取使用这些密钥加密的任何消息。
  • 接收与发送在同一执行中被解析,所以它去发送等待接收
  • 啊,找到了,因为它都在一个脚本中,我正在覆盖$key
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-22
  • 2017-07-16
  • 2020-09-18
  • 2016-11-07
相关资源
最近更新 更多