Home >Backend Development >PHP Tutorial >Simple symmetric encryption algorithm implementation in php
Preface
I found a good symmetric encryption algorithm for PHP on the Internet; in the PHP syntax environment, there are symmetric algorithms that come with urlencode and urldecode, base64_encode and base64_decode, but these come with The algorithm cannot be called an encryption algorithm, it can only be called an encoding method. But we can use these to perform some processing to implement simple encryption and decryption algorithms.
This time the encryption and decryption algorithms are adapted using base64. Usually we use the string generated by base64_encode($str). Without any processing, base64_decode() can convert back to our previous string; but what if we insert a few characters into the string after base64_encode()? , then it cannot be turned back, and even if it is turned around, it is not our own string.
The sample code is as follows:
<?php $content = "大家好,我是中国人,你是谁"; /** * 简单对称加密算法之加密 * @param String $string 需要加密的字串 * @param String $skey 加密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('O0O0O', 'o000o', 'oo00o'), join('', $strArr)); } /** * 简单对称加密算法之解密 * @param String $string 需要解密的字串 * @param String $skey 解密KEY * @return String */ function decode($string = '', $skey = 'wenzi') { $strArr = str_split(str_replace(array('O0O0O', 'o000o', 'oo00o'), array('=', '+', '/'), $string), 2); $strCount = 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 class="brush:php;toolbar:false">'; echo "string : " . $content . " <br />"; echo "encode : " . ($enstring = encode($content)) . '<br />'; echo "decode : " . decode($enstring); exit();
In the above algorithm we can see: we insert the key we set in advance into the characters generated by base64_encode(), and then add the special characters inside Characters are replaced, even if others see such a string, they will not know what it is. Of course, we can make slight improvements here, such as inserting the key into the string backwards, base64 the key and then insert it, etc. After inserting the key, base64 it again.
Of course decryption is the opposite direction of encryption. After thinking for a while, I realized the principle of decryption: before we inserted some characters into the string, now we have to take them out when decrypting. First of all Group the encrypted strings by 2 elements in each array, and then determine whether the second character is in the key. If so, then the first character is the character in the original base64.
Summary
The above is the entire content of this article. Of course, in addition to base64 encryption and decryption in php, there are also algorithms such as AES and DES. Friends in need can pay attention Script House, I believe it will be helpful to everyone.
For more articles related to the implementation of simple symmetric encryption algorithms in PHP, please pay attention to the PHP Chinese website!