Crypt
[ class tree: Crypt ] [ index: Crypt ] [ all elements ]

Source for file ArcFour.php

Documentation is available at ArcFour.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Dave Mertens <dmertens@zyprexia.com>                        |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: ArcFour.php,v 1.2 2004/09/29 12:18:38 gurugeek Exp $
  20.  
  21.  
  22. /**
  23. * ArcFour stream cipher routines implementation
  24. *
  25. * in PHP4 based on code written by Damien Miller <djm@mindrot.org>
  26. * ported to PHP 5 by David Costa <gurugeek@php.net>
  27. * Usage:
  28. * $key = "pear";
  29. * $message = "PEAR rulez!";
  30. *
  31. * $rc4 = new Crypt_RC4;
  32. * $rc4->key($key);
  33. * echo "Original message: $message <br>\n";
  34. * $rc4->crypt($message);
  35. * echo "Encrypted message: $message <br>\n";
  36. * $rc4->decrypt($message);
  37. * echo "Decrypted message: $message <br>\n";
  38. *
  39. @version $Revision: 1.2 $
  40. @access public
  41. @package Crypt
  42. @author Dave Mertens <dmertens@zyprexia.com>
  43.  */
  44. {
  45.     /**
  46.     * Real programmers...
  47.     * @var array 
  48.     */
  49.     private $s= array();
  50.     /**
  51.     * Real programmers...
  52.     * @var array 
  53.     */
  54.     private  $i= 0;
  55.     /**
  56.     * Real programmers...
  57.     * @var array 
  58.     */
  59.     private $j= 0;
  60.  
  61.     /**
  62.     * Key holder
  63.     * @var string 
  64.     */
  65.     private $_key;
  66.  
  67.     /**
  68.     * Constructor
  69.     * Pass encryption key to key()
  70.     *
  71.     * @see    key()
  72.     * @param  string key    - Key which will be used for encryption
  73.     * @return void 
  74.     * @access public
  75.     */
  76.     public function __construct($key = null
  77.     {
  78.         if ($key != null{
  79.             $this->setKey($key);
  80.         }
  81.     }
  82.  
  83.     public function setKey($key
  84.     {
  85.         if (strlen($key> 0)
  86.             $this->_key $key;
  87.     }
  88.  
  89.     /**
  90.     * Assign encryption key to class
  91.     *
  92.     * @param  string key    - Key which will be used for encryption
  93.     * @return void 
  94.     * @access public
  95.     */
  96.     public function key($key
  97.     {
  98.         $lenstrlen($key);
  99.         for ($this->= 0; $this->< 256; $this->i++{
  100.             $this->s[$this->i$this->i;
  101.         }
  102.  
  103.         $this->= 0;
  104.         for ($this->= 0; $this->< 256; $this->i++{
  105.             $this->($this->$this->s[$this->iord($key[$this->$len])) % 256;
  106.             $t $this->s[$this->i];
  107.             $this->s[$this->i$this->s[$this->j];
  108.             $this->s[$this->j$t;
  109.         }
  110.         $this->$this->= 0;
  111.     }
  112.  
  113.     /**
  114.     * Encrypt function
  115.     *
  116.     * @param  string paramstr     - string that will encrypted
  117.     * @return void 
  118.     * @access public
  119.     */
  120.     public function crypt(&$paramstr
  121.     {
  122.         //Init key for every call, Bugfix 22316
  123.         $this->key($this->_key);
  124.  
  125.         $lenstrlen($paramstr);
  126.         for ($c= 0; $c $len$c++{
  127.             $this->($this->+ 1% 256;
  128.             $this->($this->$this->s[$this->i]% 256;
  129.             $t $this->s[$this->i];
  130.             $this->s[$this->i$this->s[$this->j];
  131.             $this->s[$this->j$t;
  132.  
  133.             $t ($this->s[$this->i$this->s[$this->j]% 256;
  134.  
  135.             $paramstr[$cchr(ord($paramstr[$c]$this->s[$t]);
  136.         }
  137.     }
  138.  
  139.     /**
  140.     * Decrypt function
  141.     *
  142.     * @param  string paramstr     - string that will decrypted
  143.     * @return void 
  144.     * @access public
  145.     */
  146.     public function decrypt(&$paramstr
  147.     {
  148.         //Decrypt is exactly the same as encrypting the string. Reuse (en)crypt code
  149.         $this->crypt($paramstr);
  150.     }
  151.  
  152.  
  153. }    //end of RC4 class
  154. ?>

Documentation generated on Mon, 11 Mar 2019 13:56:03 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.