Recent posts regarding file encryption prompted me to dust off this old thing and recode in perl.
It uses "alleged RC4" which generates numbers 0-255. The RNG stream is XOR'd with the input stream to create the output stream.
The period of ARC4 is about 256**256, there is little chance of being able to reproduce the initial RNG state by brute force. If you lose the key phrase, you are toast.
Needless to say, the perl version is about 1/8 the size of the original C version. :)
#!/usr/bin/perl
# symmetrical cipher STDIN to STDOUT
use warnings;
use strict;
@ARGV or die "Usage $0 keyphrase < source > dest\n";
my @key = unpack "C*", "@ARGV";
# init ARC4 state from key
my @s = (0..255);
my $i=0;
for (0..255)
{
$i+=$key[$_%@key]+$s[$_], $i%=256;
($s[$_],$s[$i])=($s[$i],$s[$_]);
}
# do the magic
my $x=0; my $y=0; my $t;
print pack "C*", map
{
$x++, $x%=256;
$y+=$s[$x], $y%=256;
($s[$x],$s[$y])=($s[$y],$s[$x]);
$_^$s[($s[$x]+$s[$y])%256];
} unpack "C*", $t while read STDIN, $t, 32768;
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.