51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

PHP7 AES加密解密函数_兼容ios/andriod/java对等加解密

PHP7.0 7.1 7.2 7.3 AES对等加解密类 函数文件_兼容ios/andriod/java等

由于新项目规划要求使用PHP7.2开发环境,但在部分新系统中仍需使用AES加解密方式交互调取早期系统数据,由于早期AES使用的是mcrypt_encrypt函数,但该函数在PHP新版本中已逐渐废弃,PHP新版本中使用openssl_encrypt取代mcrypt_encrypt。

于是重新基于PHP7编写一个新类,由于需兼容现有早期系统的AES加解密类(并存填充与无填充),所以该类中增添部分判断。在规划项目、技术及开发时,未来往事推荐你尽可能规范化、标准化、统一化,以尽可能规避持续的维护投入,提升有效开发时间。

AES对等加密解密类文件:CBC模式 - PHP

<?php
/**
 * +---------------------------------------------------------+
 * | @Package: cloud
 * | @Version: V1.0
 * | @Purpose: AES PKCS7Encoder加密解密类文件
 * | @PHP Platform support: PHP5.6~7.2(Recommended use new version)
 * +---------------------------------------------------------+
 * | @Authors: rinald <136654168@qq.com / https://www.fity.cn>
 * | @Create Time: 2019/07/08 08:56
 */

namespace Helper;

//加解密类 class AesCrypt { protected $cipher = 'AES-128-CBC'; protected $key = ''; protected $iv = ''; protected $option = OPENSSL_ZERO_PADDING;

function __construct($key, $iv){
    $this-&gt;key = substr(md5($key), 16, 32);
    $this-&gt;iv = substr(md5($iv), 16, 32);
}

/**

  • 对明文进行加密
  • @param string $text 需要加密的明文
  • @return string 加密后的密文 */ public function encrypt($text) { $text = json_encode($text); try { //获得16位随机字符串,填充到明文之前 $random = $this-&gt;getRandomStr();//&quot;5rpN1fWcyk9LFTTN&quot;; $text = $random . $text; $pkc_encoder =new PKCS7Encoder(); $text = $pkc_encoder-&gt;encode($text); $encrypt_data = openssl_encrypt($text, $this-&gt;cipher, $this-&gt;key, $this-&gt;option, $this-&gt;iv); return [StatusCode::$OK, $encrypt_data]; }catch (\Exception $e) { return [StatusCode::$EncryptAESError, null]; }

}

/**

  • 对密文进行解密
  • @param string $encrypted 需要解密的密文
  • @return string 解密得到的明文 */ public function decrypt($encrypted) { try { $decrypted = openssl_decrypt($encrypted, $this-&gt;cipher, $this-&gt;key, $this-&gt;option, $this-&gt;iv); }catch (\Exception $e) { return [StatusCode::$DecryptAESError, null]; } try { //去除补位字符 $pkc_encoder =new PKCS7Encoder(); $result =$pkc_encoder-&gt;decode($decrypted); //去除16位随机字符串 if (strlen($result) &lt; 16){ return [StatusCode::$DecryptAESPKCSError, null]; } $content = substr($result, 16, strlen($result)); $decrypt_data = json_decode($content, true); }catch (\Exception $e) { return [StatusCode::$DecryptAESPKCSError, null]; } return [StatusCode::$OK, $decrypt_data]; }

/**

  • 获得16位随机字符串,填充到明文之前
  • @return string */ function getRandomStr() { $str = &quot;&quot;; $str_pol = &quot;ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz&quot;; $max = strlen($str_pol) - 1; for ($i = 0; $i &lt; 16; $i++) { $str .= $str_pol[mt_rand(0, $max)]; } return $str; }

}

//状态码 class StatusCode { public static $OK = 0;

public static $EncryptAESError = 9001; //加密失败
public static $DecryptAESError = 9002; //解密失败
public static $DecryptAESPKCSError = 9003; //移除填充补位失败

}

//AES加密解密填充补位类 class PKCS7Encoder { public static $block_size = 32; /** * 对需要加密的明文进行填充补位 * @param $text 需要进行填充补位操作的明文 * @return 补齐明文字符串 */ function encode($text) { $text_length = strlen($text); //计算需要填充的位数 $amount_to_pad = PKCS7Encoder::$block_size - ($text_length % PKCS7Encoder::$block_size); if ($amount_to_pad == 0) { $amount_to_pad = PKCS7Encoder::block_size; } //获得补位所用的字符 $pad_chr = chr($amount_to_pad); $tmp = ""; for ($index = 0; $index < $amount_to_pad; $index++) { $tmp .= $pad_chr; } return $text . $tmp; }

/**
 * 对解密后的明文进行补位删除
 * @param decrypted 解密后的明文
 * @return 删除填充补位后的明文
 */
function decode($text)
{
    $pad = ord(substr($text, -1));
    if ($pad &lt; 1 || $pad &gt; PKCS7Encoder::$block_size) {
        $pad = 0;
    }
    return substr($text, 0, (strlen($text) - $pad));
}

}

/**

  • 调用示例:ios/android/java加密值url传递时建议使用urlencode进行处理
  • $key = 'www.fity.cn'; //自行定义 加解密方法会处理截取md5后的16位
  • $iv = 'fitycn'; //自行定义
  • $aesCrypt = new \Helper\AesCrypt($key, $iv);
  • #加密
  • $data = ['id'=>'1', 'srt'=>'欢迎来访未来往事博客https://www.fity.cn', time=>time()];
  • $result = $aesCrypt->encrypt($data); //$data string/array
  • #解密:返回值为string/array
  • if($result[0] == 0){ //加密字符串存在则进行解密
  •  $res = $aesCrypt-&gt;decrypt($result[1]);
    
  • }
  • 注意:IOS、Android在加密时请使用PKCS7Padding模式填充数据补位。 **/
赞(6)
未经允许不得转载:工具盒子 » PHP7 AES加密解密函数_兼容ios/andriod/java对等加解密