1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- <?php
- /**
- * AES.php UTF-8
- *
- *
- * @date : 2021-03-10 17:29
- *
- * @license 这不是一个自由软件,未经授权不许任何使用和传播。
- * @author : luowei <lw@huosdk.com>
- * @version : HUOSDK 9.0
- */
- namespace huoIdentify\controller;
- class AES {
- /**
- * key
- *
- * @var string
- */
- private $key;
- /**
- * method
- *
- * @var string
- */
- private $cipher;
- /**
- * iv length
- *
- * @var int
- */
- private $ivlen;
- /**
- * AES constructor.
- *
- * @param $key
- * @param string $cipher
- * @param int $ivlen
- */
- public function __construct($key, $cipher = 'aes-128-gcm', $ivlen = 12) {
- $this->cipher = $cipher;
- $this->ivlen = $ivlen;
- $this->key = hex2bin($key);
- }
- /**
- * AES GCM 128
- *
- * @param $cipherText
- * @param callable|null $callback
- *
- * @return string|false
- */
- public function decrypt($cipherText, callable $callback = null) {
- if (!is_null($callback)) {
- $cipherText = call_user_func($callback, $cipherText);
- } else {
- // method base64_encode as default return
- $cipherText = base64_decode($cipherText);
- }
- $iv = substr($cipherText, 0, $this->ivlen);
- $data = substr($cipherText, $this->ivlen, strlen($cipherText) - 16 - $this->ivlen);
- $tag = substr($cipherText, strlen($cipherText) - 16);
- return openssl_decrypt($data, $this->cipher, $this->key, OPENSSL_RAW_DATA, $iv, $tag);
- }
- /**
- * AES GCM 128
- *
- * @param string $plainText
- * @param callable|null $callback
- *
- * @return string
- */
- public function encrypt($plainText, callable $callback = null) {
- $iv = openssl_random_pseudo_bytes($this->ivlen, $result);
- if (!$result) {
- throw new \Exception('random bytes error');
- }
- $tag = null;
- $data = openssl_encrypt($plainText, $this->cipher, $this->key, OPENSSL_RAW_DATA, $iv, $tag);
- $data = $iv.$data.$tag;
- if (!is_null($callback)) {
- return call_user_func($callback, $data);
- }
- // use the base64_encode as default method to return
- return rtrim(base64_encode($data), "=");
- }
- }
|