函数名称:sodium_crypto_aead_xchacha20poly1305_ietf_decrypt()
函数描述:该函数用于解密使用XChaCha20-Poly1305-IETF算法加密的数据。
适用版本:PHP 7.2.0及以上版本。
语法:sodium_crypto_aead_xchacha20poly1305_ietf_decrypt(string $ciphertext, string $additional_data, string $nonce, string $key) : string|false
参数:
返回值:
示例:
$key = random_bytes(SODIUM_CRYPTO_AEAD_XCHACHA20POLY1305_IETF_KEYBYTES);
$nonce = random_bytes(SODIUM_CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NPUBBYTES);
$message = 'This is a secret message';
// 加密
$ciphertext = sodium_crypto_aead_xchacha20poly1305_ietf_encrypt($message, $additional_data, $nonce, $key);
// 解密
$decrypted = sodium_crypto_aead_xchacha20poly1305_ietf_decrypt($ciphertext, $additional_data, $nonce, $key);
echo $decrypted; // 输出:This is a secret message
注意事项:
sodium_crypto_aead_xchacha20poly1305_ietf_keygen()和sodium_crypto_aead_xchacha20poly1305_ietf_npubbytes()函数生成安全的随机值。