home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / DB / Sqlite / Tools / ArcFour.php next >
Encoding:
PHP Script  |  2008-07-02  |  5.2 KB  |  220 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */
  3.  
  4. // {{{ Header
  5.  
  6. /**
  7.  * ArcFour stream cipher routines implementation PHP.
  8.  *
  9.  * PHP version 5
  10.  *
  11.  * LICENSE:
  12.  *
  13.  * The PHP License, version 2.0
  14.  *
  15.  * Copyright (c) 1997-2003 The PHP Group
  16.  *
  17.  * This source file is subject to version 2.0 of the PHP license,
  18.  * that is bundled with this package in the file LICENSE, and is
  19.  * available at through the world-wide-web at
  20.  * http://www.php.net/license/2_02.txt.
  21.  * If you did not receive a copy of the PHP license and are unable to
  22.  * obtain it through the world-wide-web, please send a note to
  23.  * license@php.net so we can mail you a copy immediately.
  24.  *
  25.  * @category Crypt
  26.  * @package DB_Sqlite_Tools
  27.  * @author Dave Mertens <dmertens@zyprexia.com>
  28.  * @author David Costa <gurugeek@php.net>
  29.  * @copyright Copyright (c) 1997-2003 The PHP Group
  30.  * @license http://www.php.net/license/2_02.txt
  31.  *          The PHP License, version 2.0
  32.  * @version CVS: $Id: ArcFour.php,v 1.1 2006/05/20 18:50:36 firman Exp $
  33.  */
  34.  
  35. // }}}
  36. // {{{ Class: DB_Sqlite_Tools_ArcFour
  37.  
  38. /**
  39.  * ArcFour stream cipher routines class.
  40.  *
  41.  * ArcFour stream cipher routines implementation in PHP written by
  42.  * Dave Mertens <dmertens@zyprexia.com> based on code written by
  43.  * Damien Miller <djm@mindrot.org> ported from PEAR::RC4 to PHP 5 by
  44.  * David Costa <gurugeek@php.net>.
  45.  *
  46.  * Usage:
  47.  * <pre>
  48.  * $key = "pear";
  49.  * $message = "PEAR rulez!";
  50.  *
  51.  * $rc4 = new Crypt_RC4;
  52.  * $rc4->key($key);
  53.  * echo "Original message: $message <br>\n";
  54.  * $rc4->crypt($message);
  55.  * echo "Encrypted message: $message <br>\n";
  56.  * $rc4->decrypt($message);
  57.  * echo "Decrypted message: $message <br>\n";
  58.  * </pre>
  59.  *
  60.  * @category Crypt
  61.  * @package DB_Sqlite_Tools
  62.  * @author Dave Mertens <dmertens@zyprexia.com>
  63.  * @author David Costa <gurugeek@php.net>
  64.  * @copyright Copyright (c) 1997-2003 The PHP Group
  65.  * @license http://www.php.net/license/2_02.txt
  66.  *          The PHP License, version 2.0
  67.  * @version Release: 0.1.5
  68.  */
  69. class DB_Sqlite_Tools_ArcFour
  70. {
  71.     // {{{ Properties
  72.  
  73.     /**
  74.      * Real programmers...
  75.      *
  76.      * @var array
  77.      */
  78.     private $s = array();
  79.  
  80.     /**
  81.      * Real programmers...
  82.      *
  83.      * @var array
  84.      */
  85.     private  $i = 0;
  86.  
  87.     /**
  88.      * Real programmers...
  89.      *
  90.      * @var array
  91.      */
  92.     private $j = 0;
  93.  
  94.     /**
  95.      * Key holder
  96.      *
  97.      * @var string
  98.      */
  99.     private $key;
  100.  
  101.     // }}}
  102.     // {{{ Constructor
  103.  
  104.     /**
  105.      * Constructor
  106.      *
  107.      * @param  string $key Key which will be used for encryption
  108.      *
  109.      * @see DB_Sqlite_Tools_ArcFour::setKey()
  110.      */
  111.     public function __construct($key = null)
  112.     {
  113.         if ($key !== null) {
  114.             $this->setKey($key);
  115.         }
  116.     }
  117.  
  118.     // }}}
  119.     // {{{ setKey()
  120.  
  121.     /**
  122.      * Set the encryption key.
  123.      *
  124.      * @param string $key Key which will be user for encrytion
  125.      */
  126.     public function setKey($key)
  127.     {
  128.         if (strlen($key) > 0) {
  129.             $this->key = $key;
  130.         }
  131.     }
  132.  
  133.     // }}}
  134.     // {{{ key()
  135.  
  136.     /**
  137.      * Initialize the encryption key.
  138.      *
  139.      * @param string $key Key which will be used for encryption
  140.      */
  141.     private function key($key)
  142.     {
  143.         $len= strlen($key);
  144.         for ($this->i = 0; $this->i < 256; $this->i++) {
  145.             $this->s[$this->i] = $this->i;
  146.         }
  147.  
  148.         $this->j = 0;
  149.         for ($this->i = 0; $this->i < 256; $this->i++) {
  150.             $this->j = ($this->j + $this->s[$this->i] + ord($key[$this->i % $len])) % 256;
  151.             $t = $this->s[$this->i];
  152.             $this->s[$this->i] = $this->s[$this->j];
  153.             $this->s[$this->j] = $t;
  154.         }
  155.         $this->i = $this->j = 0;
  156.     }
  157.  
  158.     // }}}
  159.     // {{{ crypt()
  160.  
  161.     /**
  162.      * Encrypt function.
  163.      *
  164.      * This function will be encrypt the string argument given.
  165.      *
  166.      * @param string $paramstr (reference) string that will encrypted
  167.      *
  168.      * @see DB_Sqlite_Tools_ArcFour::key()
  169.      */
  170.     public function crypt(&$paramstr)
  171.     {
  172.         //Init key for every call, Bugfix 22316
  173.         $this->key($this->key);
  174.  
  175.         $len= strlen($paramstr);
  176.         for ($c= 0; $c < $len; $c++) {
  177.             $this->i = ($this->i + 1) % 256;
  178.             $this->j = ($this->j + $this->s[$this->i]) % 256;
  179.             $t = $this->s[$this->i];
  180.             $this->s[$this->i] = $this->s[$this->j];
  181.             $this->s[$this->j] = $t;
  182.  
  183.             $t = ($this->s[$this->i] + $this->s[$this->j]) % 256;
  184.  
  185.             $paramstr[$c] = chr(ord($paramstr[$c]) ^ $this->s[$t]);
  186.         }
  187.     }
  188.  
  189.     // }}}
  190.     // {{{ decrypt()
  191.  
  192.     /**
  193.      * Decrypt function.
  194.      *
  195.      * This function will be decrypt the string argument given.
  196.      *
  197.      * @param  string $paramstr (reference) The string that will decrypted
  198.      *
  199.      * @see DB_Sqlite_Tools_ArcFour::crypt()
  200.      */
  201.     public function decrypt(&$paramstr)
  202.     {
  203.         //Decrypt is exactly the same as encrypting the string. Reuse (en)crypt code
  204.         $this->crypt($paramstr);
  205.     }
  206.  
  207.     // }}}
  208. }
  209.  
  210. // }}}
  211.  
  212. /*
  213.  * Local variables:
  214.  * mode: php
  215.  * tab-width: 4
  216.  * c-basic-offset: 4
  217.  * c-hanging-comment-ender-p: nil
  218.  * End:
  219.  */
  220. ?>