PHP to achieve AES encryption class sharing

Source: Internet
Author: User
Tags decrypt rtrim

  This article mainly introduces the AES encryption classes implemented in PHP, the code has the use of methods, the need for friends can refer to the following

  Code as follows: <?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) | | Mpty ($mode))   return NULL;     $this->bit = $bit;   $this->key = $key;   $this->iv = $iv;   $this->mode = $mode;     switch ($this->bit) {   case: $this->cipher = mcrypt_rijndael_192; break;    c ASE 256: $this->cipher = mcrypt_rijndael_256; Break    default: $this->cipher = mcrypt_rijndael_128;  }     switch ($this->mode) {   case ' ECB ': $this->mode = MCRYPT_MODE_ECB; break;   &N Bsp;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;  } &nbsp    public function Encrypt ($data) {  $data = Base64_encode (Mcrypt_encrypt ($this->ciph Er, $this->key, $data, $this->mode, $this->iv));   return $data; &NBSP}    public function decrypt ($data) {  $data = Mcrypt_decrypt ($this->cipher, $this->key, base 64_decode ($data), $this->mode, $this->iv);   $data = RTrim (RTrim ($data), "x00." x1f ");   return $data; &NBSP}  }  //use method $aes = new Aesmcrypt ($bit = 128, $key = ' abcdef1234567890 ', $iv = ' 0987654321FEDCBA ', $mode = ' CBC '); $c = $aes->encrypt (' haowei.me '); Var_dump ($aes->decrypt ($c));    
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.