in reply to RC4 cipher performance

You shell out to get the date? Change

$dt=`/cygwin/bin/date`; chomp $dt;
to
$dt=localtime;

Searching for a hi-bit character

$clen = length($ucrypt); @uchar = split(//,$ucrypt); for($i=0; $i<($clen); $i++) { $val = ord($uchar[$i]); if($val>127) { $SKIP=1; last; } }

can be done much cleaner.

while ($ucrypt =~ /(.)/g) { if (ord($1)>127) { $SKIP=1; last; } }

But it's probably faster with a regexp.

$SKIP = $ucrypt =~ /[\x80-\xFF]/;

And then there's the issue of checking the same thing ($val > 127 and $SKIP == 0) in two different locations.

my $plaintext; my $ct; my $key = 'A'; while ( 1 ) { $plaintext = RC4($key, $ciphertext); if ( $plaintext !~ /[\x80-\xFF]/ ) { print "matched using key:$key \n"; print "$plaintext \n"; } if ( ++$ct == 10000 ) { $dt = localtime; print "[$dt] count: $ct key: $key \n"; $ct = 0; } $key++; }

I don't expect any of these changes to have a significant on the speed. (Explanation follows.)

I would appreciate any suggestions and advice and would be really happy to prove my code is the problem and not perl.

The fact that a scalar can hold a number of data types is a great strength, but it has some effect on performance. Unfortunately, Crypt::RC4 does heavy number crunching, which means a lot of operations and a lot of variable usage, which means a lot of overhead. Another performance cost is the conversion from the C array of char you pass to RC4 to a Perl array of integers, and back to a C array of char.

Unless PDL is used, heavy number crunching is not where Perl will be at its best. That's why most of the Crypt:: modules are actually written in C. Crypt::RC4 is Perl all the way.

He advises that this is a clear example of compiled code over interpreted code.

Perl is compiled, just not into machine code. Even if it was, it wouldn't be any faster. It's not "interpreted vs compiled" that affects the speed, it's the extra baggage needed to support Perl's features.

Replies are listed 'Best First'.
Re^2: RC4 cipher performance
by jaiello (Novice) on Nov 15, 2008 at 23:30 UTC
    Every attempt to use 'last' in the loop failed to achieve my goal of dumping out of the current iteration and going onto the next. So, I went back to an old shell script method that worked. I am evaluating your suggestion in the while loop to see if this would have worked using my method in the for loop.

    I used the for loop to evaluate one character at a time from the decrypted message to I can bomb out sooner and not have to evaluate the entire message. Since I have not yet groc'd your use of the regex, I am not sure it does the same thing though it looks like it.

    Thanks

      Every attempt to use 'last' in the loop failed to achieve my goal of dumping out of the current iteration and going onto the next.

      Really? I don't see why last wouldn't work in the code you posted.