W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
(PHP 7 >= 7.2.0, PHP 8)
sodium_crypto_secretbox — 經(jīng)過身份驗證的共享密鑰加密
sodium_crypto_secretbox(string $message, string $nonce, string $key): string
使用對稱(共享)密鑰加密消息。
message
要加密的明文消息。
nonce
每封郵件只能使用一次的號碼。長度為 24 個字節(jié)。 這是一個足夠大的綁定,可以隨機(jī)生成(即 random_bytes())。
key
加密密鑰(256 位)。
返回加密的字符串。
示例 #1 sodium_crypto_secretbox() example
<?php
// The $key must be kept confidential
$key = sodium_crypto_secretbox_keygen();
// Do not reuse $nonce with the same key
$nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
$plaintext = "message to be encrypted";
$ciphertext = sodium_crypto_secretbox($plaintext, $nonce, $key);
var_dump(bin2hex($ciphertext));
// The same nonce and key are required to decrypt the $ciphertext
var_dump(sodium_crypto_secretbox_open($ciphertext, $nonce, $key));
?>
以上示例的輸出類似于:
string(78) "3a1fa3e9f7b72ef8be51d40abf8e296c6899c185d07b18b4c93e7f26aa776d24c50852cd6b1076" string(23) "message to be encrypted"
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: