PHP Data encryption operations

Source: Internet
Author: User
Tags crypt
: This article mainly introduces the operation of PHP Data Encryption. if you are interested in the PHP Tutorial, refer to it.
Class PHPMcrypt {/* operation handle */private $ crypt = null;/* generated key */private $ key = null;/* initial vector */private $ iv = null; /* maximum supported Keys */private $ ks = null;/* whether the data type is acceptable */private $ accept = false; // false cannot accept true acceptable/* serialization required */private $ serialize = false; // false do not require serialization true public function _ construct ($ secretKey = '! @ # $ % ^ ') {/* Open the encryption algorithm and mode */$ this-> crypt = mcrypt_module_open ('tripledes', '', 'nofb ',''); /* create an initial vector and check the key length. */$ This-> iv = mcrypt_create_iv (mcrypt_enc_get_iv_size ($ this-> crypt), MCRYPT_RAND ); /* obtain the longest supported Keys */$ this-> ks = mcrypt_enc_get_key_size ($ this-> crypt ); /* generate the key */$ this-> key = substr (md5 ($ secretKey), 0, $ this-> ks );} /*** whether the data type is acceptable or not * $ data int | string the data to be encrypted * return boolean */private function acceptAndSerialize ($ data) {$ type = gettype ($ data); switch ($ type) {case 'string': case 'integer': case 'float': c Ase 'double': $ this-> accept = true; $ this-> serialize = false; break; case 'array': case 'object ': $ this-> accept = true; $ this-> serialize = true; break; default: $ this-> accept = false; $ this-> serialize = false ;}} /*** encrypt data ** $ data mixed * return string | boolean */public function encrypt ($ data) {$ this-> acceptAndSerialize ($ data ); if (empty ($ data) | ($ this-> accept = false) {return false;} // serialization If ($ this-> serialize) {$ data = serialize ($ data);}/* initialize encryption */mcrypt_generic_init ($ this-> crypt, $ this-> key, $ this-> iv);/* encrypt data */$ encrypt = mcrypt_generic ($ this-> crypt, $ data);/* the string is encoded in MIME Base64. This encoding method allows smooth transmission of Chinese text or images over the network */$ encrypt = base64_encode ($ encrypt); if ($ this-> serialize) {$ encrypt. = '| '. $ this-> serialize;}/* ends encryption and performs cleanup */mcrypt_generic_deinit ($ this-> crypt); return $ encrypt ;} /*** decrypt data ** $ data string * return mixed */public function decrypt ($ data) {if (empty ($ data) {return false ;} /* determine whether to serialize */if (strpos ($ data, '|') {$ tempData = explode ('|', $ data ); $ data = $ tempData [0]; $ this-> serialize = $ tempData [1];}/* initialize decryption */mcrypt_generic_init ($ this-> crypt, $ this-> key, $ this-> iv);/* decrypt data */$ data = base64_decode ($ data); $ decrypt = mdecrypt_generic ($ this-> crypt, $ data);/* deserialization */if ($ this-> serialize) {$ decrypt = unserialize ($ decrypt);}/* ends encryption, execute cleanup */mcrypt_generic_deinit ($ this-> crypt); return $ decrypt;} public function _ destruct () {/* disable the encryption module */mcrypt_module_close ($ this-> crypt) ;}// int | float | string
$ OPHPMcrypt = new PHPMcrypt (123); $ encrypt = 'China * Shanghai-021 '; $ encrypt = $ oPHPMcrypt-> encrypt ($ encrypt); echo $ encrypt; echo'
'; $ Decrypt = $ oPHPMcrypt-> decrypt ($ encrypt); echo $ decrypt; // array | object $ oPHPMcrypt = new PHPMcrypt (123 ); $ encrypt = array ('name' => 'Zhang San', 'age' => 100); $ encrypt = $ oPHPMcrypt-> encrypt ($ encrypt); echo $ encrypt; echo'
'; $ Decrypt = $ oPHPMcrypt-> decrypt ($ encrypt); print_r ($ decrypt );


The above introduces the PHP Data encryption operation class, including the content, hope to be helpful to friends who are interested in the PHP Tutorial.

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.