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);

  

原文地址:https://www.cnblogs.com/burningc/p/8656736.html