Home > Backend Development > PHP Tutorial > Introduction to PHP simple symmetric encryption algorithm (code example)

Introduction to PHP simple symmetric encryption algorithm (code example)

不言
Release: 2023-04-05 16:24:02
forward
2539 people have browsed it

This article brings you an introduction to PHP’s simple symmetric encryption algorithm (code example), which has certain reference value. Friends in need can refer to it, I hope it will be helpful to you.

Starting greeting: PHP is the best language in the world, and may be the best language in the universe in the future. If you want to learn more PHP knowledge, you can blog more articles to learn more about PHP expertise.

Encryption

Without further ado, let’s get straight to the code! The code is as follows:

  /**
   * 简单对称加密算法之加密
   * @param String $string 需要加密的字串
   * @param String $skey   加密EKY
   * @return String
   */
   public static function encode($string = '', $skey = 'cxphp')
   {
       $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));
   }
Copy after login

Decryption

Without further ado, let’s go straight to the code! The code is as follows:

/**
 * 简单对称加密算法之解密
 * @param String $string 需要解密的字串
 * @param String $skey   解密KEY
 * @return String
 */
  public static function decode($string = &#39;&#39;, $skey = &#39;cxphp&#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 && isset($strArr[$key]) && $strArr[$key][1] === $value && $strArr[$key] = $strArr[$key][0];
      }
      return base64_decode(join(&#39;&#39;, $strArr));
  }
Copy after login
<br/>
Copy after login

The above is the detailed content of Introduction to PHP simple symmetric encryption algorithm (code example). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:csdn.net
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