Skip to content

Instantly share code, notes, and snippets.

@jnbdz
Created November 21, 2011 22:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jnbdz/1384192 to your computer and use it in GitHub Desktop.
Save jnbdz/1384192 to your computer and use it in GitHub Desktop.
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);
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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment