/** * Encrypt, decrypt String * * @global string $db _hash * @global Array $pwServer * @param $string Pending String * @param $action operation, encode| DECODE * @return String*/functionStrcode ($string,$action= ' ENCODE ') { $action! = ' ENCODE ' &&$string=Base64_decode($string); $code= ' '; $key=substr(MD5($_server[' Http_user_agent ']), 8, 18); $keyLen=strlen($key); $strLen=strlen($string); for($i= 0;$i<$strLen;$i++) { $k=$i%$keyLen; $code.=$string[$i] ^$key[$k]; } return($action! = ' DECODE '?Base64_encode($code) :$code);}/** Encode for encryption, decode for decryption * encryption is to take each character of the string ^ operation, generate a new string and then base64 back. * The string used for the ^ operation is obtained by MD5 some global variables substr. * Note here that the ^ operation must be 2 strings of the same length to not produce a string, * For example: ' ASD ' ^ ' 123 ' = = ' PAW ', but ' asd ' ^ ' 123456 ' is still equal to ' PAW ', extra characters dropped, * and do not know how long the incoming string is, so the generated ^ The string of the operation does not know how long to generate, * this is done in a cyclic manner, that is, the ^ operation string can be any length, and then the string to be encrypted with the first character and the ^ operation of the string of the first character with the operation, * and so on, when the ^ operation is not long enough to cycle the character, This is the reason for the% operation in the for loop above. */
PHP string Common algorithm--string encryption and decryption