svn: /phpdoc/zh/trunk/reference/openssl/ book.xml functions/openssl-encrypt.xml

From: Date: Thu, 05 Oct 2017 09:51:16 +0000
Subject: svn: /phpdoc/zh/trunk/reference/openssl/ book.xml functions/openssl-encrypt.xml
Groups: php.doc.zh 
Request: Send a blank email to doc-zh+get-5022@lists.php.net to get a copy of this message
daijie Thu, 05 Oct 2017 09:51:16 +0000 Revision: http://svn.php.net/viewvc?view=revision&revision=343195 Log: Sync with EN. Changed paths: U phpdoc/zh/trunk/reference/openssl/book.xml U phpdoc/zh/trunk/reference/openssl/functions/openssl-encrypt.xml Modified: phpdoc/zh/trunk/reference/openssl/book.xml =================================================================== --- phpdoc/zh/trunk/reference/openssl/book.xml 2017-10-05 09:43:56 UTC (rev 343194) +++ phpdoc/zh/trunk/reference/openssl/book.xml 2017-10-05 09:51:16 UTC (rev 343195) @@ -1,6 +1,6 @@ <?xml version="1.0" encoding="utf-8"?> <!-- $Revision$ --> -<!-- EN-Revision: 330340 Maintainer: daijie Status: ready --> +<!-- EN-Revision: 342538 Maintainer: daijie Status: ready --> <!-- Reviewed: no --> @@ -12,8 +12,10 @@ <preface xml:id="intro.openssl"> &reftitle.intro; <para> - 本模块使用 <link - xlink:href="&url.openssl;">OpenSSL</link> 函数来产生、验证签名,以及密封(加密)启封(解密)数据。 + 本扩展使用 <link + xlink:href="&url.openssl;">OpenSSL</link> 库来对称/非对称加解密,以及 PBKDF2、 PKCS7、 PKCS12、 X509 和其他加密操作。除此之外还提供了 TLS 流的实现。 + </para> + <para> OpenSSL 还提供了更多本模块暂不支持的功能,某些功能以后可能会增加。 </para> </preface> Modified: phpdoc/zh/trunk/reference/openssl/functions/openssl-encrypt.xml =================================================================== --- phpdoc/zh/trunk/reference/openssl/functions/openssl-encrypt.xml 2017-10-05 09:43:56 UTC (rev 343194) +++ phpdoc/zh/trunk/reference/openssl/functions/openssl-encrypt.xml 2017-10-05 09:51:16 UTC (rev 343195) @@ -1,6 +1,6 @@ <?xml version="1.0" encoding="utf-8"?> <!-- $Revision$ --> -<!-- EN-Revision: 342054 Maintainer: daijie Status: ready --> +<!-- EN-Revision: 343016 Maintainer: daijie Status: ready --> <!-- Reviewed: no --> <refentry xml:id="function.openssl-encrypt" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink"> @@ -35,7 +35,7 @@ <term><parameter>data</parameter></term> <listitem> <para> - 数据。 + 待加密的明文信息数据。 </para> </listitem> </varlistentry> @@ -150,6 +150,66 @@ </informaltable> </refsect1> + + <refsect1 role="examples"> + &reftitle.examples; + <para> + <example> + <title>PHP 7.1+ 下 GCM 模式的 AES 认证加密例子</title> + <programlisting role="php"> +<![CDATA[ +<?php +//$key should have been previously generated in a cryptographically safe way, like openssl_random_pseudo_bytes +$plaintext = "message to be encrypted"; +$cipher = "aes-128-gcm"; +if (in_array($cipher, openssl_get_cipher_methods())) +{ + $ivlen = openssl_cipher_iv_length($cipher); + $iv = openssl_random_pseudo_bytes($ivlen); + $ciphertext = openssl_encrypt($plaintext, $cipher, $key, $options=0, $iv, $tag); + //store $cipher, $iv, and $tag for decryption later + $original_plaintext = openssl_decrypt($ciphertext, $cipher, $key, $options=0, $iv, $tag); + echo $original_plaintext."\n"; +} +?> +]]> + </programlisting> + </example> + + <example> + <title>PHP 5.6+ 的 AES 认证加密例子</title> + <programlisting role="php"> +<![CDATA[ +<?php +//$key previously generated safely, ie: openssl_random_pseudo_bytes +$plaintext = "message to be encrypted"; +$ivlen = openssl_cipher_iv_length($cipher="AES-128-CBC"); +$iv = openssl_random_pseudo_bytes($ivlen); +$ciphertext_raw = openssl_encrypt($plaintext, $cipher, $key, $options=OPENSSL_RAW_DATA, $iv); +$hmac = hash_hmac('sha256', $ciphertext_raw, $key, $as_binary=true); +$ciphertext = base64_encode( $iv.$hmac.$ciphertext_raw ); + +//decrypt later.... +$c = base64_decode($ciphertext); +$ivlen = openssl_cipher_iv_length($cipher="AES-128-CBC"); +$iv = substr($c, 0, $ivlen); +$hmac = substr($c, $ivlen, $sha2len=32); +$ciphertext_raw = substr($c, $ivlen+$sha2len); +$original_plaintext = openssl_decrypt($ciphertext_raw, $cipher, $key, $options=OPENSSL_RAW_DATA, $iv); +$calcmac = hash_hmac('sha256', $ciphertext_raw, $key, $as_binary=true); +if (hash_equals($hmac, $calcmac))//PHP 5.6+ timing attack safe comparison +{ + echo $original_plaintext."\n"; +} +?> +]]> + </programlisting> + </example> + + </para> + </refsect1> + + <refsect1 role="seealso"> &reftitle.seealso; <para>

« previous php.doc.zh (#5022) next »