Php implements openssl-based encryption and decryption methods

Source: Internet
Author: User
This article mainly introduces how to implement openssl-based encryption and decryption in php, and analyzes the encryption and decryption operations related to php user-defined functions based on openssl in the form of examples, for more information about how to implement openssl-based encryption and decryption in php, see the example in this article. We will share this with you for your reference. The details are as follows:

Openssl encryption and decryption methods

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 = '000000'; // decryption 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 ;}}

I hope this article will help you with PHP programming.

For more articles about how to implement openssl-based encryption and decryption methods in php, refer to PHP Chinese network!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.