PHP's encryption and decryption string function, php's encryption and decryption string. The PHP encryption and decryption string function code is as follows: **************************************** * ***** PHP's string encryption and decryption function, php encryption and decryption string
Frequently used PHP encryption and decryption string functions in the program
The code is as follows:
/*************************************** * ***************************** Function name: encrypt function: encryption and decryption string usage: Encryption: encrypt ('str', 'e', 'nowamagic '); decryption: encrypt ('encrypted string ', 'D', 'nowamagic '); parameter description: $ string: string to be encrypted and decrypted $ operation: whether to encrypt or decrypt: E: encrypted D: decrypt $ key: encrypted Key (key ); http://www.cnblogs.com/roucheng/ **************************************** * ***************************/Function encrypt ($ string, $ operation, $ key = '') {$ key = md5 ($ key); $ key_length = strlen ($ key); $ string = $ operation = 'd '? Base64_decode ($ string): substr (md5 ($ string. $ key), 0, 8 ). $ string; $ string_length = strlen ($ string); $ rndkey = $ box = array (); $ result = ''; for ($ I = 0; $ I <= 255; $ I ++) {$ rndkey [$ I] = ord ($ key [$ I % $ key_length]); $ box [$ I] = $ I;} for ($ j = $ I = 0; $ I <256; $ I ++) {$ j = ($ j + $ box [$ I] + $ rndkey [$ I]) % 256; $ tmp = $ box [$ I]; $ box [$ I] = $ box [$ j]; $ box [$ j] = $ tmp;} for ($ a = $ j = $ I = 0; $ I <$ string_length; $ I ++) {$ a = ($ a + 1) % 256; $ j = ($ j + $ box [$ a]) % 256; $ tmp = $ box [$ a]; $ box [$ a] = $ box [$ j]; $ box [$ j] = $ tmp; $ result. = chr (ord ($ string [$ I]) ^ ($ box [($ box [$ a] + $ box [$ j]) % 256]);} if ($ operation = 'd') {if (substr ($ result,) = substr (md5 (substr ($ result, 8 ). $ key),) {return substr ($ result, 8) ;}else {return '';}} else {return str_replace ('= ','', base64_encode ($ result ));}}
Usage:
$ Id = 132; $ token = encrypt ($ id, 'e', 'A'); echo 'encryption :'. encrypt ($ id, 'e', 'A'); echo'
'; Echo 'decryption:'. encrypt ($ token, 'D ', 'A ');
The PHP encryption and decryption string function code frequently used in the encryption program is as follows: /*************************************** ********...