Home  >  Article  >  Backend Development  >  PHP encrypted data operation class

PHP encrypted data operation class

WBOY
WBOYOriginal
2016-08-08 09:30:00902browse
class PHPMcrypt{
/* 操作句柄 */
private $crypt = null;
/* 生成的密钥 */
private $key = null;
/* 初始向量 */
private $iv = null;
/* 支持的最长密钥 */
private $ks = null;
/* 数据类型是否可以接受 */
private $accept = false; // false 不可以接受 true 可以接受
/* 是否需要序列化 */
private $serialize = false; //false  不需要序列化 true 需要序列化
    
public function __construct($secretKey='!@#$%^'){
    /* 打开加密算法和模式 */
    $this->crypt = mcrypt_module_open('tripledes','','nofb','');
    /* 创建初始向量,并且检测密钥长度。 */
    $this->iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($this->crypt), MCRYPT_RAND);
    /* 获取支持的最长密钥 */
    $this->ks = mcrypt_enc_get_key_size($this->crypt);
    /* 生成密钥 */
    $this->key = substr(md5($secretKey),0,$this->ks);

}

/**
*数据类型是否可以接受和序列化
*$data int|string 要加密的数据
*return boolean
*/
private function acceptAndSerialize($data){
    $type = gettype($data);
    switch($type){
        case 'string':
        case 'integer':
        case 'float':
        case 'double ':
            $this->accept = true;
            $this->serialize = false;
        break;
        case 'array':
        case 'object':
            $this->accept = true;
            $this->serialize = true;
        break;
        default:
            $this->accept = false;
            $this->serialize = false;
    }
}

/**
*加密数据
*$data mixed 要加密的数据
*return string|boolean
*/
public function encrypt($data){
    $this->acceptAndSerialize($data);
    if( empty($data) || ($this->accept == false) ){
        return false;
    }
    //序列化
    if( $this->serialize ){
        $data = serialize($data);
    }
    /* 初始化加密 */
    mcrypt_generic_init($this->crypt,$this->key,$this->iv);
    /* 加密数据 */
    $encrypt = mcrypt_generic($this->crypt,$data);
    /* 字符串以 MIME BASE64 编码。此编码方式可以让中文字或者图片也能在网络上顺利传输 */
    $encrypt = base64_encode($encrypt);
    if( $this->serialize ){
        $encrypt .='|'.$this->serialize;
    }
    /* 结束加密,执行清理工作 */
    mcrypt_generic_deinit($this->crypt);
    return $encrypt;
}

/**
*解密数据
*$data string 要解密的数据
*return mixed
*/
public function decrypt($data){
    if( empty($data)  ){
        return false;
    }
    /* 判断是否序列化 */
    if( strpos($data,'|') ){
        $tempData = explode('|',$data);
        $data = $tempData[0];
        $this->serialize = $tempData[1];
    }
    /* 初始化解密 */
    mcrypt_generic_init($this->crypt,$this->key,$this->iv);
    /* 解密数据 */
    $data = base64_decode($data);
    $decrypt = mdecrypt_generic($this->crypt,$data);
    /* 解序列化 */
    if( $this->serialize){
        $decrypt = unserialize($decrypt);
    }
    /* 结束加密,执行清理工作 */
    mcrypt_generic_deinit($this->crypt);
    return $decrypt;
}

public function __destruct(){
    /* 关闭加密模块 */
    mcrypt_module_close($this->crypt);
}

}

//int|float|字符串
<pre name="code" class="html">$oPHPMcrypt = new PHPMcrypt(123);
$encrypt = '中国*上海-021';
$encrypt = $oPHPMcrypt->encrypt($encrypt);
echo $encrypt;
echo '<br />';
$decrypt = $oPHPMcrypt->decrypt($encrypt);
echo $decrypt;

//数组|对象
$oPHPMcrypt = new PHPMcrypt(123);
$encrypt = array('name'=>'张三','age'=>100);
$encrypt = $oPHPMcrypt->encrypt($encrypt);
echo $encrypt;
echo '<br />';
$decrypt = $oPHPMcrypt->decrypt($encrypt);
print_r($decrypt);





The above introduces the PHP encrypted data operation class, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn