码迷,mamicode.com
首页 > Web开发 > 详细

php代码加密笔记(二)

时间:2018-03-27 14:33:01      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:代码   echo   密钥   UNC   div   实现   decrypt   class   serialize   

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

  

php代码加密笔记(二)

标签:代码   echo   密钥   UNC   div   实现   decrypt   class   serialize   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!