Complete example code of AES encryption algorithm in php

怪我咯
Release: 2023-03-13 19:46:01
Original
1776 people have browsed it

Advanced Encryption Standard (English: Advanced Encryption Standard, abbreviation: AES), also known as Rijndael encryption method in cryptography, is a block encryption standard adopted by the U.S. federal government. This standard is used to replace the original DES. It has been analyzed by many parties and is widely used around the world. After a five-year selection process, the Advanced Encryption Standard was published by the National Institute of Standards and Technology (NIST) in FIPS PUB 197 on November 26, 2001, and became an effective standard on May 26, 2002. In 2006, Advanced Encryption Standard has become one of the most popular algorithms for symmetric key encryption. 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

<?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

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