openssl aes padding命令解密_php7.* 加密解密 openssl_encrypt 替代mcrypt

1 .概况

php7.1发布后新特性吸引了不少PHPer,大家都在讨论新特性带来的好处与便利。但是从php7.0 升级到 php7.1 废弃了一个在过去普遍应用的扩展(mcrypt扩展)。官方提供了相应的解决提示,却没有提供更详细的解决办法。于是坑来了….

2 .解决

php手册目前缺少“ openssl_encrypt ”和“ openssl_decrypt ”功能的文档,所以花了我一段时间来完成我需要做的工作,以使这些功能可以替代mcrypt

2.1 首先,您将需要生成一个用作256位加密密钥的伪随机字节串,请求的长度将为32(32位= 256位)。
$encryption_key_256bit = base64_encode(openssl_random_pseudo_bytes(32));
2.2 现在我们有了关键,我们将创建加密功能。我们将把要编码的数据和我们的密钥传递给该函数。除了我们的密钥,还有一个二次随机字符串,我们将创建和使用称为 初始化向量 (IV),有助于加强加密。
function my_encrypt($data, $key) {    // Remove the base64 encoding from our key    $encryption_key = base64_decode($key);    // Generate an initialization vector    $iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cbc'));    // Encrypt the data using AES 256 encryption in CBC mode using our encryption key and initialization vector.    $encrypted = openssl_encrypt($data, 'aes-256-cbc', $encryption_key, 0, $iv);    // The $iv is just as important as the key for decrypting, so save it with our encrypted data using a unique separator (::)    return base64_encode($encrypted . '::' . $iv);}
2.3 现在为解密功能:
function my_decrypt($data, $key) {      // Remove the base64 encoding from our key      $encryption_key = base64_decode($key);      // To decrypt, split the encrypted data from our IV - our unique separator used was "::"      list($encrypted_data, $iv) = explode('::', base64_decode($data), 2);      return openssl_decrypt($encrypted_data, 'aes-256-cbc', $encryption_key, 0, $iv);}

2.4 放在一起测试

//$key is our base64 encoded 256bit key that we created earlier. You will probably store and define this     key in a config file.$key = 'bRuD5WYw5wd0rdHR9yLlM6wt2vteuiniQBqE70nAuhU=';function my_encrypt($data, $key) {    // Remove the base64 encoding from our key    $encryption_key = base64_decode($key);    // Generate an initialization vector    $iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cbc'));    // Encrypt the data using AES 256 encryption in CBC mode using our encryption key and initialization vector.    $encrypted = openssl_encrypt($data, 'aes-256-cbc', $encryption_key, 0, $iv);    // The $iv is just as important as the key for decrypting, so save it with our encrypted data using a unique separator (::)    return base64_encode($encrypted . '::' . $iv);}function my_decrypt($data, $key) {    // Remove the base64 encoding from our key    $encryption_key = base64_decode($key);    // To decrypt, split the encrypted data from our IV - our unique separator used was "::"    list($encrypted_data, $iv) = explode('::', base64_decode($data), 2);    return openssl_decrypt($encrypted_data, 'aes-256-cbc', $encryption_key, 0, $iv);}//our data to be encoded$password_plain = 'abc123';echo $password_plain . "
";//our data being encrypted. This encrypted data will probably be going into a database//since it's base64 encoded, it can go straight into a varchar or text database field without corruption worry$password_encrypted = my_encrypt($password_plain, $key);echo $password_encrypted . "
";//now we turn our encrypted data back to plain text$password_decrypted = my_decrypt($password_encrypted, $key);echo $password_decrypted . "
";
2.5 上面的代码将输出以下内容。请注意,由于我们的初始化向量,每次运行代码时,中间的加密字符串将会更改:
abc123K3gzWkxySUd6VkgvQTNJUUtZMjV2UT09Ojpia3sh1zglO3DYodw84855abc123

我是这样使用的:

40c58da6915e32e92d46ea8da1179012.png
9425f071a13e2c69de4ba943632c871a.png
974a2e899cb55db5b5826e3dd1a00d07.png
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我理解了你的问题。关于 PHP 中如何使用 AES/CBC/PKCS7Padding 进行加密/解密,可以使用 PHP OpenSSL 库来实现。以下是一个简单的示例代码: ```php <?php /** * AES加密 * @param string $data 要加密的数据 * @param string $key 密钥 * @param string $iv 初始化向量 * @return string */ function aes_encrypt($data, $key, $iv) { // PKCS7Padding 填充方式 $block_size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC); $padding = $block_size - (strlen($data) % $block_size); $data .= str_repeat(chr($padding), $padding); // 加密 $cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, ''); mcrypt_generic_init($cipher, $key, $iv); $encrypted = mcrypt_generic($cipher, $data); mcrypt_generic_deinit($cipher); mcrypt_module_close($cipher); return base64_encode($encrypted); } /** * AES解密 * @param string $data 要解密的数据 * @param string $key 密钥 * @param string $iv 初始化向量 * @return string */ function aes_decrypt($data, $key, $iv) { // 解密 $cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, ''); mcrypt_generic_init($cipher, $key, $iv); $decrypted = mdecrypt_generic($cipher, base64_decode($data)); mcrypt_generic_deinit($cipher); mcrypt_module_close($cipher); // 去掉 PKCS7Padding 填充 $padding = ord($decrypted[strlen($decrypted)-1]); return substr($decrypted, 0, -$padding); } // 测试 $key = '1234567890123456'; // 密钥必须为16、24或32个字符 $iv = '1234567890123456'; // 初始化向量必须为16个字符 $data = 'Hello, world!'; $cipher_text = aes_encrypt($data, $key, $iv); $plain_text = aes_decrypt($cipher_text, $key, $iv); echo '明文: ' . $data . "\n"; echo '密文: ' . $cipher_text . "\n"; echo '解密后: ' . $plain_text . "\n"; ``` 需要注意几点: 1. 密钥必须为 16、24 或 32 个字符,初始化向量必须为 16 个字符,可以根据需要修改。 2. 在加密时需要进行 PKCS7Padding 填充,解密后需要去除填充。 3. 需要安装 PHP OpenSSL 扩展库,才能使用 mcrypt 函数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值