<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.sodium.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'zh',
  ),
  'this' => 
  array (
    0 => 'function.sodium-crypto-secretbox-open.php',
    1 => 'sodium_crypto_secretbox_open',
    2 => 'Authenticated shared-key decryption',
  ),
  'up' => 
  array (
    0 => 'ref.sodium.php',
    1 => 'Sodium 函数',
  ),
  'prev' => 
  array (
    0 => 'function.sodium-crypto-secretbox-keygen.php',
    1 => 'sodium_crypto_secretbox_keygen',
  ),
  'next' => 
  array (
    0 => 'function.sodium-crypto-secretstream-xchacha20poly1305-init-pull.php',
    1 => 'sodium_crypto_secretstream_xchacha20poly1305_init_pull',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/sodium/functions/sodium-crypto-secretbox-open.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.sodium-crypto-secretbox-open" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">sodium_crypto_secretbox_open</h1>
  <p class="verinfo">(PHP 7 &gt;= 7.2.0, PHP 8)</p><p class="refpurpose"><span class="refname">sodium_crypto_secretbox_open</span> &mdash; <span class="dc-title">Authenticated shared-key decryption</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.sodium-crypto-secretbox-open-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>sodium_crypto_secretbox_open</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$ciphertext</code></span>, <span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$nonce</code></span>, <span class="methodparam"><span class="attribute"><a href="class.sensitiveparameter.php">#[\SensitiveParameter]</a> </span><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$key</code></span>): <span class="type"><span class="type"><a href="language.types.string.php" class="type string">string</a></span>|<span class="type"><a href="language.types.singleton.php" class="type false">false</a></span></span></div>

  <p class="simpara">
   Decrypt an encrypted message with a symmetric (shared) key.
  </p>

 </div>


 <div class="refsect1 parameters" id="refsect1-function.sodium-crypto-secretbox-open-parameters">
  <h3 class="title">参数</h3>
  <dl>
   
    <dt><code class="parameter">ciphertext</code></dt>
    <dd>
     <span class="simpara">
      Must be in the format provided by <span class="function"><a href="function.sodium-crypto-secretbox.php" class="function">sodium_crypto_secretbox()</a></span>
      (ciphertext and tag, concatenated).
     </span>
    </dd>
   
   
    <dt><code class="parameter">nonce</code></dt>
    <dd>
     <span class="simpara">
      A number that must be only used once, per message. 24 bytes long.
      This is a large enough bound to generate randomly (i.e. <span class="function"><a href="function.random-bytes.php" class="function">random_bytes()</a></span>).
     </span>
    </dd>
   
   
    <dt><code class="parameter">key</code></dt>
    <dd>
     <span class="simpara">
      Encryption key (256-bit).
     </span>
    </dd>
   
  </dl>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.sodium-crypto-secretbox-open-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="simpara">
   The decrypted string on success 或者在失败时返回 <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>.
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-function.sodium-crypto-secretbox-open-errors">
  <h3 class="title">错误／异常</h3>
  <ul class="itemizedlist">
   <li class="listitem">
    <span class="simpara">
     If <code class="parameter">nonce</code> has a length of bytes different than
     <a href="sodium.constants.php#constant.sodium-crypto-secretbox-noncebytes" class="link"><strong><code><a href="sodium.constants.php#constant.sodium-crypto-secretbox-noncebytes">SODIUM_CRYPTO_SECRETBOX_NONCEBYTES</a></code></strong></a>
     (24 bytes), a <span class="classname"><a href="class.sodiumexception.php" class="classname">SodiumException</a></span> will be thrown.
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     If <code class="parameter">key</code> has a length of bytes different than
     <a href="sodium.constants.php#constant.sodium-crypto-secretbox-keybytes" class="link"><strong><code><a href="sodium.constants.php#constant.sodium-crypto-secretbox-keybytes">SODIUM_CRYPTO_SECRETBOX_KEYBYTES</a></code></strong></a>
     (32 bytes), a <span class="classname"><a href="class.sodiumexception.php" class="classname">SodiumException</a></span> will be thrown.
    </span>
   </li>
  </ul>
 </div>


 <div class="refsect1 examples" id="refsect1-function.sodium-crypto-secretbox-open-examples">
  <h3 class="title">示例</h3>
  <div class="example" id="example-1">
   <p><strong>示例 #1 <span class="function"><strong>sodium_crypto_secretbox_open()</strong></span> example</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// The $key must be kept confidential<br /></span><span style="color: #0000BB">$key </span><span style="color: #007700">= </span><span style="color: #0000BB">random_bytes</span><span style="color: #007700">(</span><span style="color: #0000BB">SODIUM_CRYPTO_SECRETBOX_KEYBYTES</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">// Do not reuse $nonce with the same key<br /></span><span style="color: #0000BB">$nonce </span><span style="color: #007700">= </span><span style="color: #0000BB">random_bytes</span><span style="color: #007700">(</span><span style="color: #0000BB">SODIUM_CRYPTO_SECRETBOX_NONCEBYTES</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$ciphertext </span><span style="color: #007700">= </span><span style="color: #0000BB">sodium_crypto_secretbox</span><span style="color: #007700">(</span><span style="color: #DD0000">'message to be encrypted'</span><span style="color: #007700">, </span><span style="color: #0000BB">$nonce</span><span style="color: #007700">, </span><span style="color: #0000BB">$key</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// The same nonce and key are required to decrypt the $ciphertext<br /></span><span style="color: #0000BB">$plaintext </span><span style="color: #007700">= </span><span style="color: #0000BB">sodium_crypto_secretbox_open</span><span style="color: #007700">(</span><span style="color: #0000BB">$ciphertext</span><span style="color: #007700">, </span><span style="color: #0000BB">$nonce</span><span style="color: #007700">, </span><span style="color: #0000BB">$key</span><span style="color: #007700">);<br />if (</span><span style="color: #0000BB">$plaintext </span><span style="color: #007700">!== </span><span style="color: #0000BB">false</span><span style="color: #007700">) {<br />    echo </span><span style="color: #0000BB">$plaintext </span><span style="color: #007700">. </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

   <div class="example-contents"><p>以上示例会输出：</p></div>
   <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">message to be encrypted</pre>
</div>
   </div>
  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.sodium-crypto-secretbox-open-seealso">
  <h3 class="title">参见</h3>
  <ul class="simplelist">
   <li><span class="function"><a href="function.sodium-crypto-secretbox.php" class="function" rel="rdfs-seeAlso">sodium_crypto_secretbox()</a> - Authenticated shared-key encryption</span></li>
   <li><span class="function"><a href="function.sodium-crypto-secretbox-keygen.php" class="function" rel="rdfs-seeAlso">sodium_crypto_secretbox_keygen()</a> - Generate random key for sodium_crypto_secretbox</span></li>
   <li><span class="function"><a href="function.random-bytes.php" class="function" rel="rdfs-seeAlso">random_bytes()</a> - Get cryptographically secure random bytes</span></li>
  </ul>
 </div>


</div><?php manual_footer($setup); ?>