PHP implements encryption and decryption method based on openssl

高洛峰
Release: 2023-03-04 14:14:01
Original
1531 people have browsed it

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;
}
Copy after login

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;
  }
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!