php的AES加密、解密类

<?php
/**
 * php、ios、Android 通用的AES加密、解密方法
 */
namespace Common\Business;


class AESCrypt
{
    /**
     * 初始矢量
     * @var string
     */
    # converted JAVA byte code in to HEX and placed it here
    //private $hex_iv = '00000000000000000000000000000000';
    private $key = '';

    function __construct($key) {
        $this->key = $key;
        $this->key = hash('sha256', $this->key, true);
    }

    /**
     * 设置密钥
     * @param $new_key
     */
    function set_key($new_key)
    {
        $this->key = $new_key;
    }

    /**
     * 加密
     * @param $str
     * @return string
     */
    function encrypt($str) {
        $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
        mcrypt_generic_init($td, $this->key, $this->hexToStr($this->hex_iv));
        $block = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
        $pad = $block - (strlen($str) % $block);
        $str .= str_repeat(chr($pad), $pad);
        $encrypted = mcrypt_generic($td, $str);
        mcrypt_generic_deinit($td);
        mcrypt_module_close($td);
        return base64_encode($encrypted);
    }

    /**
     * 解密
     * @param $code
     * @return bool|string
     */
    function decrypt($code) {
        $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
        mcrypt_generic_init($td, $this->key, $this->hexToStr($this->hex_iv));
        $str = mdecrypt_generic($td, base64_decode($code));
        $block = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
        mcrypt_generic_deinit($td);
        mcrypt_module_close($td);
        return $this->strippadding($str);
    }

    /*
      For PKCS7 padding
     */
    private function addpadding($string, $blocksize = 16) {
        $len = strlen($string);
        $pad = $blocksize - ($len % $blocksize);
        $string .= str_repeat(chr($pad), $pad);
        return $string;
    }

    private function strippadding($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;
        }
    }

    function hexToStr($hex)
    {
        $string='';
        for ($i=0; $i < strlen($hex)-1; $i+=2)
        {
            $string .= chr(hexdec($hex[$i].$hex[$i+1]));
        }
        return $string;
    }
}

 
?>

  

 

 

/**
 * AES
 */
class AES
{

    /**
     * 注:不要修改这几个值,有不同需求请以参数形式传入!
     */
    const METHOD = 'AES-128-CBC';
    const KEY = 'zbtcyuc5msxidyam';
    const IV = 'bthuiwldicoayzbt';

    /**
     * 加密
     *
     * @param string $data
     * @param string $method
     * @param string $key
     * @param int $options
     * @param string $iv
     * @return string
     */
    public static function encrypt($data = '', $method = self::METHOD, $key = self::KEY, $options = 0, $iv = self::IV)
    {
        $encrypted = openssl_encrypt($data, $method, $key, $options, $iv);
        return $encrypted;
    }

    /**
     * 解密
     *
     * @param string $data
     * @param string $method
     * @param string $key
     * @param int $options
     * @param string $iv
     * @return string
     */
    public static function decrypt($data = '', $method = self::METHOD, $key = self::KEY, $options = 0, $iv = self::IV)
    {
        $decrypted = openssl_decrypt($data, $method, $key, $options, $iv);
        return $decrypted;
    }

}

  RSA2加、解密

//加密
$signContent = $this->getSignContent($params);
                openssl_private_encrypt($signContent, $encrypted, $this->getPrivateKey());//私钥加密
                $sign = base64_encode($encrypted);  //base64只是为了避免特殊字符


//解密
$signContent = $this->getSignContent($params);
                $publicKey = openssl_get_publickey($this->getPublicKey()); //转换为openssl格式密钥
                $result = (bool)openssl_verify($params, base64_decode($sign), $publicKey, OPENSSL_ALGO_SHA256); //调用openssl内置方法验签,返回bool值

  

posted on 2017-07-20 10:19  diguaer  阅读(373)  评论(0编辑  收藏  举报

导航