public
Last active

Great way to encrypt and decrypt files with AES-256 with CBC mode. Made for Kohana framework (controller).

  • Download Gist
gistfile1.aw
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
<?php defined('SYSPATH') or die('No direct script access.');
 
class Controller_Filemanager extends Controller {
 
private $file = '/var/www/assets/img/big/IMG_2083.JPG';
private $new_file = '/var/www/assets/encrypt/';
private $key = 'your key';
private $cipher = MCRYPT_RIJNDAEL_256;
private $mode = MCRYPT_MODE_CBC;
 
public function action_encrypt()
{
//$finfo = finfo_open(FILEINFO_MIME_TYPE);
//$mime_type = finfo_file($finfo, $file);
//$read_file = readfile($file);
$file_content = file_get_contents($this->file);
$class_encrypt = new Encrypt($this->key, $this->mode, $this->cipher);
$encrypt_data = $class_encrypt->encode($file_content);
$handle = fopen($this->new_file.basename($this->file).'.enc', 'w') or die("Can't open file.");
fwrite($handle, $encrypt_data);
fclose($handle);
exit;
}
 
public function action_decrypt()
{
$file_content = file_get_contents($this->new_file.basename($this->file).'.enc', 'r');
$class_encrypt = new Encrypt($this->key, $this->mode, $this->cipher);
$decrypt_data = $class_encrypt->decode($file_content);
header('Content-Type: image/jpeg');
print_r($decrypt_data);
exit;
}
 
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.