Objective
Found a good PHP symmetric encryption algorithm on the Internet, in the PHP syntax environment, there are UrlEncode and Urldecode,base64_encode and Base64_decode with the symmetric algorithm, but these self-band algorithm can not be called encryption algorithm, It can only be said to be a coding method. But we can use these to do some processing, to implement a simple encryption and decryption algorithm.
This time the encryption and decryption algorithms are adapted using the Base64 method. Usually we use Base64_encode ($STR) generated string, without any processing, base64_decode () will be able to return to our previous string, but if we are in the string after Base64_encode () to insert a few characters, Then he will not turn back, even if turned around is not our own string.
The sample code is as follows:
<?php$content = "Hello everyone, I am Chinese, who you are"; /*** encryption for simple symmetric encryption * @param string $string strings that need to be encrypted * @param string $skey encryption eky* @return string*/function encode ($string = ', $skey = ' Wenzi ') {$STRARR = Str_split (Base64_encode ($string)); $strCount = count ($STRARR); foreach (Str_split ($skey) as $ Key = $value) $key < $strCount && $strArr [$key].= $value; return str_replace (Array (' = ', ' + ', '/'), Array (' O0 o0o ', ' o000o ', ' oo00o '), join (', $STRARR));} Decryption of/*** simple symmetric encryption algorithm * @param string $string The string to be decrypted * @param string $skey decrypt key* @return string*/function decode ($string = ', $skey = ' Wenzi ') {$STRARR = Str_split (Str_replace (' o0o0o ', ' o000o ', ' oo00o '), array (' = ', ' + ', '/'), $string), 2); $st Rcount = count ($STRARR), foreach (Str_split ($skey) as $key = + $value) $key <= $strCount && $strArr [$key][1] = = = $value && $strArr [$key] = $STRARR [$key][0];return Base64_decode (Join (", $STRARR));} Echo ' <pre> '; echo "string:". $content. "<br/>"; echo "encode:". ($enstring = Encode ($content)). ' <br/> '; echo "decode:". Decode ($enstring); Exit ();
In the algorithm above we can see: We insert the character of the Base64_encode () into our pre-set key, and then replace the special characters inside, even if others see such a string does not know what it is. Of course, here we have a little bit of improvement, such as inserting the key backwards into the string, the key base64 after the insertion, and so on, insert the key and then Base64 once.
Of course, decryption is the opposite direction of encryption, thinking for a while to know the principle of decryption: before we inserted some characters in the string, now decryption we have to pick him out, first the encrypted string in each array of 2 elements to group, and then determine whether the second character is in the key, if it is, Then the first character is the character in the original Base64.
Summarize
The above is the entire content of this article, of course, in addition to Base64 encryption and decryption in PHP, as well as AES and Des algorithms, there is a need for friends to focus on the script house, I believe it will help you.