<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.openssl.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'zh',
  ),
  'this' => 
  array (
    0 => 'function.openssl-open.php',
    1 => 'openssl_open',
    2 => '打开密封的数据',
  ),
  'up' => 
  array (
    0 => 'ref.openssl.php',
    1 => 'OpenSSL 函数',
  ),
  'prev' => 
  array (
    0 => 'function.openssl-get-publickey.php',
    1 => 'openssl_get_publickey',
  ),
  'next' => 
  array (
    0 => 'function.openssl-password-hash.php',
    1 => 'openssl_password_hash',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'zh',
    'path' => 'reference/openssl/functions/openssl-open.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.openssl-open" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">openssl_open</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.0.4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">openssl_open</span> &mdash; <span class="dc-title">打开密封的数据</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.openssl-open-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>openssl_open</strong></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$data</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<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 reference">&$output</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$encrypted_key</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="attribute"><a href="class.sensitiveparameter.php">#[\SensitiveParameter]</a> </span><span class="type"><span class="type"><a href="class.opensslasymmetrickey.php" class="type OpenSSLAsymmetricKey">OpenSSLAsymmetricKey</a></span>|<span class="type"><a href="class.opensslcertificate.php" class="type OpenSSLCertificate">OpenSSLCertificate</a></span>|<span class="type"><a href="language.types.array.php" class="type array">array</a></span>|<span class="type"><a href="language.types.string.php" class="type string">string</a></span></span> <code class="parameter">$private_key</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$cipher_algo</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><span class="type"><a href="language.types.null.php" class="type null">?</a></span><span class="type"><a href="language.types.string.php" class="type string">string</a></span></span> <code class="parameter">$iv</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></span></span><br>): <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>openssl_open()</strong></span> 使用通过 <code class="parameter">private_key</code> 从 <code class="parameter">encrypted_key</code> 解密得到的信封密钥来打开/解密
   <code class="parameter">data</code>。解密过程采用 <code class="parameter">cipher_algo</code> 和 <code class="parameter">iv</code>。仅当加密方法需要时，才需提供
   iv。该函数将解密后的数据填充至 <code class="parameter">output</code>。信封密钥通常在使用与私钥对应的公钥密封数据时生成。更多信息请参阅 <span class="function"><a href="function.openssl-seal.php" class="function">openssl_seal()</a></span>。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.openssl-open-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">data</code></dt>
     <dd>
      <p class="para">
       密封数据。
      </p>
     </dd>
    
    
     <dt><code class="parameter">output</code></dt>
     <dd>
      <p class="para">
       如果调用成功，则在这个参数中返回打开的数据。
      </p>
     </dd>
    
    
     <dt><code class="parameter">encrypted_key</code></dt>
     <dd>
      <p class="para">
       可使用 <code class="parameter">private_key</code> 解密的对称加密密钥。
      </p>
     </dd>
    
    
     <dt><code class="parameter">private_key</code></dt>
     <dd>
      <p class="para">
       用于解密 <code class="parameter">encrypted_key</code> 的私钥。
      </p>
     </dd>
    
    
     <dt><code class="parameter">cipher_algo</code></dt>
     <dd>
      <p class="para">
       用于解密 <code class="parameter">data</code> 的密码算法。
       <div class="caution"><strong class="caution">警告</strong>
        <p class="simpara">
         PHP 8.0 之前版本的默认值为（<code class="literal">&#039;RC4&#039;</code>），该算法存在安全隐患。强烈建议明确指定安全密码方法。
        </p>
       </div>
      </p>
     </dd>
    
    
     <dt><code class="parameter">iv</code></dt>
     <dd>
      <p class="para">
       用于解密 <code class="parameter">data</code> 的初始化向量。当密码算法需要 IV 时，必须提供该参数。可通过调用
       <span class="function"><a href="function.openssl-cipher-iv-length.php" class="function">openssl_cipher_iv_length()</a></span> 并传入 <code class="parameter">cipher_algo</code> 来确认是否需要 IV。
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.openssl-open-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功时返回 <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong>， 或者在失败时返回 <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.openssl-open-changelog">
  <h3 class="title">更新日志</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>版本</th>
      <th>说明</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.0.0</td>
      <td>
       <code class="parameter">private_key</code> 现在接受 <span class="classname"><a href="class.opensslasymmetrickey.php" class="classname">OpenSSLAsymmetricKey</a></span>
       或 <span class="classname"><a href="class.opensslcertificate.php" class="classname">OpenSSLCertificate</a></span> 实例；之前接受类型 <code class="literal">OpenSSL key</code>
       或 <code class="literal">OpenSSL X.509 CSR</code> 的 <span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span>。
      </td>
     </tr>

     <tr>
      <td>8.0.0</td>
      <td>
       <code class="parameter">cipher_algo</code> 不再是可选参数。
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.openssl-open-examples">
  <h3 class="title">示例</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>示例 #1 <span class="function"><strong>openssl_open()</strong></span> 示例</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #FF8000">// $sealed、$env_key 和 $iv 分别假设包含密封数据、信封密钥和初始化向量，这些参数均由数据密封方提供。<br /><br />// 从 private_key.pem 文件中获取私钥<br /></span><span style="color: #0000BB">$pkey </span><span style="color: #007700">= </span><span style="color: #0000BB">openssl_get_privatekey</span><span style="color: #007700">(</span><span style="color: #DD0000">"file://private_key.pem"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// 解密数据并存储到 $open<br /></span><span style="color: #007700">if (</span><span style="color: #0000BB">openssl_open</span><span style="color: #007700">(</span><span style="color: #0000BB">$sealed</span><span style="color: #007700">, </span><span style="color: #0000BB">$open</span><span style="color: #007700">, </span><span style="color: #0000BB">$env_key</span><span style="color: #007700">, </span><span style="color: #0000BB">$pkey</span><span style="color: #007700">, </span><span style="color: #DD0000">'AES256'</span><span style="color: #007700">, </span><span style="color: #0000BB">$iv</span><span style="color: #007700">)) {<br />    echo </span><span style="color: #DD0000">"Here is the opened data: "</span><span style="color: #007700">, </span><span style="color: #0000BB">$open</span><span style="color: #007700">;<br />} else {<br />    echo </span><span style="color: #DD0000">"Failed to open data"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.openssl-open-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.openssl-seal.php" class="function" rel="rdfs-seeAlso">openssl_seal()</a> - 密封 (加密) 数据</span></li>
   </ul>
  </p>
 </div>


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