http://qs321.pair.com?node_id=726770
Category: Cryptography
Author/Contact Info Oromis92 mail » nicolabbi@libero.it msn » oromis92@email.it
Description: this is a very simple code to encode a file or a string by ARC4. I don't use modules. I'm a relatively perl newbie... so the code isn't perfect
#!/usr/bin/perl -s
# coded by Oromis92
# <nicolabbi@libero.it>




$key = shift or die "usage: perl $0 [-d] <key> [infile [>> outfile]]\n
+";
while (<>) {
    $plain .= $_;
}
chomp($plain);
@key = split //,$key;
@plain = split //,$plain;
@key = map(ord, @key);

@S = 0..255;
foreach $i (0..255)  { 
    $K[$i]=$key[$i%($#key+1)]; 
}



sub KSA {
    $j=0;
    for ($i=0;$i<255;$i++){
        $j = ($j + $S[$i] + $K[$i])%256;
        $temp = $S[$i];
        $S[$i] = $S[$j];
        $S[$j] = $temp;
    }
}



sub PRGA {
    $i = 0;
    $j = 0;
    for ($count=0;$count<length($plain);$count++) {
        $i = ($i + 1)%256;
        $j = ($j + $S[$i])%256;
        $temp = $S[$i];
        $S[$i] = $S[$j];
        $S[$j] = $temp;
        $t = ($S[$i] + $S[$j])%256;

        if (!$d) {
            $output = sprintf('%X', ($S[$t] ^ ord($plain[$count])));
            if (length($output) % 2 == 0) {
                $result .= $output;
            } else {
                $output .= "0" ;
                $result .= $output;
                $output = "";    
            }
        } else {
            if ($count*2 < length($plain)) {
                $output = chr($S[$t] ^ (hex(substr($plain,($count*2),2
+))));
                $result .= $output;
                $output = "";
            }
        }
    }
    print "$result\n";
}
KSA;
PRGA;