Home > Backend Development > PHP Tutorial > Implementation of symmetric encryption algorithm in php

Implementation of symmetric encryption algorithm in php

墨辰丷
Release: 2023-03-28 09:06:01
Original
2019 people have browsed it

Recently, I suddenly wanted to save some confidential things in the database, and then I thought about how to prevent others from understanding what is stored even if they enter the database, so the only way is to encrypt it; but we still have to see it ourselves. Then we can only find some symmetric encryption algorithms, and then decrypt it when we want to see it. The following introduces the implementation of a simple symmetric encryption algorithm in PHP.

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();
Copy after login

In the above algorithm we can see: We insert the key we set in advance into the characters generated by

base64_encode(), 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.

The above is the entire content of this article, I hope it will be helpful to everyone's study.


Related recommendations:

PHP uses recursionAlgorithmMethod for infinite traversal of an array

Commonly used sorting in PHPAlgorithmDetailed explanation of examples

##Commonly used PHP algorithms

Detailed explanation of examples of data structures

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template