Re[2]: mcrypt_generic_init(): Iv size incorrect

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

Wednesday, April 13, 2005, 5:14:25 AM, you wrote:
MH> Do you do this on the encryption or the decryption section or both?

I do it on both, here is a class I use that may help:


class encryptClass{
  var $secret;
  function encryptClass(){
    $this->secret = 'put secret here';
  }
  Function encode($id){
    $eid = $iv = 0;
    $len = strlen($id);
    $id = $len.'-'.$id;
    $td = mcrypt_module_open(MCRYPT_TripleDES, "", MCRYPT_MODE_ECB, "");
    $key = substr($this->secret, 0, mcrypt_enc_get_key_size ($td));
    $iv = pack("a".mcrypt_enc_get_iv_size($td),$iv);
    mcrypt_generic_init ($td, $key, $iv);
    $eid = base64_encode(mcrypt_generic ($td, $id));
    mcrypt_generic_deinit($td);
    return $eid;
  }
  Function decode($eid){
    $id = $iv = 0;
    $td = mcrypt_module_open (MCRYPT_TripleDES, "", MCRYPT_MODE_ECB, "");
    $key = substr($this->secret, 0, mcrypt_enc_get_key_size ($td));
    $iv = pack("a".mcrypt_enc_get_iv_size($td),$iv);
    mcrypt_generic_init ($td, $key, $iv);
    $id = mdecrypt_generic ($td, base64_decode($eid));
    $len = strtok($id,'-');
    $id = substr($id,(strlen($len)+1),$len);
    mcrypt_generic_deinit($td);
    return $id;
  }
}


( I encode the length of the original string so I can trim the extra
rubbish that deinit leaves behind as packing)

-- 
regards,
Tom

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux