`

mcrypt 安装使用

    博客分类:
  • PHP
阅读更多
用途:

用于加密,解密数据。


安装:

先安装libmcrypt库

#wget http://softlayer.dl.sourceforge.net/sourceforge/mcrypt/libmcrypt-2.5.8.tar.gz#tar -zxvf libmcrypt-2.5.8.tar.gz#cd 源代码/libmcrypt-2.5.8#./configure --prefix=/usr/local#make#make install


安装php扩展

# cd php-5.x.x/ext/mcrypt# phpize# aclocal# ./configure# make && make installEnable the module by adding: 'extension=mcrypt.so' to PHP.ini.


使用:


class Cipher {
    private $securekey, $iv;
    public function __construct($key) {
    	if (!function_exists('mcrypt_create_iv')) {
    		throw new Exception('Please install mcrypt extension!');
    	}
        $this->securekey = hash('sha256',$key,TRUE);
        $this->iv = mcrypt_create_iv(32);
    }

    public function encrypt($input) {
        return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $this->securekey, $input, MCRYPT_MODE_ECB, $this->iv));
    }

    public function decrypt($input) {
        return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $this->securekey, base64_decode($input), MCRYPT_MODE_ECB, $this->iv));
    }
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics