RC4 in 3 lines of perl


#!/usr/bin/perl -0777 @k=unpack('C*',pack('H*',shift));for(@t=@s=0..255){$y=($k[$_%@k]+$s[$x=$_ ]+$y)%256;&S}$x=$y=0;for(unpack('C*',<>)){$x++;$y=($s[$x%=256]+$y)%256; &S;print pack(C,$_^=$s[($s[$x]+$s[$y])%256])}sub S{@s[$x,$y]=@s[$y,$x]}
Malcolm Beattie contributed a 26 byte saving, which combined with losing the usage string allowed the move to 3 lines!

John Allen contributed a further 9 byte saving. Plus a bug fix. He also contributed some perl5 specific improvements (a smallest perl5 version, and a fastest perl5 version).

See also rc4 in C for RC4 in C, by John Allen.


Comments, html bugs to me (Adam Back) at <adam@cypherspace.org>