home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 April / CMCD0404.ISO / Software / Freeware / Programare / groupoffice-com-2.01 / classes / crypto.class.inc < prev    next >
Text File  |  2004-03-08  |  3KB  |  114 lines

  1. <?php
  2. /*
  3. Copyright Intermesh 2003
  4. Author: Merijn Schering <mschering@intermesh.nl>
  5. Version: 1.0 Release date: 08 July 2003
  6.  
  7. This program is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2 of the License, or (at your
  10. option) any later version.
  11. */
  12. class GO_CRYPTO extends db
  13. {
  14.     var $time="";
  15.     var $cryptokey;
  16.     var $varcryptokey;
  17.  
  18.     function GO_CRYPTO(){
  19.         global $GO_CONFIG;
  20.                 $this->db();
  21.         $this->time="1073437878";
  22.         $this->cryptokey='secret';
  23.         $this->varcryptokey='secret';
  24.     }
  25.  
  26.     function set_cryptokey(){
  27.         $this->cryptokey=$_SESSION['GO_SESSION']['password'];
  28.         return $this->cryptokey;
  29.     }
  30.  
  31.   function bytexor($a,$b)
  32.   {
  33.     $c="";
  34.     for($i=0;$i<strlen($a);$i++) {
  35.     $a{$i}= isset($a{$i}) ?  $a{$i} : null;
  36.     $b{$i}= isset($b{$i}) ?  $b{$i} : null;
  37.       $c.=$a{$i}^$b{$i};
  38.     }
  39.     return($c);
  40.   }
  41.  
  42.   function binmd5($val)
  43.   {
  44.     return(pack("H*",md5($val)));
  45.   }
  46.  
  47.   function decrypt_md5($msg,$heslo,$time)
  48.   {
  49.     $key=$heslo.$time;$sifra="";
  50.     $key1=$this->binmd5($key);
  51.     while($msg) {
  52.       $m=substr($msg,0,16);
  53.       $msg=substr($msg,16);
  54.       $sifra.=$m=$this->bytexor($m,$key1);
  55.       $key1=$this->binmd5($key.$key1.$m);
  56.     }
  57.     return($sifra);
  58.   }
  59.  
  60.   function crypt_md5($msg,$heslo,$time)
  61.   {
  62.     $key=$heslo.$time;$sifra="";
  63.     $key1=$this->binmd5($key);
  64.     while($msg) {
  65.       $m=substr($msg,0,16);
  66.       $msg=substr($msg,16);
  67.       $sifra.=$this->bytexor($m,$key1);
  68.       $key1=$this->binmd5($key.$key1.$m);
  69.     }
  70.     return($sifra);
  71.   }
  72.  
  73.  
  74.   function encrypt($text,$cryptokey=null){
  75.     $this->set_cryptokey();
  76.     if ($cryptokey==null) {$cryptokey=$this->cryptokey;}
  77.     #echo "encrypting with passphrase: $cryptokey!!!<br>";
  78.     $key="12345678";
  79.         $base64="base64_encode";
  80.         $url="urlencode";
  81.       $fce="\$this->crypt_md5";
  82.         return $url($base64($this->crypt_md5($text,$cryptokey,$this->time)));
  83.   }
  84.  
  85.   function decrypt($text,$cryptokey=null){
  86.     $this->set_cryptokey();
  87.     if ($cryptokey==null) {$cryptokey=$this->cryptokey;}
  88.     #echo "decrypting with passphrase: $cryptokey!!!<br>";
  89.     $key="12345678";
  90.     $fce="\$this->decrypt_md5";
  91.         $base64="base64_decode";
  92.         $url="urldecode";
  93.         return $this->decrypt_md5($base64($url($text)),$cryptokey,$this->time);
  94.   }
  95.  
  96.  
  97.  
  98.   function re_encrypt_email($user_id,$old_password,$password){
  99.     $sql_accounts="select id,password from emAccounts where user_id='$user_id'";
  100.     $this->query($sql_accounts);
  101.     $that=new db();
  102.     while($this->next_record()){
  103.         $old_email_encrypted_password=$this->f('password');
  104.         $old_email_password=$this->decrypt($old_email_encrypted_password,$old_password);
  105.         $email_password=$this->encrypt($old_email_password,$password);
  106.         $id=$this->f('id');
  107.         $sql="update emAccounts set password='$email_password' where user_id='$user_id' and id='$id'";
  108.         $that->query($sql);
  109.     }
  110.  
  111.  
  112.   }
  113. }
  114.