Skip to content

Instantly share code, notes, and snippets.

@shoghicp
Last active March 17, 2016 06:10
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shoghicp/a3896c38f4c332430816 to your computer and use it in GitHub Desktop.
Save shoghicp/a3896c38f4c332430816 to your computer and use it in GitHub Desktop.
RC4 implementation in PHP, without run-time notices or errors. Outputs keystream directly. http://en.wikipedia.org/wiki/RC4
<?php
for($K='Key',$_=range($i=$j=0,$n=255);$i<=$n;$j+=$_[$i]+ord($K{$i%strlen($K)}),_($_[$j&=$n],$_[$i++]));
for($j=$i=0;++$i;$j+=$_[$i&=$n],_($_[$j&=$n],$_[$i]),printf('%x',$_[($_[$i]+$_[$j])&$n]));
function _(&$i,&$j){$i=$j+$i-($j=$i);}
<?php for($K='Key',$_=range($i=$j=0,$n=255);$i<=$n;$j+=$_[$i]+ord($K{$i%strlen($K)}),_($_[$j&=$n],$_[$i++]));for($j=$i=0;++$i;$j+=$_[$i&=$n],_($_[$j&=$n],$_[$i]),printf('%c',$_[($_[$i]+$_[$j])&$n]));function _(&$i,&$j){$i=$j+$i-($j=$i);}
<?php
for(
$K = 'Key',
$_ = range($i = $j = 0, $n = 255);
$i <= $n;
$j += $_[$i] + ord($K{$i % strlen($K)}),
_($_[
$j &= $n
], $_[
$i++
])
);
for($j = $i = 0;
++$i;
$j += $_[
$i &= $n
],
_($_[
$j &= $n
], $_[$i]),
printf('%x', $_[
($_[$i] + $_[$j]) & $n
])
);
function _(&$i, &$j){
$i = $j + $i - ($j = $i);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment