首页 > Web开发 > 详细

php代码加密笔记(二)

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

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代码加密笔记(二)

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!