For most password encryption, we can use MD5, SHA1 and other methods.
Can effectively prevent data breaches, but these methods apply only to data encryption that does not need to be restored.
For information that needs to be restored, a reversible cryptographic decryption algorithm is required.
Many of the project's membership system, are required to remember the login function, in the use of cookies to realize the function is, because the customer information to be stored directly to the cookies, if directly written to the cookies will be a security risk, so by reversible encryption and then save to the cookie is relatively safe
Function source Code
function Encrypt ($data, $key) {$prep _code = serialize ($data); $block = Mcrypt_get_block_size (' des ', ' ECB '); if ($pad = $ Block-(strlen ($prep _code)% $block) < $block) {$prep _code. = Str_repeat (Chr ($pad), $pad);} $encrypt = Mcrypt_encry PT (Mcrypt_des, $key, $prep _code, MCRYPT_MODE_ECB); Return Base64_encode ($encrypt); } function Decrypt ($STR, $key) {$str = Base64_decode ($str); $str = Mcrypt_decrypt (Mcrypt_des, $key, $str, MCRYPT_MODE_ECB ); $block = Mcrypt_get_block_size (' des ', ' ECB '); $pad = Ord ($str [($len = strlen ($STR))-1]); if ($pad && $pad < $block && Preg_match ('/'. Chr ($pad). ' {'. $pad. '}$/', $str)) {$str = substr ($str, 0, strlen ($str)-$pad);} return Unserialize ($STR); }
Calling functions
$key = ' okyo.cn '; $data = array (' id ' = ' = ', ' username ' = ' customer ', ' Passwo Rd ' = ' e10adc3949ba59abbe56e057f20f883e '); $snarr = serialize ($data); $en = Encrypt ($data, $key); $de = Decrypt ($en, $key); echo "Crypto prototype:"; Print_r ($data); echo "key: $key encryption Result: $en decryption result:"; Print_r ($de);