PHP Encryption and Decryption utility class sharing

Source: Internet
Author: User

  Encryption and decryption is a common task, and here is an encryption and decryption class. If you want to retrieve the original password for him or her when the user forgets the password, this class is a useful tool

User registered password generally will not be saved in plain text, always add a secret first. The simplest of course is to call the MD5 function in the database SQL statement to encrypt the user's password. Here is an encryption and decryption class. This class is a handy tool if you want to retrieve the original password for him or her when the user forgets the password. Of course, this encryption and decryption class can also be used for other purposes.   Code as follows: <?php   class Crypt {      private $skey;       Public Function __const Ruct ($key) {        $this->skey = hash ("MD5", $key, true);//32-bit skey    }       Public Function Safe_b64encode ($string) {        $data = Base64_encode ($string);   &NBSP ;     $data = str_replace (Array (' + ', '/', ' = '), array ('-', ' _ ', '), $data);         return $data;    }       public Function Safe_b64decode ($string) {        $data = Str_rep Lace (Array ('-', ' _ '), Array (' + ', '/'), $string);         $mod 4 = strlen ($data)% 4;         if ($mod 4) {            $data = substr      }         return Base64_decode ($data);    }       Public function encode ($value) {        if (! $value) {  &N Bsp         return false;        }         $text = $value;         $iv _size = Mcrypt_get_iv_size (mcrypt_rijndael_256, MCRYPT_MODE_ECB);         $IV = Mcrypt_create_iv ($iv _size, Mcrypt_rand);         $crypttext = Mcrypt_encrypt (mcrypt_rijndael_256, $this->skey, $text, MCRYPT_MODE_ECB, $iv );         return trim ($this->safe_b64encode ($crypttext));    }       Public function decode ($value) {        if (! $value) {  &N Bsp         return false;        }         $crypttext = $this->safe_b64decode ($value);         $iv _size = mcrypt_get_iv_size (mcrypt_rijndael_256, MCRYPT_MODE_ECB);         $IV = Mcrypt_create_iv ($iv _size, Mcrypt_rand);         $decrypttext = Mcrypt_decrypt (mcrypt_rijndael_256, $this->skey, $crypttext, Mcrypt_mode_ ECB, $IV);         return trim ($decrypttext);    }  }  

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.