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 'ecb':$this->mode = MCRYPT_MODE_ECB; break; case 'cfb':$this->mode = MCRYPT_MODE_CFB; break; case 'ofb':$this->mode = MCRYPT_MODE_OFB; break; case 'nofb':$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 = 'abcdef1234567890', $iv = '0987654321fedcba', $mode = 'cbc'); $c = $aes->encrypt('haowei.me'); var_dump($aes->decrypt($c));
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!