Detailed explanation of PHP's AES encryption algorithm example

墨辰丷
Release: 2023-03-29 10:16:02
Original
2023 people have browsed it

This article mainly introduces PHP's AES encryption algorithm, and analyzes PHP's AES encryption algorithm class and its specific usage in the form of a complete example. It has certain reference value. Friends in need can refer to it

The details are as follows:

<?php
class MCrypt {
  private $hex_iv = &#39;00000000000000000000000000000000&#39;; # converted JAVA byte code in to HEX and placed it here
  private $key = &#39;U1MjU1M0FDOUZ.Qz&#39;; #Same as in JAVA
  function __construct() {
    $this->key = hash(&#39;sha256&#39;, $this->key, true);
    //echo $this->key.&#39;<br/>&#39;;
  }
  function encrypt($str) {
    $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, &#39;&#39;, MCRYPT_MODE_CBC, &#39;&#39;);
    mcrypt_generic_init($td, $this->key, $this->hexToStr($this->hex_iv));
    $block = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
    $pad = $block - (strlen($str) % $block);
    $str .= str_repeat(chr($pad), $pad);
    $encrypted = mcrypt_generic($td, $str);
    mcrypt_generic_deinit($td);
    mcrypt_module_close($td);
    return base64_encode($encrypted);
  }
  function decrypt($code) {
    $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, &#39;&#39;, MCRYPT_MODE_CBC, &#39;&#39;);
    mcrypt_generic_init($td, $this->key, $this->hexToStr($this->hex_iv));
    $str = mdecrypt_generic($td, base64_decode($code));
    $block = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
    mcrypt_generic_deinit($td);
    mcrypt_module_close($td);
    return $this->strippadding($str);
  }
  /*
   For PKCS7 padding
   */
  private function addpadding($string, $blocksize = 16) {
    $len = strlen($string);
    $pad = $blocksize - ($len % $blocksize);
    $string .= str_repeat(chr($pad), $pad);
    return $string;
  }
  private function strippadding($string) {
    $slast = ord(substr($string, -1));
    $slastc = chr($slast);
    $pcheck = substr($string, -$slast);
    if (preg_match("/$slastc{" . $slast . "}/", $string)) {
      $string = substr($string, 0, strlen($string) - $slast);
      return $string;
    } else {
      return false;
    }
  }
  function hexToStr($hex)
  {
    $string=&#39;&#39;;
    for ($i=0; $i < strlen($hex)-1; $i+=2)
    {
      $string .= chr(hexdec($hex[$i].$hex[$i+1]));
    }
    return $string;
  }
}
$encryption = new MCrypt();
echo $encryption->encrypt(&#39;123456&#39;) . "<br/>";
echo $encryption->decrypt(&#39;tpyxISJ83dqEs3uw8bN/+w==&#39;);
?>
Copy after login

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

Related recommendations:

Detailed explanation of the steps to generate data dictionary in PHP

##phpFramework CodeIgniter database Detailed explanation of the configuration steps

php7 Detailed explanation of the use of new features

The above is the detailed content of Detailed explanation of PHP's AES encryption algorithm example. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!