标准的AES加密网上有很多,注意补位就是了.其他说到底就用到个函数

解决方案 »

  1.   

    AES PHP有标注的实现库啊?自己写干嘛。
      

  2.   

    一直是乱码不知道什么问题
    <?php
    class CryptAES
    {
        protected $cipher = MCRYPT_RIJNDAEL_128;
        protected $mode = MCRYPT_MODE_CBC;
        protected $pad_method = NULL;
        protected $secret_key = '';
        protected $iv = '';    public function set_cipher($cipher)
        {
            $this->cipher = $cipher;
        }    public function set_mode($mode)
        {
            $this->mode = $mode;
        }    public function set_iv($iv)
        {
            $this->iv = $iv;
        }    public function set_key($key)
        {
            $this->secret_key = $key;
        }    public function require_pkcs5()
        {
            $this->pad_method = 'pkcs5';
        }    protected function pad_or_unpad($str, $ext)
        {
            if ( is_null($this->pad_method) )
            {
                return $str;
            }
            else
            {
                $func_name = __CLASS__ . '::' . $this->pad_method . '_' . $ext . 'pad';
                if ( is_callable($func_name) )
                {
                    $size = mcrypt_get_block_size($this->cipher, $this->mode);
                    return call_user_func($func_name, $str, $size);
                }
            }
            return $str;
        }    protected function pad($str)
        {
            return $this->pad_or_unpad($str, '');
        }    protected function unpad($str)
        {
            return $this->pad_or_unpad($str, 'un');
        }    public function encrypt($str)
        {
            $str = $this->pad($str);
            $td = mcrypt_module_open($this->cipher, '', $this->mode, '');        if ( empty($this->iv) )
            {
                $iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
            }
            else
            {
                $iv = $this->iv;
            }        mcrypt_generic_init($td, $this->secret_key, $iv);
            $cyper_text = mcrypt_generic($td, $str);
            $rt=base64_encode($cyper_text);
            //$rt = bin2hex($cyper_text);
            mcrypt_generic_deinit($td);
            mcrypt_module_close($td);        return $rt;
        }    public function decrypt($str){
    $size = mcrypt_get_block_size($this->cipher,$this->mode);
    $td = mcrypt_module_open($this->cipher, '', $this->mode, '');
            if ( empty($this->iv) )
            {
                $iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
            }
            else
            {
                $iv = $this->iv;
            }

    mcrypt_generic_init($td, $this->secret_key, $iv);
            $decrypted_text = mdecrypt_generic($td, base64_decode($str));
    print_r($decrypted_text);exit;
    $rt = $decrypted_text;
            mcrypt_generic_deinit($td);
            mcrypt_module_close($td);
            return $this->unpad($rt);
        } 
     /**
         * 转换一个String字符串为byte数组
         * @param $str 需要转换的字符串
         * @param $bytes 目标byte数组
         * @author wqw
         */
    private function toHexString ($string){  
            $buf = "";  
            for ($i = 0; $i < strlen($string); $i++){  
                $val = dechex(ord($string{$i}));  
                if(strlen($val)< 2)  
                    $val = "0".$val;  
                $buf .= $val;  
            }  
            return $buf;  
        }  
        public static function hex2bin($hexdata) {
            $bindata = '';
            $length = strlen($hexdata);
            for ($i=0; $i< $length; $i += 2)
            {
                $bindata .= chr(hexdec(substr($hexdata, $i, 2)));
            }
            return $bindata;
        }    public static function pkcs5_pad($text, $blocksize)
        {
            $pad = $blocksize - (strlen($text) % $blocksize);
            return $text . str_repeat(chr($pad), $pad);
        }    public static function pkcs5_unpad($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);
        }
    }以上是我php的解密算法求大神帮我看看
      

  3.   

    你应该给几组 java 的明文和密文
    这样才便于验证代码的正确性
      

  4.   

    明文:ooejCjlO0jqfwW1l2H8FMf-liOMQ
    密文:ojbmnkcobgdoadondiofnnaahjkkdbjpggnbolapjdgidgbofemhjlibhfoobkbh
    现在我只有这一组,希望各位帮帮忙
      

  5.   

    你够可以的,密钥也不给?搜索一下你的机器,你会找到不少 aes 加解密的 php 程序(至少在 phpmyadmin 中可以找到一个)
      

  6.   

    public function encode_aes($data, $key)
        {
            $td = mcrypt_module_open(MCRYPT_RIJNDAEL_256, '', MCRYPT_MODE_CBC, '');
            $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);        mcrypt_generic_init($td, $key, $iv);        $encrypted = mcrypt_generic($td, $data);        mcrypt_generic_deinit($td);        return $iv . $encrypted;
        }    public function decode_aes($data, $key)
        {
            $td = mcrypt_module_open(MCRYPT_RIJNDAEL_256, '', MCRYPT_MODE_CBC, '');
            $iv = mb_substr($data, 0, 32, 'latin1');        mcrypt_generic_init($td, $key, $iv);        $data = mb_substr($data, 32, mb_strlen($data, 'latin1'), 'latin1');
            $data = mdecrypt_generic($td, $data);        mcrypt_generic_deinit($td);
            mcrypt_module_close($td);        return trim($data);
        }
    上面两个函数是我以前用过的加密解密类的一部分使用的时候
    $data = encode_aes($data, $key);     //AES加密
    $data = base64_encode($data);        //base64编码因为当时有其它规则的需要,就没使用CI默认的加密类