watermark->appid != self::$appid ) { return self::$IllegalBuffer; } $data = $result[1]; return self::$OK; } /** * 对需要加密的明文进行填充补位 * @param $text 需要进行填充补位操作的明文 * @return 补齐明文字符串 */ static function encode( $text ) { $text_length = strlen( $text ); //计算需要填充的位数 $amount_to_pad = self::$block_size - ( $text_length % self::$block_size ); if ( $amount_to_pad == 0 ) { $amount_to_pad = self::$block_size; } //获得补位所用的字符 $pad_chr = chr( $amount_to_pad ); $tmp = ""; for ( $index = 0; $index < $amount_to_pad; $index++ ) { $tmp .= $pad_chr; } return $text . $tmp; } /** * 对解密后的明文进行补位删除 * @param decrypted 解密后的明文 * @return 删除填充补位后的明文 */ static function decode($text) { $pad = ord(substr($text, -1)); if ($pad < 1 || $pad > 32) { $pad = 0; } return substr($text, 0, (strlen($text) - $pad)); } /** * 对密文进行解密 * @param string $aesCipher 需要解密的密文 * @param string $aesIV 解密的初始向量 * @return string 解密得到的明文 */ static function decrypt( $aesCipher, $aesIV ,$key) { try { $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, ''); mcrypt_generic_init($module, $key, $aesIV); //解密 $decrypted = mdecrypt_generic($module, $aesCipher); mcrypt_generic_deinit($module); mcrypt_module_close($module); } catch (Exception $e) { return array(self::$IllegalBuffer, null); } try { //去除补位字符 $result = self::decode($decrypted); } catch (Exception $e) { //print $e; return array(self::$IllegalBuffer, null); } return array(0, $result); } }