Skip to content

Instantly share code, notes, and snippets.

@jaonoctus
Last active February 10, 2016 05:33
Show Gist options
  • Select an option

  • Save jaonoctus/8d6891cb7a4b889fce2b to your computer and use it in GitHub Desktop.

Select an option

Save jaonoctus/8d6891cb7a4b889fce2b to your computer and use it in GitHub Desktop.
Playing with CodeIgniters Encryptation Library and Encrypt Class
<?php
/**
* Encrypt Controller
*
* You can set the key to encode/decode.
*
* @uses CodeIgniter's Encrypt Class
* @see http://www.codeigniter.com/user_guide/libraries/encrypt.html
* @author John Dias <joaodias@noctus.org>
*/
class Encrypt extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->library('encrypt');
}
public function index()
{
$this->load->helper('form');
$post = $this->input->post();
$hash = $this->input->get('hash');
if ( ! empty($post['what']) AND ! empty($post['encrypt']) AND ! empty($post['chave'])) {
$hash = self::encrypt($post['what'], $post['chave']);
$what = '';
}
if ( ! empty($post['result']) AND ! empty($post['decrypt']) AND ! empty($post['chave'])) {
$what = self::decrypt($post['result'], $post['chave']);
$hash = '';
}
@$key = (isset($new_key)) ? $new_key : $post['chave'];
@$result = (isset($hash)) ? $hash : $post['result'];
@$what = (isset($what)) ? $what : $post['what'];
echo form_open('encrypt')
.form_textarea('what', $what, 'placeholder="Texto descriptografado"')
.form_textarea('result', $result, 'placeholder="Texto criptografado"')
.'<br/>'
.form_input('chave', $key, 'placeholder="Senha" size="24"')
.form_submit('encrypt', 'Criptografar')
.form_submit('decrypt', 'Descriptografar')
.form_close();
}
private function encrypt($what, $key)
{
return $this->encrypt->encode($what, $key);
}
private function decrypt($result, $key)
{
return $this->encrypt->decode($result, $key);
}
}
<?php
/**
* Encryptation Controller
*
* The key to encode/decode must be a hexadecimal string.
*
* @uses CodeIgniter's Encrypt Class
* @see http://www.codeigniter.com/user_guide/libraries/encrypt.html
* @author John Dias <joaodias@noctus.org>
*/
class Encryptation extends CI_Controller
{
public $params = array(
'cipher' => 'aes-128',
'mode' => 'cbc',
'hmac_digest' => 'sha512',
'hmac_key' => TRUE
);
public function __construct()
{
parent::__construct();
$this->load->library('encryption');
}
public function index()
{
$this->load->helper('form');
$post = $this->input->post();
$hash = $this->input->get('hash');
if ( ! empty($post['new_key'])) {
$new_key = self::new_key();
}
if ( ! empty($post['what']) AND ! empty($post['encrypt']) AND ! empty($post['chave']) AND ctype_xdigit($post['chave'])) {
$hash = self::encrypt($post['what'], $post['chave']);
$what = '';
}
if ( ! empty($post['result']) AND ! empty($post['decrypt']) AND ! empty($post['chave']) AND ctype_xdigit($post['chave'])) {
$what = self::decrypt($post['result'], $post['chave']);
$hash = '';
}
@$key = (isset($new_key)) ? $new_key : $post['chave'];
//@$result = (isset($result)) ? $result : $post['result'];
@$result = (isset($hash)) ? $hash : $post['result'];
@$what = (isset($what)) ? $what : $post['what'];
echo form_open('encryptation')
.form_textarea('what', $what, 'placeholder="Texto descriptografado"')
.form_textarea('result', $result, 'placeholder="Texto criptografado"')
.'<br/>'
.form_input('chave', $key, 'placeholder="Chave" size="24"')
.form_submit('new_key', 'Gerar chave')
.form_submit('encrypt', 'Criptografar')
.form_submit('decrypt', 'Descriptografar')
.form_close();
}
private function new_key()
{
return bin2hex($this->encryption->create_key(16));
}
private function encrypt($what, $key)
{
$this->params['key'] = hex2bin($key);
return bin2hex($this->encryption->encrypt($what, $this->params));
}
private function decrypt($result, $key)
{
$this->params['key'] = hex2bin($key);
return $this->encryption->decrypt(hex2bin($result), $this->params);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment