Sodium Crypto 盒子密封打开在 PHP 中不起作用
Sodium Crypto box seal open not working in PHP
所以我正在尝试让 libsodium 的 sodium_crypto_box_seal
and sodium_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'"
这里有两个问题。
首先——在"Server Opening"下的这一步:
$opened = sodium_crypto_box_seal_open($msg, $key);
$msg
仍然是 Base64 编码,因此尝试解密它会失败。
其次 -- $send
的 "pub"
字段中包含的 public 密钥是由 [= 生成的随机密钥对的 public 密钥14=]、不是 与$remotePublic
或$apps
中的密钥对相同的public 密钥。稍后在应用程序中调用 sodium_crypto_box_keypair_from_secretkey_and_publickey()
会覆盖此密钥,从而使原始消息无法恢复。
所以我正在尝试让 libsodium 的 sodium_crypto_box_seal
and sodium_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'"
这里有两个问题。
首先——在"Server Opening"下的这一步:
$opened = sodium_crypto_box_seal_open($msg, $key);
$msg
仍然是 Base64 编码,因此尝试解密它会失败。
其次 -- $send
的 "pub"
字段中包含的 public 密钥是由 [= 生成的随机密钥对的 public 密钥14=]、不是 与$remotePublic
或$apps
中的密钥对相同的public 密钥。稍后在应用程序中调用 sodium_crypto_box_keypair_from_secretkey_and_publickey()
会覆盖此密钥,从而使原始消息无法恢复。