error = '未启用 openssl 扩展,请开启。'; } $this->ivStatus = $ivStatus; $this->key = $key; } /** * 加密 */ public function encrypt($data) { $str_padded = json_encode($data); if (strlen($str_padded) % 16) { $str_padded = str_pad($str_padded, strlen($str_padded) + 16 - strlen($str_padded) % 16, "\0"); } if ($this->ivStatus) { $iv = $this->makeIv(); $code = openssl_encrypt($str_padded, SELF::METHOD, $this->key, OPENSSL_NO_PADDING, $iv); return [ 'iv' => base64_encode($iv), 'code' => base64_encode($code) ]; } else { $code = openssl_encrypt($str_padded, SELF::METHOD, $this->key, OPENSSL_NO_PADDING); return base64_encode($code); } } /** * 解密 */ public function decrypt($code, $iv = '') { $code = base64_decode($code); $iv = base64_decode($iv); $res = openssl_decrypt($code, SELF::METHOD, $this->key, OPENSSL_NO_PADDING, $iv); return json_decode(trim($res), true); } /** * 生成 初始化向量 iv */ public function makeIv() { $ivlen = openssl_cipher_iv_length(SELF::METHOD); return openssl_random_pseudo_bytes($ivlen); } }