Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Great way to encrypt and decrypt files with AES-256 with CBC mode. Made for Kohana framework (controller).
<?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);
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');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment