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.


In reply to Re: RC4 cipher performance by ikegami
in thread RC4 cipher performance by jaiello

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.