Source for file Decrypt.class.php

Documentation is available at Decrypt.class.php

  1. <?php
  2. /**
  3.  * Gumbo Library Framework
  4.  *
  5.  * LICENSE
  6.  * This library is being released under the terms of the New BSD License.  A
  7.  * copy of the license is packaged with the software (LICENSE.txt).  If no
  8.  * copy is found, a copy of the license template can be found at:
  9.  * http://www.opensource.org/licenses/bsd-license.php
  10.  * 
  11.  * @category Gumbo
  12.  * @package Encryption
  13.  * @copyright Copyright (c) 2007, iBayou, Michael Luster
  14.  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  15.  * @author Michael Luster <mluster79@yahoo.com>
  16.  * @link http://sourceforge.net/projects/phpgumbo
  17.  * @version 0.0.1
  18.  */
  19.  
  20. /**
  21.  * Decrypt Class
  22.  *
  23.  * @category Gumbo
  24.  * @package Encryption
  25.  * @copyright Copyright (c) 2007, iBayou, Michael Luster
  26.  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  27.  * @author Michael Luster <mluster79@yahoo.com>
  28.  * @link http://sourceforge.net/projects/phpgumbo
  29.  * @desc Decrypt Class
  30.  * @version 0.0.1
  31.  */
  32.  
  33. gumbo_load ("Encryption");
  34.  
  35. class Gumbo_Decrypt extends Gumbo_Encryption {
  36.     
  37.     /**
  38.      * Constructor
  39.      * @param string $data 
  40.      * @param string $key 
  41.      * @param int $cipher 
  42.      * @param int $mode 
  43.      * @param bool $encode 
  44.      */
  45.     public function __construct ($data=null$key=null$cipher=null$mode=null$encode=null{
  46.         if (!is_null ($data)) $this->setData ($data)}
  47.         $this->setKey ($key);
  48.         $this->setCipher ($cipher);
  49.         $this->setMode ($mode);
  50.         $this->setEncode ($encode);
  51.     }
  52.     
  53.     
  54.     
  55.     /** ACTION METHODS **/
  56.     /**
  57.      * Parses the data by decryption
  58.      * @return string 
  59.      * @throws Gumbo_Exception
  60.      */
  61.     public function parse ({
  62.         try {
  63.             // verify precondition
  64.             if (!$this->getData ()) {
  65.                 throw new Gumbo_Exception ("No Data to DECRYPT");
  66.             }
  67.             
  68.             if ($this->isRaw ()) {
  69.                 $data $this->getData ();
  70.                 if ($this->toEncode ()) {
  71.                     $cryptcode false;
  72.                     for ($x 0;$x strlen ($data);$x += 2{
  73.                         $cryptcode .= chr (hexdec (substr ($data$x2)));
  74.                     }
  75.                     $data $cryptcode;
  76.                 }
  77.                 
  78.                 // set IV
  79.                 $iv_size mcrypt_get_iv_size ($this->getCipher ()$this->getMode ());
  80.                 $iv null;
  81.                 for ($x 0;$x $iv_size;$x++$iv .= "a"}
  82.                 
  83.                 $this->setData (trim (mcrypt_decrypt ($this->getCipher ()$this->getKey ()$data$this->getMode ()$iv)));
  84.                 $this->setRaw (false);
  85.             }
  86.         catch (Gumbo_Exception $e{
  87.             $e->setFunction (__METHOD__);
  88.             gumbo_trigger ($e);
  89.         }
  90.         return $this->getData ();
  91.     }
  92.     
  93. }
  94.  
  95. ?>