The member systems of many projects require the remember login function. the Function of using cookies is to directly store customer information to cookies, if you write cookies directly, security risks are inevitable. Therefore, if you store cookies after reversible encryption, the function source code is secure.
The code is as follows:
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_encrypt (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 );
}
Call a function
The code is as follows:
$ Key = 'okyo. cn ';
$ Data = array ('id' => 100, 'username' => 'customer', 'password' => 'e10adc3949ba59abbe56e057f20f883e ');
$ Snarr = serialize ($ data );
$ En = encrypt ($ data, $ key );
$ De = decrypt ($ en, $ key );
Echo "encryption prototype :";
Print_r ($ data );
Echo"
Key: $ key
Encryption result: $ en
Decryption result :";
Print_r ($ de );