1. 程式人生 > >php EAS CBC 128 256加密(16進位制/base64)

php EAS CBC 128 256加密(16進位制/base64)

技術QQ交流群:294088839

<?php
namespace Api\Controller;
use Think\Controller;


/**
 * Created by PhpStorm.
 * User: Json
 * Date: 2018/7/19
 * Time: 18:15
 */
class EasController extends Controller {
    const KEY="";
    const IV ="";
    /**
     * pkcs7補碼
     * @param string $string  明文
     * @param int $blocksize Blocksize , 以 byte 為單位
     * @return String
     */
    private function addPkcs7Padding($string, $blocksize = 32) {
        $len = strlen($string); //取得字串長度
        $pad = $blocksize - ($len % $blocksize); //取得補碼的長度
        $string .= str_repeat(chr($pad), $pad); //用ASCII碼為補碼長度的字元, 補足最後一段
        return $string;
    }

    /**
     * 加密然後base64轉碼
     *
     * @param String 明文
     * @param 加密的初始向量(IV的長度必須和Blocksize一樣, 且加密和解密一定要用相同的IV)
     * @param $key 金鑰
     */
    function aes256cbcEncrypt($str, $iv, $key ) {
        return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $this->addPkcs7Padding($str) , MCRYPT_MODE_CBC, $iv));
    }

    /**
     * 除去pkcs7 padding
     *
     * @param String 解密後的結果
     *
     * @return String
     */
    private function stripPkcs7Padding($string){
        $slast = ord(substr($string, -1));
        $slastc = chr($slast);
        $pcheck = substr($string, -$slast);

        if(preg_match("/$slastc{".$slast."}/", $string)){
            $string = substr($string, 0, strlen($string)-$slast);
            return $string;
        } else {
            return false;
        }
    }
    /**
     * 解密
     *
     * @param String $encryptedText 二進位制的密文
     * @param String $iv 加密時候的IV
     * @param String $key 金鑰
     * @return String
     */
    function aes256cbcDecrypt($encryptedText, $iv, $key) {
        $encryptedText =base64_decode($encryptedText);

        return $this->stripPkcs7Padding(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $encryptedText, MCRYPT_MODE_CBC, $iv));
    }

    function aes128cbcDecrypt($encryptedText, $iv=self::IV, $key=self::KEY) {
        $encryptedText =base64_decode($encryptedText);
        return $this->stripPkcs7Padding(mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $encryptedText, MCRYPT_MODE_CBC, $iv));
    }

    function hexToStr($hex)//十六進位制轉字串
    {
        $string="";
        for($i=0;$i<strlen($hex)-1;$i+=2)
            $string.=chr(hexdec($hex[$i].$hex[$i+1]));
        return  $string;
    }
    function strToHex($string)//字串轉十六進位制
    {
        $hex="";
        $tmp="";
        for($i=0;$i<strlen($string);$i++)
        {
            $tmp = dechex(ord($string[$i]));
            $hex.= strlen($tmp) == 1 ? "0".$tmp : $tmp;
        }
        $hex=strtoupper($hex);
        return $hex;
    }
    function aes128cbcHexDecrypt($encryptedText, $iv=self::IV, $key=self::KEY) {
        $str = $this->hexToStr($encryptedText);
        return $this->stripPkcs7Padding(mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $str, MCRYPT_MODE_CBC, $iv));
    }

    function aes128cbcEncrypt($str, $iv=self::IV, $key=self::KEY ) {    // $this->addPkcs7Padding($str,16)
        $base = (mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key,$this->addPkcs7Padding($str,16) , MCRYPT_MODE_CBC, $iv));
        return $this->strToHex($base);
    }
    function aes129cbcEncrypt($str, $iv=self::IV, $key=self::KEY ) {    // $this->addPkcs7Padding($str,16)
        $base = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key,$this->addPkcs7Padding($str,16) , MCRYPT_MODE_CBC, $iv));
        return $base;
    }

}