[ Index ]

PHP Cross Reference of Joomla 1.5.26 DE

title

Body

[close]

/libraries/joomla/utilities/ -> simplecrypt.php (source)

   1  <?php
   2  /**
   3   * @version        $Id: simplecrypt.php 14401 2010-01-26 14:10:00Z louis $
   4   * @package        Joomla.Framework
   5   * @subpackage    Utilities
   6   * @copyright    Copyright (C) 2005 - 2010 Open Source Matters. All rights reserved.
   7   * @license        GNU/GPL, see LICENSE.php
   8   * Joomla! is free software. This version may have been modified pursuant to the
   9   * GNU General Public License, and as distributed it includes or is derivative
  10   * of works licensed under the GNU General Public License or other free or open
  11   * source software licenses. See COPYRIGHT.php for copyright notices and
  12   * details.
  13   */
  14  
  15  // Check to ensure this file is within the rest of the framework
  16  defined('JPATH_BASE') or die();
  17  
  18  /**
  19   * JSimpleCrypt is a very simple encryption algorithm for encyrpting/decrypting strings
  20   *
  21   * @static
  22   * @package     Joomla.Framework
  23   * @subpackage    Utilities
  24   * @since        1.5
  25   */
  26  class JSimpleCrypt extends JObject
  27  {
  28      /**
  29       * Encryption/Decryption Key
  30       * @access    private
  31       * @var        string
  32       */
  33      var $_key;
  34  
  35      /**
  36       * Object Constructor takes an optional key to be used for encryption/decryption.  If no key is given then the
  37       * secret word from the configuration object is used.
  38       *
  39       * @access    protected
  40       * @param    string    $key    Optional encryption key
  41       * @return    void
  42       * @since    1.5
  43       */
  44  	function __construct($key = null)
  45      {
  46          if ($key) {
  47              $this->_key = (string) $key;
  48          } else {
  49              $conf = &JFactory::getConfig();
  50              $this->_key = md5($conf->getValue('config.secret'));
  51          }
  52      }
  53  
  54  	function decrypt($s)
  55      {
  56          $ai = $this->_hexToIntArray($s);
  57          (string) $s1 = $this->_xorString($ai);
  58          return $s1;
  59      }
  60  
  61  	function encrypt($s)
  62      {
  63          $ai = $this->_xorCharString($s);
  64          $s1 = "";
  65          for ($i = 0; $i < count($ai); $i++)
  66              $s1 = $s1 . $this->_intToHex((int) $ai[$i]);
  67          return $s1;
  68      }
  69  
  70  	function _hexToInt($s, $i)
  71      {
  72          (int) $j = $i * 2;
  73          (string) $s1 = $s;
  74          (string) $c = substr($s1, $j, 1); // get the char at position $j, length 1
  75          (string) $c1 = substr($s1, $j +1, 1); // get the char at postion $j + 1, length 1
  76          (int) $k = 0;
  77  
  78          switch ($c)
  79          {
  80              case "A" :
  81                  $k += 160;
  82                  break;
  83              case "B" :
  84                  $k += 176;
  85                  break;
  86              case "C" :
  87                  $k += 192;
  88                  break;
  89              case "D" :
  90                  $k += 208;
  91                  break;
  92              case "E" :
  93                  $k += 224;
  94                  break;
  95              case "F" :
  96                  $k += 240;
  97                  break;
  98              case " " :
  99                  $k += 0;
 100                  break;
 101              default :
 102                  (int) $k = $k + (16 * (int) $c);
 103                  break;
 104          }
 105  
 106          switch ($c1)
 107          {
 108              case "A" :
 109                  $k += 10;
 110                  break;
 111              case "B" :
 112                  $k += 11;
 113                  break;
 114              case "C" :
 115                  $k += 12;
 116                  break;
 117              case "D" :
 118                  $k += 13;
 119                  break;
 120              case "E" :
 121                  $k += 14;
 122                  break;
 123              case "F" :
 124                  $k += 15;
 125                  break;
 126              case " " :
 127                  $k += 0;
 128                  break;
 129              default :
 130                  $k += (int) $c1;
 131                  break;
 132          }
 133  
 134          return $k;
 135      }
 136  
 137  	function _hexToIntArray($s)
 138      {
 139          (string) $s1 = $s;
 140          (int) $i = strlen($s1);
 141          (int) $j = $i / 2;
 142          for ($l = 0; $l < $j; $l++) {
 143              (int) $k = $this->_hexToInt($s1, $l);
 144              $ai[$l] = $k;
 145          }
 146  
 147          return $ai;
 148      }
 149  
 150  	function _charToInt($c)
 151      {
 152          $ac[0] = $c;
 153          return $ac;
 154      }
 155  
 156  	function _xorString($ai)
 157      {
 158          $s = $this->_key; //
 159          (int) $i = strlen($s);
 160          $ai1 = $ai;
 161          (int) $j = count($ai1);
 162          for ($i = 0; $i < $j; $i = strlen($s))
 163              $s = $s . $s;
 164  
 165          for ($k = 0; $k < $j; $k++) {
 166              (string) $c = substr($s, $k, 1);
 167              $ac[$k] = chr($ai1[$k] ^ ord($c));
 168          }
 169  
 170          (string) $s1 = implode('', $ac);
 171          return $s1;
 172      }
 173  
 174  	function _intToHex($i)
 175      {
 176          (int) $j = (int) $i / 16;
 177          if ((int) $j == 0) {
 178              (string) $s = " ";
 179          } else {
 180              (string) $s = strtoupper(dechex($j));
 181          }
 182          (int) $k = (int) $i - (int) $j * 16;
 183          (string) $s = $s . strtoupper(dechex($k));
 184  
 185          return $s;
 186      }
 187  
 188  	function _xorCharString($s)
 189      {
 190          $ac = preg_split('//', $s, -1, PREG_SPLIT_NO_EMPTY);
 191          (string) $s1 = $this->_key;
 192          (int) $i = strlen($s1);
 193          (int) $j = count($ac);
 194          for ($i = 0; $i < $j; $i = strlen($s1)) {
 195              $s1 = $s1 . $s1;
 196          }
 197  
 198          for ($k = 0; $k < $j; $k++) {
 199              $c = substr($s1, $k, 1);
 200              $ai[$k] = ord($c) ^ ord($ac[$k]);
 201          }
 202  
 203          return $ai;
 204      }
 205  }


Generated: Wed Mar 28 15:54:07 2012 Cross-referenced by PHPXref 0.7.1