云服务器 99 / 年,新老同享(可以99/年续费),开发者力荐特惠渠道,新客户在享受9折
阿里云推广

openssl_decrypt代替mcrypt_decrypt做可逆加密解密代替方案

  • 内容
  • 评论
  • 相关

 mcrypt_encrypt 超过php7.1舍弃了就,php7.1屏蔽错误还可以用。升级php版本就必须使用openssl_decrypt代替之前使用 mcrypt_encrypt 这个函数做的加密解密。下面是自己的老项目升级加密解密的代替方案,仅供参考。

<?php
//php7.1中屏蔽错误 mcrypt_decrypt还能继续使用说明没有移除
error_reporting(0);

//aes的解密方案1
$miwen1 = "F6D6F143A7ACBCF0FCFCF542C638836C4AEA9BC33B9A5CE867A5102E0770F62D8D76EFBE1EF023790D48058EDACF5847D5864A3331FE0F0E4BA1B81B774EB294";
$key1 = 'OKLU520XJB09906H'; //秘钥
$iv1 = $key1;
$decrypted1 = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key1, hex2bin($miwen1), MCRYPT_MODE_CBC, $iv1); //秘钥解密
//处理json
$json_str1 = str_replace("\u0000", "", json_encode($decrypted1));
$arr1 = json_decode(json_decode($json_str1), true);
//获取账号密码
print_r($arr1);

$miwen2 = "F6D6F143A7ACBCF0FCFCF542C638836C4AEA9BC33B9A5CE867A5102E0770F62D8D76EFBE1EF023790D48058EDACF5847D5864A3331FE0F0E4BA1B81B774EB294";
$key2 = "OKLU520XJB09906H";
$iv2 = $key2;
//OpenSSL 解密 或者OPENSSL_NO_PADDING
$decrypted2 = openssl_decrypt(hex2bin($miwen2), "aes-128-cbc", $key2, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv2);
//echo json_encode($de_data);
//有特殊字符再json_encode之后能否发现,然后过滤掉"[{\"T\":\"638107685499086584\",\"P\":\"OT4pQ42TiE6jDPy6\",\"N\":\"wd_wd\"}]\u0000"
$json_str2 = str_replace("\u0000", "", json_encode($decrypted2));
$arr2 = json_decode(json_decode($json_str2), true);
print_r($arr2);


//aes加密方案1 mcrypt_encrypt 超过7.1舍弃了就,php7.1屏蔽错误还可以用
$mingwen3 = "你好";
$key3 = 'OKLU520XJB09906H';
$iv3 = $key3;
$encrypted3 = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key3, $mingwen3, MCRYPT_MODE_CBC, $iv3);
$miwen3 = base64_encode($encrypted3);
echo $miwen3;


//代替方案  openssl_encrypt 方式更通用
$key4 = "OKLU520XJB09906H";
$iv4    = $key4;
$mingwen4 = "你好";
if (strlen($mingwen4) % 16) {
    $mingwen4 = str_pad($mingwen4, strlen($mingwen4) + 16 - strlen($mingwen4) % 16, "\0");
}
$encrypted4 =  openssl_encrypt($mingwen4, 'AES-128-CBC', $key4, OPENSSL_NO_PADDING, $iv4);
$miwen4 = base64_encode($encrypted4);
echo $miwen4;


//aes解密方案2
$miwen33 = "fyy6UkIa/PXyXxR5vm316w=="; //你好
$key33 = 'OKLU520XJB09906H';
$iv33 = $key33;
$plain_str33 = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key33, base64_decode($miwen33), MCRYPT_MODE_CBC, $iv33);
$plain_str33 = str_replace(array("\r\n", "\r", "\n", "\0"), "", $plain_str33);
echo json_encode($plain_str33);


$miwen44 = "fyy6UkIa/PXyXxR5vm316w=="; //你好
$key44 = "OKLU520XJB09906H";
$iv44 = $key44;
//OpenSSL 解密  或者OPENSSL_NO_PADDING
$plain_str44 = openssl_decrypt(base64_decode($miwen44), "aes-128-cbc", $key44, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv44);
$plain_str44 = str_replace(array("\r\n", "\r", "\n", "\0"), "", $plain_str44);
echo json_encode($plain_str44);


本文标签:

版权声明:若无特殊注明,本文皆为《菜鸟站长》原创,转载请保留文章出处。

本文链接:openssl_decrypt代替mcrypt_decrypt做可逆加密解密代替方案 - https://wziyi.com.cn/?post=391

发表评论

电子邮件地址不会被公开。 必填项已用*标注