Rev 218 | AutorÃa | Comparar con el anterior | Ultima modificación | Ver Log |
<?phpdeclare(strict_types=1);namespace LeadersLinked\Library;class AesEncryption {protected $key;protected $data;protected $method;/*** Available OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING**/protected $options = 0;function __construct($data = null, $key = null, $blockSize = null, $mode = 'CBC') {$this->setData($data);$this->setKey($key);$this->setMethode($blockSize, $mode);}public function setData($data) {$this->data = $data;}public function setKey($key) {$this->key = $key;}/*** CBC 128 192 256CBC-HMAC-SHA1 128 256CBC-HMAC-SHA256 128 256CFB 128 192 256CFB1 128 192 256CFB8 128 192 256CTR 128 192 256ECB 128 192 256OFB 128 192 256XTS 128 256*/public function setMethode($blockSize, $mode = 'CBC') {if($blockSize==192 && in_array('', array('CBC-HMAC-SHA1','CBC-HMAC-SHA256','XTS'))){$this->method=null;throw new \Exception('Invlid block size and mode combination!');}$this->method = 'AES-' . $blockSize . '-' . $mode;}/**** @return boolean*/public function validateParams() {if ($this->data != null &&$this->method != null ) {return true;} else {return FALSE;}}//it must be the same when you encrypt and decryptprotected function getIV() {return '1234567890123456';//return mcrypt_create_iv(mcrypt_get_iv_size($this->cipher, $this->mode), MCRYPT_RAND);return openssl_random_pseudo_bytes(openssl_cipher_iv_length($this->method));}/*** @throws \Exception*/public function encrypt() {if ($this->validateParams()) {return trim(openssl_encrypt($this->data, $this->method, $this->key, $this->options,$this->getIV()));} else {throw new \Exception('Invlid params!');}}/**** @throws \Exception*/public function decrypt() {if ($this->validateParams()) {$ret=openssl_decrypt($this->data, $this->method, $this->key, $this->options,$this->getIV());error_log('ret = ' . $ret . ' data = ' . $this->data . ' key = ' . $this->key);return trim($ret);} else {throw new \Exception('Invlid params!');}}}