includes/rijndael.php
changeset 286 b2f985e4cef3
parent 54 84b56303cab5
child 345 4ccdfeee9a11
child 440 105457df35e5
equal deleted inserted replaced
285:7846d45bd250 286:b2f985e4cef3
    14 
    14 
    15 define ('ENC_HEX', 201);
    15 define ('ENC_HEX', 201);
    16 define ('ENC_BASE64', 202);
    16 define ('ENC_BASE64', 202);
    17 define ('ENC_BINARY', 203);
    17 define ('ENC_BINARY', 203);
    18 
    18 
       
    19 $_aes_objcache = array();
       
    20 
    19 class AESCrypt {
    21 class AESCrypt {
    20   
    22   
    21   var $debug = false;
    23   var $debug = false;
    22   var $mcrypt = false;
    24   var $mcrypt = false;
       
    25   var $decrypt_cache = array();
    23 
    26 
    24   // Rijndael parameters --  Valid values are 128, 192, or 256
    27   // Rijndael parameters --  Valid values are 128, 192, or 256
    25   
    28   
    26   var $keySizeInBits = 128;
    29   var $keySizeInBits = 128;
    27   var $blockSizeInBits = 128;
    30   var $blockSizeInBits = 128;
   126     $this->Nb = $this->blockSizeInBits / 32;
   129     $this->Nb = $this->blockSizeInBits / 32;
   127     $this->Nr = $this->roundsArray[$this->Nk][$this->Nb];
   130     $this->Nr = $this->roundsArray[$this->Nk][$this->Nb];
   128     $this->debug = $debug;
   131     $this->debug = $debug;
   129   }
   132   }
   130   
   133   
       
   134   function singleton($key_size, $block_size)
       
   135   {
       
   136     global $_aes_objcache;
       
   137     if ( isset($_aes_objcache["$key_size,$block_size"]) )
       
   138     {
       
   139       return $_aes_objcache["$key_size,$block_size"];
       
   140     }
       
   141     
       
   142     $_aes_objcache["$key_size,$block_size"] = new AESCrypt($key_size, $block_size);
       
   143     return $_aes_objcache["$key_size,$block_size"];
       
   144   }
       
   145   
   131   // Error handler
   146   // Error handler
   132   
   147   
   133   function trigger_error($text, $level = E_USER_NOTICE)
   148   function trigger_error($text, $level = E_USER_NOTICE)
   134   {
   149   {
   135     $bt = debug_backtrace();
   150     $bt = debug_backtrace();
   802    
   817    
   803   function decrypt($text, $key, $input_encoding = ENC_HEX)
   818   function decrypt($text, $key, $input_encoding = ENC_HEX)
   804   {
   819   {
   805     if ( $text == '' )
   820     if ( $text == '' )
   806       return '';
   821       return '';
       
   822     $text_orig = $text;
       
   823     if ( isset($this->decrypt_cache[$key]) && is_array($this->decrypt_cache[$key]) )
       
   824     {
       
   825       if ( isset($this->decrypt_cache[$key][$text]) )
       
   826       {
       
   827         return $this->decrypt_cache[$key][$text];
       
   828       }
       
   829     }
   807     switch($input_encoding)
   830     switch($input_encoding)
   808     {
   831     {
   809       case ENC_BINARY:
   832       case ENC_BINARY:
   810       default:
   833       default:
   811         break;
   834         break;
   836         echo '<pre>'.print_r($dypt, true).'</pre>';
   859         echo '<pre>'.print_r($dypt, true).'</pre>';
   837         $this->trigger_error('Rijndael main decryption routine failed', E_USER_ERROR);
   860         $this->trigger_error('Rijndael main decryption routine failed', E_USER_ERROR);
   838       }
   861       }
   839       $dypt = $this->byteArrayToString($dypt);
   862       $dypt = $this->byteArrayToString($dypt);
   840     }
   863     }
       
   864     if ( !isset($this->decrypt_cache[$key]) )
       
   865       $this->decrypt_cache[$key] = array();
       
   866     
       
   867     $this->decrypt_cache[$key][$text_orig] = $dypt;
       
   868     
   841     return $dypt;
   869     return $dypt;
   842   }
   870   }
   843   
   871   
   844   /**
   872   /**
   845    * Enano-ese equivalent of str_split() which is only found in PHP5
   873    * Enano-ese equivalent of str_split() which is only found in PHP5