RC4/ARCFOUR Implementation in PHP

I wrote this RC4/ARCFOUR implementation in PHP - based on the original C source code posted on usenet in 1994. The rc4() call itself is completely self-contained, two other methods rc4_test() and rc4_benchmark() have been provided for testing and are optional.

My motivation for writing it was to replace the dependency on MCrypt in my SpamKit plugin for Wordpress - see Gerry's site for the updated TBT code I will wrap in the next SpamKit Plugin release.

This is software is completely public domain, all I ask for is a simple credit for my work if you find it useful.

View Source: rc4.php
View Source: rc4tests.php

Examples:

1. Simple encryption & decryption

PHP:
  1. <?php
  2. require_once( "rc4.php" );
  3.  
  4. $key = "0123456789abcdef";
  5. $plaintext = "Hello World!";
  6.  
  7. $ciphertext = rc4( $key, $plaintext );
  8.  
  9. $decrypted = rc4( $key, $ciphertext );
  10.  
  11. echo $decrypted . " - " . $plaintext . "\n";
  12.  
  13. ?>

2. Execute the tests and display the results

PHP:
  1. <?php
  2. require_once( "rc4tests.php" ); // Auto includes rc4.php
  3.  
  4. echo rc4_tests();
  5.  
  6. ?>

3. Execute the tests as benchmarks and display the results

PHP:
  1. <?php
  2. require_once( "rc4tests.php" ); // Auto includes rc4.php
  3.  
  4. echo rc4_benchmark();
  5.  
  6. ?>

3 Comments so far
Leave a comment

TBTs: SpamKit_Token v1.3

I spotted a “bug” in the TBT code for SpamKit this week when using the awesome power of google. It appears that building PHP with mcrypt support is not that common, which means spamkit causes a fatal error when trying to generate your TBT…

Bug: $x and $y are undefined!

Fair cop, guv…

A quick test with error_reporting (E_ERROR | E_WARNING | E_PARSE | E_NOTICE); shows no others.

It didnt fail the test cases for it, but it caused a ‘E_NOTICE’ warning from PHP. Source code above has been updated.



Leave a comment
Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

(required)

(required, but not displayed publically)