中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何使用PHP實現AES加密、解密

發布時間:2020-07-21 09:09:51 來源:億速云 閱讀:444 作者:Leah 欄目:編程語言

這期內容當中小編將會給大家帶來有關如何使用PHP實現AES加密、解密,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

1、mcrypt_encrypt AES加密,解密

class Lib_desEnctyp
{
    private $key = "";
    private $iv = "";

    /**
    * 構造,傳遞二個已經進行base64_encode的KEY與IV
    *
    * @param string $key
    * @param string $iv
    */
    function __construct ($key, $iv)
    {
        if (empty($key) || empty($iv)) {
            echo 'key and iv is not valid';
            exit();
        }
        $this->key = $key;
        $this->iv = $iv;
    }

    /**
    *加密
    * @param <type> $value
    * @return <type>
    */
    public function encrypt ($value)
    {
        $td = mcrypt_module_open(MCRYPT_3DES, '', MCRYPT_MODE_CBC, '');
        $iv = base64_decode($this->iv);
        $value = $this->PaddingPKCS7($value);
        $key = base64_decode($this->key);
        mcrypt_generic_init($td, $key, $iv);
        $ret = base64_encode(mcrypt_generic($td, $value));
        mcrypt_generic_deinit($td);
        mcrypt_module_close($td);
        return $ret;
    }

    /**
    *解密
    * @param <type> $value
    * @return <type>
    */
    public function decrypt ($value)
    {
        $td = mcrypt_module_open(MCRYPT_3DES, '', MCRYPT_MODE_CBC, '');
        $iv = base64_decode($this->iv);
        $key = base64_decode($this->key);
        mcrypt_generic_init($td, $key, $iv);
        $ret = trim(mdecrypt_generic($td, base64_decode($value)));
        $ret = $this->UnPaddingPKCS7($ret);
        mcrypt_generic_deinit($td);
        mcrypt_module_close($td);
        return $ret;
    }

    private function PaddingPKCS7 ($data)
    {
        $block_size = mcrypt_get_block_size('tripledes', 'cbc');
        $padding_char = $block_size - (strlen($data) % $block_size);
        $data .= str_repeat(chr($padding_char), $padding_char);
        return $data;
    }

    private function UnPaddingPKCS7($text)
    {
        $pad = ord($text{strlen($text) - 1});
        if ($pad > strlen($text)) {
            return false;
        }
        if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) {
            return false;
        }
        return substr($text, 0, - 1 * $pad);
    }
}

2、openssl 加密,解密 [方式1]

/**
 * DES加密類
 * User: gaowei
 * Date: 2017/12/12
 * Time: 19:23
 */
class DesEncrypt {
    private $key = "";
    private $iv = "";

    /**
     * 構造,傳遞二個已經進行base64_encode的KEY與IV
     *
     * @param string $key
     * @param string $iv
     */
    function __construct ($key, $iv)
    {
        if (empty($key) || empty($iv)) {
            echo 'key and iv is not valid';
            exit();
        }
        $this->key = $key;
        $this->iv = $iv;//8
        //$this->iv = $iv.'00000000000';//16

    }

    /**
     * @title 加密
     * @author gaowei
     * @date 2017/12/18
     * @param string $value 要傳的參數
     * @ //OPENSSL_RAW_DATA|OPENSSL_ZERO_PADDING //AES-128-ECB|AES-256-CBC|BF-CBC
     * @return json
     * */
    public function encrypt ($value) {

        //參考地址:https://stackoverflow.com/questions/41181905/php-mcrypt-encrypt-to-openssl-encrypt-and-openssl-zero-padding-problems#
        $value = $this->PaddingPKCS7($value);
        $key = base64_decode($this->key);
        $iv  = base64_decode($this->iv);
        //AES-128-ECB|不能用 AES-256-CBC|16 AES-128-CBC|16 BF-CBC|8 aes-128-gcm|需要加$tag  DES-EDE3-CBC|8
        $cipher = "DES-EDE3-CBC";
        if (in_array($cipher, openssl_get_cipher_methods())) {
            //$ivlen = openssl_cipher_iv_length($cipher);
           // $iv = openssl_random_pseudo_bytes($ivlen);
            $result = openssl_encrypt($value, $cipher, $key, OPENSSL_SSLV23_PADDING, $iv);
            //$result = base64_encode($result); //為3的時間要用
            //store $cipher, $iv, and $tag for decryption later
           /* $original_plaintext = openssl_decrypt($result, $cipher, $key, OPENSSL_SSLV23_PADDING, $iv);
            echo $original_plaintext."\n";*/
        }
        return $result;

    }
    /**
     * @title 解密
     * @author gaowei
     * @date 2017/12/18
     * @param string $value 要傳的參數
     * @return json
     * */
    public function decrypt ($value) {
        $key       = base64_decode($this->key);
        $iv        = base64_decode($this->iv);
        $decrypted = openssl_decrypt($value, 'DES-EDE3-CBC', $key, OPENSSL_SSLV23_PADDING, $iv);
        $ret = $this->UnPaddingPKCS7($decrypted);
        return $ret;
    }

    private function PaddingPKCS7 ($data) {
        //$block_size = mcrypt_get_block_size('tripledes', 'cbc');//獲取長度
        //$block_size = openssl_cipher_iv_length('tripledes', 'cbc');//獲取長度
        $block_size = 8;
        $padding_char = $block_size - (strlen($data) % $block_size);
        $data .= str_repeat(chr($padding_char), $padding_char);
        return $data;
    }
    private function UnPaddingPKCS7($text) {
        $pad = ord($text{strlen($text) - 1});
        if ($pad > strlen($text)) {
            return false;
        }
        if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) {
            return false;
        }
        return substr($text, 0, - 1 * $pad);
    }
}

3、openssl 加密,解密 [方式2]

/**
 * @desc:php aes加密解密類
 * @author gl
 * @date 2019/08/31
 */
class CI_Aes{
    /**
     * CI_Aes cipher
     * @var string
     */
    protected $cipher = 'aes-128-ecb';
    /**
     * CI_Aes key
     *
     * @var string
     */
    protected $key;
    /**
     * CI_Aes constructor
     * @param string $key Configuration parameter
     */

    public function __construct($key=null){
        $this->key = $key;
    }

    /**
     * Initialize
     *
     * @param array $params Configuration parameters
     * @return CI_Encryption
     */
    public function initialize($params)
    {
        if (!empty($params) && is_array($params)) {
            foreach ($params as $key => $val) {
                $this->$key = $val;
            }
        }
    }
    /**
     * Encrypt
     *
     * @param string $data Input data
     * @return string
     */
    public function encrypt($data) {
        $endata =  openssl_encrypt($data, $this->cipher, $this->key, OPENSSL_RAW_DATA);
        return  bin2hex($endata);
    }

    /**
     * Decrypt
     *
     * @param string $data Encrypted data
     * @return string
     */
    public function decrypt($data) {
        $encrypted = hex2bin($data);
        return openssl_decrypt($encrypted, $this->cipher, $this->key, OPENSSL_RAW_DATA);
    }

}

4、其他 加密,解密

//加密函數
function lock_url($txt,$key='www.jb51.net')
{
  $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
  $nh = rand(0,64);
  $ch = $chars[$nh];
  $mdKey = md5($key.$ch);
  $mdKey = substr($mdKey,$nh%8, $nh%8+7);
  $txt = base64_encode($txt);
  $tmp = '';
  $i=0;$j=0;$k = 0;
  for ($i=0; $i<strlen($txt); $i++) {
    $k = $k == strlen($mdKey) ? 0 : $k;
    $j = ($nh+strpos($chars,$txt[$i])+ord($mdKey[$k++]))%64;
    $tmp .= $chars[$j];
  }
  return urlencode($ch.$tmp);
}
//解密函數
function unlock_url($txt,$key='www.jb51.net')
{
  $txt = urldecode($txt);
  $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
  $ch = $txt[0];
  $nh = strpos($chars,$ch);
  $mdKey = md5($key.$ch);
  $mdKey = substr($mdKey,$nh%8, $nh%8+7);
  $txt = substr($txt,1);
  $tmp = '';
  $i=0;$j=0; $k = 0;
  for ($i=0; $i<strlen($txt); $i++) {
    $k = $k == strlen($mdKey) ? 0 : $k;
    $j = strpos($chars,$txt[$i])-$nh - ord($mdKey[$k++]);
    while ($j<0) $j+=64;
    $tmp .= $chars[$j];
  }
  return base64_decode($tmp);
}

上述就是小編為大家分享的如何使用PHP實現AES加密、解密了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

依安县| 永善县| 望江县| 沐川县| 太原市| 景德镇市| 江西省| 福清市| 上林县| 余姚市| 阿城市| 萨嘎县| 潢川县| 合阳县| 怀远县| 天等县| 天津市| 衢州市| 巩义市| 麟游县| 娱乐| 汤原县| 禹州市| 旬邑县| 民乐县| 介休市| 右玉县| 哈尔滨市| 辉县市| 辰溪县| 开封市| 克东县| 托克托县| 罗城| 大姚县| 阳新县| 民县| 宽城| 金溪县| 五峰| 高青县|