<?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-box-seal.php',
    1 => 'sodium_crypto_box_seal',
    2 => 'Anonymous public-key encryption',
  ),
  'up' => 
  array (
    0 => 'ref.sodium.php',
    1 => 'Sodium 函数',
  ),
  'prev' => 
  array (
    0 => 'function.sodium-crypto-box-publickey-from-secretkey.php',
    1 => 'sodium_crypto_box_publickey_from_secretkey',
  ),
  'next' => 
  array (
    0 => 'function.sodium-crypto-box-seal-open.php',
    1 => 'sodium_crypto_box_seal_open',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/sodium/functions/sodium-crypto-box-seal.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.sodium-crypto-box-seal" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">sodium_crypto_box_seal</h1>
  <p class="verinfo">(PHP 7 &gt;= 7.2.0, PHP 8)</p><p class="refpurpose"><span class="refname">sodium_crypto_box_seal</span> &mdash; <span class="dc-title">Anonymous public-key encryption</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.sodium-crypto-box-seal-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>sodium_crypto_box_seal</strong></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">$message</code></span>, <span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$public_key</code></span>): <span class="type"><a href="language.types.string.php" class="type string">string</a></span></div>

  <p class="simpara">
   Encrypt a message such that only the recipient can decrypt it.
  </p>
  <p class="simpara">
   Unlike with <span class="function"><a href="function.sodium-crypto-box.php" class="function">sodium_crypto_box()</a></span>, you only need to know the recipient&#039;s
   public key to use <span class="function"><strong>sodium_crypto_box_seal()</strong></span>. One consequence of this
   convenience, however, is that the ciphertext isn&#039;t bound to a static public key,
   and is therefore not authenticated. Hence, anonymous public-key encryption.
  </p>
  <p class="simpara">
   <span class="function"><strong>sodium_crypto_box_seal()</strong></span> still provides ciphertext integrity. Just not
   sender identity authentication.
  </p>
  <p class="simpara">
   If you also need sender authentication, the <span class="function"><a href="function.sodium-crypto-sign.php" class="function">sodium_crypto_sign()</a></span> functions
   are likely the best place to start.
  </p>

 </div>


 <div class="refsect1 parameters" id="refsect1-function.sodium-crypto-box-seal-parameters">
  <h3 class="title">参数</h3>
  <dl>
   
    <dt><code class="parameter">message</code></dt>
    <dd>
     <span class="simpara">
      The message to encrypt.
     </span>
    </dd>
   
   
    <dt><code class="parameter">public_key</code></dt>
    <dd>
     <span class="simpara">
      The public key that corresponds to the only key that can decrypt the message.
     </span>
    </dd>
   
  </dl>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.sodium-crypto-box-seal-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="simpara">
   A ciphertext string in the format of (one-time public key, encrypted message, authentication tag).
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.sodium-crypto-box-seal-examples">
  <h3 class="title">示例</h3>
  <div class="example" id="sodium-crypto-box-seal.example.basic">
   <p><strong>示例 #1 <span class="function"><strong>sodium_crypto_box_seal()</strong></span> example</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$keypair </span><span style="color: #007700">= </span><span style="color: #0000BB">sodium_crypto_box_keypair</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$public_key </span><span style="color: #007700">= </span><span style="color: #0000BB">sodium_crypto_box_publickey</span><span style="color: #007700">(</span><span style="color: #0000BB">$keypair</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Obfuscated plaintext to make the example more fun<br /></span><span style="color: #0000BB">$plaintext_b64 </span><span style="color: #007700">= </span><span style="color: #DD0000">"V3JpdGluZyBzb2Z0d2FyZSBpbiBQSFAgY2FuIGJlIGEgZGVsaWdodCE="</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$decoded_plaintext </span><span style="color: #007700">= </span><span style="color: #0000BB">sodium_base642bin</span><span style="color: #007700">(</span><span style="color: #0000BB">$plaintext_b64</span><span style="color: #007700">, </span><span style="color: #0000BB">SODIUM_BASE64_VARIANT_ORIGINAL</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$sealed </span><span style="color: #007700">= </span><span style="color: #0000BB">sodium_crypto_box_seal</span><span style="color: #007700">(</span><span style="color: #0000BB">$decoded_plaintext</span><span style="color: #007700">, </span><span style="color: #0000BB">$public_key</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">base64_encode</span><span style="color: #007700">(</span><span style="color: #0000BB">$sealed</span><span style="color: #007700">));<br /><br /></span><span style="color: #0000BB">$opened </span><span style="color: #007700">= </span><span style="color: #0000BB">sodium_crypto_box_seal_open</span><span style="color: #007700">(</span><span style="color: #0000BB">$sealed</span><span style="color: #007700">, </span><span style="color: #0000BB">$keypair</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$opened</span><span style="color: #007700">);<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">string(120) &quot;oRBXXAV4iQBrxlV4A21Bord8Yo/D8ZlrIIGNyaRCcGBfpz0map52I3xq6l+CST+1NSgQkbV+HiYyFjXWiWiaCGupGf+zl4bgWj/A9Adtem7Jt3h3emrMsLw=&quot;
string(41) &quot;Writing software in PHP can be a delight!&quot;</pre>
</div>
   </div>
  </div>
 </div>


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