laravel框架的AES加密解密

laravel框架:

1.在App\Library下新建一个函数库,我起名Functions.php

pkcs5_pad($input, $size);
        $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
        $iv = '0102030405060708';
        mcrypt_generic_init($td, $key, $iv);
        $data = mcrypt_generic($td, $input);
        mcrypt_generic_deinit($td);
        mcrypt_module_close($td);
        $data = base64_encode($data);
        return $data;
    }
    
    
    //填充
    public function pkcs5_pad ($text, $blocksize) {
        $pad = $blocksize - (strlen($text) % $blocksize);
        return $text . str_repeat(chr($pad), $pad);
    }
    
    
    //解密
    public static function decrypt_pass($sStr, $sKey) {
        $iv = '0102030405060708';
        $decrypted= mcrypt_decrypt(
            MCRYPT_RIJNDAEL_128,
            $sKey,
            base64_decode($sStr),
            MCRYPT_MODE_CBC,
            $iv
        );
        $dec_s = strlen($decrypted);
        $padding = ord($decrypted[$dec_s-1]);
        $decrypted = substr($decrypted, 0, -$padding);
        return $decrypted;
    }
}

2.在你需要调用的类中 use App\Library\Functions;

3.在需要调用的方法中

$encryptKey = '12f862d21d3ceafba1b88e5f22960d55';   //这里我选择的32位

4.加密  

Functions::encrypt_pass($input['phone'],$encryptKey);  //$input['phone'],需要加密的参数,$encryptKey 加密用的key


5,解密

Functions::decrypt_pass($input['phone'],$encryptKey);    //$input['phone'],需要加密的参数,$encryptKey 加密用的key

你可能感兴趣的:(php)