Re: Help with encryption

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

 



Hi,

Friday, January 14, 2005, 7:53:30 AM, you wrote:
BD> Howdy all -

BD> I have RTFM and STFW and I still can't get encryption to work. What I 
BD> finally ended up with from the PHP documentation is long, unwieldy, 
BD> confusing, and doesn't work. I give up. I threw my big mess away and 
BD> would like to start from scratch.

BD> Could anyone point me to a web page or other documentation that shows a 
BD> SIMPLE example of encryption? I need two-way encryption & decryption, 
BD> not a one-way hash. I'll be using this to obfuscate get parameters.

BD> Any pointers appreciated. Thanks all,

BD> - Brian

Here is a class that uses mcrypt that might be helpful:


class encrypt_class{
  var $secret;
  function encrypt_class(){
    $this->secret = 'put pass string here';
  }
  Function encode($id){
    $eid = $iv = 0;
    $len = strlen($id);
    $id = $len.'-'.$id; //encode the string length for trimming later
    $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;
  }
}

//useage
$str = "Hello World";
$enc = new encrypt_class();
$estr = $enc->encode($str);
echo "$estr<br>";
$dstr = $enc->decode($estr);
echo "$dstr<br>";

-- 
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