php代码加密笔记(二)

php实现基于openssl的加密解密方法

<?php
class openssl{
	private $key = 'key';
	public $id = '';
	function encrypt($id){
	  $id=serialize($id);
	  $this->id = $id;
	  $key= $this->key;
	  $data['iv']=base64_encode(substr('fdakinel;injajdji',0,16));
	  $data['value']=openssl_encrypt($id, 'AES-256-CBC',$key,0,base64_decode($data['iv']));
	  $encrypt=base64_encode(json_encode($data));
	  return $encrypt;
	}
	function decrypt($encrypt){
	  $key = $this->key;//解密钥匙
	  $encrypt = json_decode(base64_decode($encrypt), true);
	  $iv = base64_decode($encrypt['iv']);
	  $decrypt = openssl_decrypt($encrypt['value'], 'AES-256-CBC', $key, 0, $iv);
	  $id = unserialize($decrypt);
	  if($id){
	    return $id;
	  }else{
	    return 0;
	  }
	}
}

$obj = new openssl();
$encrypt = $obj->encrypt('1');
echo $obj->decrypt($encrypt);

  

posted @ 2018-03-27 12:01  Burning_Ice  阅读(156)  评论(0编辑  收藏  举报