PHP string Common algorithm--string encryption and decryption

Source: Internet
Author: User

/** * 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

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.