aes encryption class implemented by php

怪我咯
Release: 2023-03-13 19:48:01
Original
2632 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 in FIPS PUB 197 by the National Institute of Standards and Technology (NIST) 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 the AES encryption class implemented in PHP. There are usage methods in the code. Friends in need can refer to it

<?php
class AESMcrypt {

 public $iv = null;
 public $key = null;
 public $bit = 128;
 private $cipher;

 public function construct($bit, $key, $iv, $mode) {
  if(empty($bit) || empty($key) || empty($iv) || empty($mode))
  return NULL;

  $this->bit = $bit;
  $this->key = $key;
  $this->iv = $iv;
  $this->mode = $mode;

  switch($this->bit) {
   case 192:$this->cipher = MCRYPT_RIJNDAEL_192; break;
   case 256:$this->cipher = MCRYPT_RIJNDAEL_256; break;
   default: $this->cipher = MCRYPT_RIJNDAEL_128;
  }

  switch($this->mode) {
   case &#39;ecb&#39;:$this->mode = MCRYPT_MODE_ECB; break;
   case &#39;cfb&#39;:$this->mode = MCRYPT_MODE_CFB; break;
   case &#39;ofb&#39;:$this->mode = MCRYPT_MODE_OFB; break;
   case &#39;nofb&#39;:$this->mode = MCRYPT_MODE_NOFB; break;
   default: $this->mode = MCRYPT_MODE_CBC;
  }
 }

 public function encrypt($data) {
  $data = base64_encode(mcrypt_encrypt( $this->cipher, $this->key, $data, $this->mode, $this->iv));
  return $data;
 }

 public function decrypt($data) {
  $data = mcrypt_decrypt( $this->cipher, $this->key, base64_decode($data), $this->mode, $this->iv);
  $data = rtrim(rtrim($data), "\x00..\x1F");
  return $data;
 }

}
//使用方法
$aes = new AESMcrypt($bit = 128, $key = &#39;abcdef1234567890&#39;, $iv = &#39;0987654321fedcba&#39;, $mode = &#39;cbc&#39;);
$c = $aes->encrypt(&#39;haowei.me&#39;);
var_dump($aes->decrypt($c));
Copy after login

The above is the detailed content of aes encryption class implemented by 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!