The example of this article describes the encryption and decryption method based on openssl in PHP. Share it with everyone for your reference, the details are as follows:
Encryption and decryption method through openssl
1. openssl encryption method:
function encrypt($id){ $id=serialize($id); $key="1112121212121212121212"; $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; }
2. openssl decryption method:
function decrypt($encrypt) { $key = '1112121212121212121212';//解密钥匙 $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; } }
I hope this article will be helpful to everyone in PHP programming.
For more articles related to PHP implementing encryption and decryption methods based on openssl, please pay attention to the PHP Chinese website!