Scripts

Encryption class

Ik had een methode nodig om data en files te encrypten zodat mensen die er geen gebruik van mochten maken er niets mee konden doen, maar ik vond om steeds maar weer de mcrypt functies te gebruiken te omslachtig. Daarom heb ik een class gemaakt die alles heel makkelijk maakt, hou er wel rekening mee dat je mcrypt enabled moet hebben. Oja je hebt zowel de iv en de key nodig om te decrypten.

encryption-class
<?php

class secure_data
{

// this function is the constructor for our class.
function open_module(){
	
	$this->td = mcrypt_module_open('rijndael-256', '', 'ofb', '');
	$this->iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($this->td), MCRYPT_RAND);
	$this->iv = substr(md5($this->iv), 0, mcrypt_enc_get_iv_size($this->td));
	$this->ks = mcrypt_enc_get_key_size($this->td);
	
}


// this function encrypt's only input data
function encrypt($key,$data){
	
	$this->ori_data = $data;
	
	$this->key = substr(md5($key), 0, $this->ks);
	
	mcrypt_generic_init($this->td, $this->key, $this->iv);
	
	$this->data = mcrypt_generic($this->td, $data);
	
}


// this function encrypt's only files
function encrypt_file($key,$filename){
	
	$this->fp = fopen($filename,"r");
	
	$file_content ="";
	while (!feof($this->fp)){
		$file_content .= fread($this->fp, 8192);
	}
	
	$this->ori_data = $file_content;
		
	$this->key = substr(md5($key), 0, $this->ks);
	
	mcrypt_generic_init($this->td, $this->key, $this->iv);
	
	$this->data = mcrypt_generic($this->td, $file_content);
	
	
	$this->fpn = fopen($filename.".safe","w");
	
	fwrite($this->fpn,$this->data);
	
	
}


// decript data
function decrypt($key,$data,$iv){
	
	mcrypt_generic_init($this->td, $key, $iv);
	
	$this->data = mdecrypt_generic($this->td, $data);
	
}


// decrypt encrypted file
function decrypt_file($key,$filename,$iv){
	
	$this->fp = fopen($filename,"r");
	
	$file_content ="";
	while (!feof($this->fp)){
		$file_content .= fread($this->fp, 8192);
	}
	
	mcrypt_generic_init($this->td, $key, $iv);
	
	$this->data = mdecrypt_generic($this->td, $file_content);
	
	
	$filename = str_replace(".safe","",$filename);
	$this->fpn = fopen($filename,"w");
	
	fwrite($this->fpn,$this->data);
	
}


// verify's if the data original data is different from the encrypted data
function verify(){
	
	if($this->ori_data != $this->data){
		return true;
	}else{
		return false;
	}
	
}

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


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


// returns the key used for encryption
function get_key(){
	
	return $this->key;
	
}


// returns intilization vector
function get_iv(){
	
	return $this->iv;
	
}


// this returns current data
function get_data(){
	
	return $this->data;
	
}


// We dont need to close the module
function close_module(){
	
	mcrypt_generic_deinit($this->td);
	mcrypt_module_close($this->td);
	
}

// end class
}

/* this is just a example
$secure = new secure_data;
$secure->open_module();
$secure->encrypt_file("dunno","test.png");
$secure->decrypt_file($secure->get_key(),"test.png.safe",$secure->get_iv());
echo $secure->get_key();
echo $secure->get_iv();
$secure->close_module();
*/
?> 

Reacties

0
Nog geen reacties.