1

我是 php 和 stackoverflow 的新手,因此可能会发生一些错误。如果是,请通知我,我会尽快解决。

我的加密功能:

function encrypt($data){
$nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
$ciphertext = sodium_crypto_secretbox($data, $nonce, $GLOBALS['key']);
$ciphertext = base64_encode($ciphertext);
$ciphertext .= "[-SPLIT-]" . base64_encode($nonce);

return $ciphertext;}

我的解密功能:

function decrypt($data){   
$data = explode("[-SPLIT-]", $data);
$ciphertext = base64_decode($data[0]);
$nonce = base64_decode($data[1]);
$plaintext = sodium_crypto_secretbox_open($ciphertext, $nonce, $GLOBALS['key']);

if(!$plaintext){$plaintext = "FAILED";}
return $plaintext;}

我已经做了足够的测试,知道我正在将从加密函数返回的 $ciphertext 传递给解密函数。

如何创建一个函数来返回在我的加密函数中加密的解密字符串?

4

1 回答 1

0

发现了我的问题,一个简单的 print_r() 把我搞砸了。上面的代码按预期工作,我将很快发布一些改进。

于 2021-03-13T05:58:08.927 回答