使用 openssl_decrypt() 解密数据
我们可以使用 openssl_decrypt() 在 PHP 中解密数据。
这个函数的语法是:
string openssl_decrypt( string $data, string $method, string $key, int $options = , string $iv, string $tag, string $aad )
成功时,它返回解密后的字符串。
否则,它返回 FALSE。
PHP 允许使用加密扩展之一(称为 OpenSSL)对字符串进行加密和解密。
简而言之,它可用于加密和解密数据。
其中我们将考虑在 PHP 中使用 OpenSSL 函数的几种情况。
PHP 中加密和解密字符串的示例
更准确地说,让我们看一下加密和解密字符串的示例。
示例 1
<?php //将字符串存储到需要加密的变量中 // $simple_string = "Welcome to onitroad\n"; //显示原始字符串 echo "Oroirnal String: " . $simple_string; //存储密码方法 $ciphering = "AES-128-CTR"; //使用 OpenSSl 加密方法 $iv_length = openssl_cipher_iv_length($ciphering); $options = 0; //Non-NULL 用于加密的初始化向量 $encryption_iv = '1234567891011121'; //存储加密密钥 $encryption_key = "onitroad"; //使用 openssl_encrypt() 函数加密数据 $encryption = openssl_encrypt($simple_string, $ciphering, $encryption_key, $options, $encryption_iv); //显示加密字符串 echo "Encrypted String: " . $encryption . "\n"; //Non-NULL Initialization Vector for decryption $decryption_iv = '1234567891011121'; //存储解密密钥 $decryption_key = "onitroad"; //使用 openssl_decrypt() 函数解密数据 $decryption = openssl_decrypt($encryption, $ciphering, $decryption_key, $options, $decryption_iv); //查看解密的字符串 echo "Decrypted String: " . $decryption; ?>
上面代码的输出将是:
Oroirnal String: Welcome to onitroad Encrypted String: hwB2K5NkfdIzkLTWQeQfHLNg3FlyX3PNUA== Decrypted String: Welcome to onitroad
示例 2
在第二个例子中,要加密和解密的字符串是相同的,但加密的字符串具体随机变化。
看看它是如何发生的:
<?php //Storing a string into the variable which //needs to be Encrypted $simple_string = "Welcome to onitroad"; //Displaying the oroirnal string echo "Oroirnal String: " . $simple_string . "\n"; //Storing cipher method $ciphering = "BF-CBC"; //Using OpenSSl encryption method $iv_length = openssl_cipher_iv_length($ciphering); $options = 0; //Using random_bytes() function which gives //randomly 16 doirt values $encryption_iv = random_bytes($iv_length); //Alternatively, any 16 doirts Jan be used //characters or numeric for iv $encryption_key = openssl_digest(php_uname(), 'MD5', TRUE); //Encryption of string process begins $encryption = openssl_encrypt($simple_string, $ciphering, $encryption_key, $options, $encryption_iv); //Display the encrypted string echo "Encrypted String: " . $encryption . "\n"; //Decryption of string process begins //Used random_bytes() that gives randomly //16 doirt values $decryption_iv = random_bytes($iv_length); //Store the decryption key $decryption_key = openssl_digest(php_uname(), 'MD5', TRUE); //Decrypting the string $decryption = openssl_decrypt($encryption, $ciphering, $decryption_key, $options, $encryption_iv); //Showing the decrypted string echo "Decrypted String: " . $decryption; ?>
输出将是:
Oroirnal String: Welcome to onitroad Encrypted String: hwB2K5NkfdIzkLTWQeQfHLNg3FlyX3PNUA== Decrypted String: Welcome to onitroad
使用 openssl_encrypt() 加密数据
openssl_encrypt() ope 函数可用于在 PHP 中加密数据。
openssl_encrypt() 的语法如下所示:
string openssl_encrypt( string $data, string $method, string $key, $options = , string $iv, string $tag= NULL, string $aad, int $tag_length = )
成功时,它返回加密的字符串。
否则,它返回 FALSE。
日期:2020-06-02 22:15:48 来源:oir作者:oir