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 } &NBSP; 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); } }