Advertisement
johnburn

rc4.php

Apr 17th, 2011
456
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.80 KB | None | 0 0
  1. <?php
  2.  
  3. class rc4crypt
  4. {
  5.  
  6.     function endecrypt( $pwd, $data, $case = "encrypt" )
  7.     {
  8.         if ( $case == "decrypt" )
  9.         {
  10.             $data = urldecode( $data );
  11.         }
  12.         $key[] = "";
  13.         $box[] = "";
  14.         $temp_swap = "";
  15.         $pwd_length = 0;
  16.         $pwd_length = strlen( $pwd );
  17.         $i = 0;
  18.         for ( ; $i <= 255; ++$i )
  19.         {
  20.             $key[$i] = ord( substr( $pwd, $i % $pwd_length, 1 ) );
  21.             $box[$i] = $i;
  22.         }
  23.         $x = 0;
  24.         $i = 0;
  25.         for ( ; $i <= 255; ++$i )
  26.         {
  27.             $x = ( $x + $box[$i] + $key[$i] ) % 256;
  28.             $temp_swap = $box[$i];
  29.             $box[$i] = $box[$x];
  30.             $box[$x] = $temp_swap;
  31.         }
  32.         $temp = "";
  33.         $k = "";
  34.         $cipherby = "";
  35.         $cipher = "";
  36.         $a = 0;
  37.         $j = 0;
  38.         $i = 0;
  39.         for ( ; $i < strlen( $data ); ++$i )
  40.         {
  41.             $a = ( $a + 1 ) % 256;
  42.             $j = ( $j + $box[$a] ) % 256;
  43.             $temp = $box[$a];
  44.             $box[$a] = $box[$j];
  45.             $box[$j] = $temp;
  46.             $k = $box[( $box[$a] + $box[$j] ) % 256];
  47.             $cipherby = ord( substr( $data, $i, 1 ) ) ^ $k;
  48.             $cipher .= chr( $cipherby );
  49.         }
  50.         if ( $case == "decrypt" )
  51.         {
  52.             $cipher = urldecode( urlencode( $cipher ) );
  53.         }
  54.         else
  55.         {
  56.             $cipher = urlencode( $cipher );
  57.         }
  58.         return $cipher;
  59.     }
  60.  
  61.     function decrypt( $key, $data )
  62.     {
  63.         return $this->endecrypt( $key, base64_decode( $data ), "decrypt" );
  64.     }
  65.  
  66.     function encrypt( $key, $data )
  67.     {
  68.         return base64_encode( $this->endecrypt( $key, $data, "encrypt" ) );
  69.     }
  70.  
  71. }
  72.  
  73. new rc4crypt( )->rc4crypt( );
  74. $rc4 = new rc4crypt( );
  75. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement