Location: PHPKode > scripts > EnDe > ende/class_ende.php
<?
/*
Ende - Simple text encryption and decryption
by Johan De Klerk
hide@address.com
*/

class EnDe {

	var $key;
	var $data;

	var $td;
	var $iv;
	var $init = false;

	function EnDe($key='',$data='') {
		if ($key != '') {
			$this->init($key);			
		}
		$this->data = $data;
	}

	function init(&$key) {
		$this->td = mcrypt_module_open ('des', '', 'ecb', ''); 
		$this->key = substr ($key, 0, mcrypt_enc_get_key_size ($this->td)); 
		$iv_size = mcrypt_enc_get_iv_size ($this->td); 
		$this->iv = mcrypt_create_iv ($iv_size, MCRYPT_RAND);
		$this->init = true;
	}

	function setKey($key) {
		$this->init($key);
	}

	function setData($data) {
		$this->data = $data;
	}

	function & getKey() {
		return $this->key;
	}

	function & getData() {
		return $this->data;
	}

	function & encrypt($data='') {
		return $this->_crypt('encrypt',$data);
	}

	function & decrypt($data='') {
		return $this->_crypt('decrypt',$data);
	}

	function close() {
		mcrypt_module_close($this->td); 
	}

	function & _crypt($mode,&$data) {
		if ($data != '') {
			$this->data = $data;
		}

		if ($this->init) {
			if (mcrypt_generic_init($this->td,$this->key,$this->iv) != -1) { 
				if ($mode == 'encrypt') {
					$this->data = mcrypt_generic($this->td, $this->data); 
				}
				elseif ($mode == 'decrypt') {
					$this->data = mdecrypt_generic($this->td, $this->data); 
				}

				mcrypt_generic_deinit($this->td);
				
				return $this->data;
			}
			else {
				trigger_error('Error initialising '.$mode.'ion handle',E_USER_ERROR);
			}
		}
		else {
			trigger_error('Key not set. Use setKey() method',E_USER_ERROR);
		}
	}
}
?>
Return current item: EnDe