美文网首页
PHP与Java接口通讯使用AES对称加密

PHP与Java接口通讯使用AES对称加密

作者: baiping_hb | 来源:发表于2017-07-27 11:27 被阅读0次
    private $key = '';
    private $vi = '';

    public function encrypt($encryptStr) {
        $localIV = $this->vi;
        $encryptKey = $this->key;
        //Open module
        $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, $localIV);
        //print "module = $module <br/>" ;
        mcrypt_generic_init($module, $encryptKey, $localIV);
        //Padding
        $block = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
        $pad = $block - (strlen($encryptStr) % $block); //Compute how many characters need to pad
        $encryptStr .= str_repeat(chr($pad), $pad);
        // After pad, the str length must be equal to block or its integer multiples
        //encrypt
        $encrypted = mcrypt_generic($module, $encryptStr);
        //Close
        mcrypt_generic_deinit($module);
        mcrypt_module_close($module);
        //return base64_encode($encrypted);
        return bin2hex($encrypted);
    }

    /**
     * 解密
     * @param $encryptStr
     * @return string
     * @author Baip 125618036@qq.com
     */
    public function decrypt($encryptStr) {
        $localIV = $this->vi;
        $encryptKey = $this->key;
        //Open module
        $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, $localIV);
        //print "module = $module <br/>" ;
        mcrypt_generic_init($module, $encryptKey, $localIV);
        //$encryptedData = base64_decode($encryptStr);
        $encryptedData = hex2bin($encryptStr);
        $encryptedData = mdecrypt_generic($module, $encryptedData);
        return $encryptedData;
    }

相关文章

网友评论

      本文标题:PHP与Java接口通讯使用AES对称加密

      本文链接:https://www.haomeiwen.com/subject/ibhzkxtx.html