PHP string encryption and decryption function, php string encryption and decryption
/*************************************** * ***************************** 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; encryption 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 ));}}
$ Id = 132; $ token = encrypt ($ id, 'E', 'A'); echo 'encryption :'. encrypt ($ id, 'E', 'A'); echo '<br/>'; echo 'decryption :'. encrypt ($ token, 'D', 'A ');