secret = $secret; } public function GetMagEncoder($filename) { if (file_exists($filename)) { $fp = fopen($filename, "r"); $this->json_encode = fgets($fp); fclose($fp); if ($this->json_encode == "") return null; $this->rec_encode = json_decode($this->json_encode, TRUE); $ret = explode($this->deli, $this->decrypt($this->rec_encode['sys_info'])); $this->rec_ret['ip'] = $ret[0]; $this->rec_ret['time'] = $ret[1]; $this->rec_ret['msg'] = $ret[2]; return ($this->rec_ret); } return null; } public function PutMagEncoder($filename, $ip, $time, $msg) { $this->rec_encode['sys_info'] = $this->encrypt($ip.$this->deli.$time.$this->deli.$msg); $this->json_encode = json_encode($this->rec_encode); $fp = fopen($filename, "w"); fwrite($fp, $this->json_encode); fclose($fp); } public function decrypt($hex_buf) { $key = $this->secret; $len = strlen($hex_buf); for ($i=0; $i<$len; $i+=2) $buf .= chr(hexdec(substr($hex_buf, $i, 2))); $key1 = pack("H*", md5($key)); while($buf) { $m = substr($buf, 0, 16); $buf = substr($buf, 16); $c = ""; for($i=0;$i<16;$i++) { $c .= $m{$i}^$key1{$i}; } $ret_buf .= $m = $c; $key1 = pack("H*",md5($key.$key1.$m)); } return($ret_buf); } function encrypt($buf) { $key = $this->secret; $key1 = pack("H*",md5($key)); while($buf) { $m = substr($buf, 0, 16); $buf = substr($buf, 16); $c = ""; for($i=0;$i<16;$i++) { $c .= $m{$i}^$key1{$i}; } $ret_buf .= $c; $key1 = pack("H*",md5($key.$key1.$m)); } $len = strlen($ret_buf); for($i=0; $i<$len; $i++) $hex_data .= sprintf("%02x", ord(substr($ret_buf, $i, 1))); return($hex_data); } public function base64encode($string) { $data = str_replace(array('+','/','='),array('-','_',''),base64_encode($string)); return $data; } public function base64decode($string) { $data = str_replace(array('-','_'),array('+','/'),$string); $mod4 = strlen($data) % 4; if ($mod4) { $data .= substr('====', $mod4); } return base64_decode($data); } } //$test = new MagEncoder("test"); //$data = "허윤영 test is test1234"; //$enc_data = $test->encrypt($data); //echo "$data -> ".$enc_data."\n"; //$de_data = $test->decrypt($enc_data); //echo "$enc_data -> ".$de_data."\n"; // 생성시.. //$test->PutMagEncoder("test.txt", "10", "2017-12-31 23:59:59", "데모용입니다."); // 테스트 시.. //$ret = $test->GetMagEncoder("test.txt"); //print_r($ret); ?>