[eluser]takasia[/eluser]
Some php way to do this:
Code:
function encrypt($key , $text){
$size = mcrypt_get_block_size('blowfish', 'ecb');
$input = $this->pkcs5_pad($text, $size);
$td = mcrypt_module_open('blowfish', '', 'ecb', '');
$iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
mcrypt_generic_init($td, $key , $iv);
$data = mcrypt_generic($td, $input);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
$data = bin2hex($data);
return $data;
}
function decrypt($key , $text){
$td = mcrypt_module_open('blowfish', '', 'ecb', '');
$data = pack("H*" , $text);
$iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
mcrypt_generic_init($td, $key , $iv);
$data = mdecrypt_generic($td, $data);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
$data = $this->pkcs5_unpad($data);
return $data;
}
function pkcs5_pad($string)
{
$blocksize = mcrypt_get_block_size('blowfish', 'ecb');
$pad = $blocksize - (mb_strlen($string) % $blocksize);
return $string . str_repeat(chr($pad), $pad);
}
function pkcs5_unpad($string)
{
$pad = ord($string{strlen($string)-1});
if ($pad > strlen($string)) return false;
if (strspn($string, chr($pad), strlen($string) - $pad) != $pad) return false;
return substr($string, 0, -1 * $pad);
}
All four functions in a model and then, just
Code:
$encrypted = $this->the_model->encrypt($key , $sting);
$this->the_model->decrypt($key , $encrypted);
in the controller.
But still, would like it a little more CI-like :-P