Class phpmcrypt{/* operation handle */private $crypt = null;/* generated key */private $key = null;/* initial vector */private $iv = null;/* supported maximum key */p Rivate $ks = null;/* Whether the data type can accept */private $accept = false; False can not accept true to accept/* whether serialization */private $serialize = False is required; false does not need to serialize true to require serialization public function __construct ($secretKey = '!@#$%^ ') { /* open cryptographic algorithms and patterns */ $this->crypt = Mcrypt_module_open (' TripleDES ', ' ', ' nofb ', '); /* creates the initial vector and detects the key length. */ $this->iv = Mcrypt_create_iv (mcrypt_enc_get_iv_size ($this->crypt), Mcrypt_rand); /* get the longest key supported */ $this->ks = mcrypt_enc_get_key_size ($this->crypt); & nbsp;/* generate key */ $this->key = substr (MD5 ($secretKey), 0, $this->ks);} Whether the/*** data type can be accepted and serialized * $data int|string data to be encrypted *return boolean*/private function acceptandserialize ($data) { $type = GetType ($data); switch ($type) {&NBsp; case ' string ': case ' integer ': case ' float ': case ' double ': & nbsp $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; }}/*** Encrypted data * $data mixed data to be encrypted *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); /* Encrypted data */ $encrypt = mcrypt_generic ($this->crypt, $data); /* string to MIME BASE64 encoding. This encoding allows Chinese text or images to be transferred smoothly on the network */ $encrypt = Base64_encode ($encrypt); if ($this- Serialize) { $encrypt. = ' | '. $this->serialize; } /* end encryption, perform cleanup */ mcrypt_generic_ Deinit ($this->crypt); return $encrypt;} /*** Decryption NumberAccording to * $data string to decrypt data *return mixed*/public function decrypt ($data) { if (Empty ($data) ) { & nbsp return false; } /* determine if serialization */ if ( Strpos ($data, ' | ')) { $tempData = explode (' | ', $data); $data = $ tempdata[0]; $this->serialize = $tempData [1]; } /* initial dissolve */ mcrypt_generic_init ($this->crypt, $this->key, $this->iv); /* decryption Data */ $data = Base64_decode ($data); $decrypt = Mdecrypt_generic ($ This->crypt, $data); /* serialization */ if ($this->serialize) { $decrypt = unserialize ($decrypt); } /* end encryption, perform cleanup work */ mcrypt_generic_deinit ($this->crypt); return $decrypt;} Public Function __destruct () { /* Close the cryptographic 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);p rint_r ($decrypt);
The above describes the PHP encryption data operation class, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.