Home >Backend Development >PHP Tutorial >PHP implements simple symmetric encryption

PHP implements simple symmetric encryption

*文
*文Original
2018-01-02 16:13:363789browse

Sometimes we encounter data that needs to be encrypted in the project, but we have to decrypt it ourselves, so we can only find some symmetric encryption algorithms, and then decrypt it when we want to see it. This article introduces the implementation of a simple symmetric encryption algorithm in PHP. I hope it will be helpful to everyone.

Preface

I found a good symmetric encryption algorithm for PHP on the Internet; in the PHP syntax environment, there are urlencode and urldecode , base64_encode and base64_decode come with their own symmetric algorithms, but these built-in algorithms cannot be called encryption algorithms, they can only be said to be encoding methods. 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 if we # How many characters are inserted into the string after ##base64_encode()? Then it cannot be converted back. Even if it is converted, 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 = &#39;&#39;, $skey = &#39;wenzi&#39;) {
 $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(&#39;=&#39;, &#39;+&#39;, &#39;/&#39;), array(&#39;O0O0O&#39;, &#39;o000o&#39;, &#39;oo00o&#39;), join(&#39;&#39;, $strArr));
 }

 /**
 * 简单对称加密算法之解密
 * @param String $string 需要解密的字串
 * @param String $skey 解密KEY
 * @return String
 */
 function decode($string = &#39;&#39;, $skey = &#39;wenzi&#39;) {
 $strArr = str_split(str_replace(array(&#39;O0O0O&#39;, &#39;o000o&#39;, &#39;oo00o&#39;), array(&#39;=&#39;, &#39;+&#39;, &#39;/&#39;), $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(&#39;&#39;, $strArr));
 }

 echo &#39;<pre class="brush:php;toolbar:false">&#39;;
 echo "string : " . $content . " <br />";
 echo "encode : " . ($enstring = encode($content)) . &#39;<br />&#39;;
 echo "decode : " . decode($enstring);

 exit();

In the above algorithm we can see: we generate

base64_encode() Insert the key we set in advance into the characters, and then replace the special characters inside. 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.

Related recommendations:

php encryption and decryption

PHP How to better encrypt passwords

php method to obtain flash detailed data

The above is the detailed content of PHP implements simple symmetric encryption. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn