Sodium Crypto box seal не работает в PHP - PullRequest
0 голосов
/ 19 марта 2019

Так что я пытаюсь заставить работать libsodium sodium_crypto_box_seal и sodium_crypto_box_seal_open, но по какой-то причине открытие не удается, и я не могу понять, почему.

Итак, во всех моих попытках заставить это работать, я построил тестовую систему в виде одного файла PHP, который проверяет, как он будет работать на разных серверах.

<code><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";
}
?>

Вывод:

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'"

1 Ответ

3 голосов
/ 19 марта 2019

Здесь две проблемы.

Сначала - на этом шаге в разделе «Открытие сервера»:

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

$msg все еще закодирован в Base64, поэтому попытка расшифровать его не удастся.

Второе - открытый ключ, включенный в поле "pub" $send, является открытым ключом случайной пары ключей, сгенерированной sodium_crypto_box_keypair(), , а не того же открытого ключа, что и $remotePublic или пара в $apps. Этот ключ перезаписывается вызовом sodium_crypto_box_keypair_from_secretkey_and_publickey() позже в приложении, что делает исходное сообщение невосстановимым.

...