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

Source for file rc4crypt.class.php

Documentation is available at rc4crypt.class.php

  1. <?php
  2.  
  3.     /* vim: set expandtab shiftwidth=4 softtabstop=4 tabstop=4: */
  4.  
  5.     /**
  6.      * RC4Crypt 3.2
  7.      *
  8.      * RC4Crypt is a petite library that allows you to use RC4
  9.      * encryption easily in PHP. It's OO and can produce outputs
  10.      * in binary and hex.
  11.      *
  12.      * (C) Copyright 2006 Mukul Sabharwal [http://mjsabby.com]
  13.      *     All Rights Reserved
  14.      *
  15.      * @link http://rc4crypt.devhome.org
  16.      * @author Mukul Sabharwal <mjsabby@gmail.com>
  17.      * @version $Id: class.rc4crypt.php,v 3.2 2006/03/10 05:47:24 mukul Exp $
  18.      * @copyright Copyright &copy; 2006 Mukul Sabharwal
  19.      * @license http://www.gnu.org/copyleft/gpl.html
  20.      * @package RC4Crypt
  21.      */
  22.  
  23.     /**
  24.      * RC4 Class
  25.      * @package RC4Crypt
  26.      */
  27.     class rc4crypt {
  28.         /**
  29.          * The symmetric encryption function
  30.          *
  31.          * @param string $pwd Key to encrypt with (can be binary of hex)
  32.          * @param string $data Content to be encrypted
  33.          * @param bool $ispwdHex Key passed is in hexadecimal or not
  34.          * @access public
  35.          * @return string 
  36.          */
  37.         function encrypt ($pwd$data$ispwdHex 0)
  38.         {
  39.             if ($ispwdHex)
  40.                 $pwd @pack('H*'$pwd)// valid input, please!
  41.  
  42.             $key['';
  43.             $box['';
  44.             $cipher '';
  45.  
  46.             $pwd_length strlen($pwd);
  47.             $data_length strlen($data);
  48.  
  49.             for ($i 0$i 256$i++)
  50.             {
  51.                 $key[$iord($pwd[$i $pwd_length]);
  52.                 $box[$i$i;
  53.             }
  54.             for ($j $i 0$i 256$i++)
  55.             {
  56.                 $j ($j $box[$i$key[$i]256;
  57.                 $tmp $box[$i];
  58.                 $box[$i$box[$j];
  59.                 $box[$j$tmp;
  60.             }
  61.             for ($a $j $i 0$i $data_length$i++)
  62.             {
  63.                 $a ($a 1256;
  64.                 $j ($j $box[$a]256;
  65.                 $tmp $box[$a];
  66.                 $box[$a$box[$j];
  67.                 $box[$j$tmp;
  68.                 $k $box[(($box[$a$box[$j]256)];
  69.                 $cipher .= chr(ord($data[$i]$k);
  70.             }
  71.             return $cipher;
  72.         }
  73.         /**
  74.          * Decryption, recall encryption
  75.          *
  76.          * @param string $pwd Key to decrypt with (can be binary of hex)
  77.          * @param string $data Content to be decrypted
  78.          * @param bool $ispwdHex Key passed is in hexadecimal or not
  79.          * @access public
  80.          * @return string 
  81.          */
  82.         function decrypt ($pwd$data$ispwdHex 0)
  83.         {
  84.             return rc4crypt::encrypt($pwd$data$ispwdHex);
  85.         }
  86.     }
  87. ?>

Documentation generated on Fri, 18 Jul 2008 21:56:36 +0200 by phpDocumentor 1.4.1