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
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.