<?php
/* itcode 2007 */

class cripta{

	var $key, $td, $iv_size;
	
	function cripta(){
		srand((double) microtime() * 1000000);
		$this->key = 'farmacia internacional encripta dados 129845';
		$this->key = md5($this->key);
		$this->td = mcrypt_module_open('rijndael-128', '','cfb', '');
		//$this->td = mcrypt_module_open('tripledes', '','ecb', '');
		
		$this->key = substr($this->key, 0, mcrypt_enc_get_key_size($this->td));
		$this->iv_size = mcrypt_enc_get_iv_size($this->td);	
	}


	function enc($string) {
		if (empty($string)) return false;
		
		$iv = mcrypt_create_iv($this->iv_size, MCRYPT_RAND);
		if (mcrypt_generic_init($this->td, $this->key, $iv) != 	-1) {
			$c_t = mcrypt_generic($this->td, $string);
			mcrypt_generic_deinit($this->td);
			$c_t = $iv.$c_t;
			return $c_t;
		}
		return false;
	}

	function dec($string) {
		if (empty($string)) return false;
			
		$iv = substr($string,0,$this->iv_size);
		$string = substr($string,$this->iv_size);
		if (mcrypt_generic_init($this->td, $this->key, $iv) != -1) {
			$c_t = mdecrypt_generic($this->td, $string);
			mcrypt_generic_deinit($this->td);
			return $c_t;
		} 
		return false;
	}
	
	function close(){
		mcrypt_module_close($this->td);
	}
	
	function encLista($arr, $campos){
		foreach ($arr as $key => $value) {
			if (!in_array($key, $campos)) continue;
			//if (is_numeric($value)) continue;
			if (empty($value)) continue;
		
			$arr[$key]=$this->enc($value);
			//print $value ."=". $arr[$key] ."<br>";
		}
		return $arr;
	}
	
	function decLista($arr, $campos){
		foreach ($arr as $key => $value) {
			if (!in_array($key, $campos)) continue;
			//if (is_numeric($value)) continue;
			if (empty($value)) continue;
			$arr[$key]=$this->dec($value);
			//print $value ."=". $arr[$key] ."<br>";
		}
		return $arr;
	}
}
?>