Home > Backend Development > PHP Tutorial > Detailed explanation of examples of encryption and decryption methods based on openssl in PHP

Detailed explanation of examples of encryption and decryption methods based on openssl in PHP

怪我咯
Release: 2023-03-12 19:56:01
Original
1976 people have browsed it

This article mainly introduces the implementation of encryption and decryption methods based on openssl in PHP, and analyzes PHP in the form of examplesCustom functionsRelevant techniques for implementing encryption and decryption operations based on openssl, friends in need can refer to the following

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

The above is the detailed content of Detailed explanation of examples of encryption and decryption methods based on openssl in PHP. For more information, please follow other related articles on 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