Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

ARC4

by Oromis92 (Sexton)
on Nov 29, 2008 at 12:04 UTC ( #726770=sourcecode: print w/ replies, xml ) Need Help??

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;

Comment on ARC4
Download Code
Replies are listed 'Best First'.
Re: ARC4
by wazoox (Prior) on Dec 01, 2008 at 11:34 UTC
    Just a couple of comments :

    - Unless you know what you're doing, always add use strict; use warnings; at the top of your code. This will force you to declare variables and will save you a huge amount of problems.

    - Though "C style" loops are generally OK, people usually prefer "perlisms", for instance :

    for my $i ( 0 .. 255 ) { # whatever }

Re: ARC4
by andreas1234567 (Vicar) on Dec 01, 2008 at 12:49 UTC
    You mean ARC4 as in Alleged RC4? If yes, you know there are already modules for that, e.g. Crypt::RC4?
    --
    No matter how great and destructive your problems may seem now, remember, you've probably only seen the tip of them. [1]

Back to Code Catacombs

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://726770]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2015-07-30 03:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (269 votes), past polls