Home >Backend Development >PHP Tutorial >PHP implements encryption and decryption method based on openssl

PHP implements encryption and decryption method based on openssl

高洛峰
高洛峰Original
2016-12-30 13:54:561609browse

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!

Statement:
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