Welcome Guest, Not a member yet? Register   Sign In
I need to encrypt with Blowfish using ECB mode and PKCS#5 (PKCS5 Padding) - how to do it?
#1

[eluser]takasia[/eluser]
I know how to set the mode - but I have a problem with the padding.
Can I do this with CI encryption library?

If yes - how?
If no - what are other options? I need to (and really want to!) do it with CI though - so it would be great if just some simple changes in the library would do the trick. :-)

I would really love it if someone would share with me some samples - I looked over the internet, but couldn't find anything helpful.

Thanks in advance!

BTW - I know it's not the best method, but I don't have any choice and cannot decide in this case what mode I will use etc... :-(
#2

[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




Theme © iAndrew 2016 - Forum software by © MyBB