Wondered if anyone can throw some light on why this script won't act
properly.

Wrote it to convert a string using a ceaser cipher for a course I am
doing. Basically get the string and replace each character with the
character offset by 3 or 4 up to 26. Have to try up to 26 times so it
was crying out for a script, trouble is it doesn't work the way I
intended and I cannot see how. 

No real attempts at error catching have been done as it was only
supposed to be used by myself.
Supposed to do this 
Enter 
>cipher.pl phhw dw plgqljkw
...and get all 26 different combinations to crack the message.
Trouble is I get this.....
qiix ex qmhrmklx  -------> offset = 1 <-------
qiix ex qmhrmklx  -------> offset = 1 <-------
rjjy fy rnisnlmy  -------> offset = 2 <-------
qiix ex qmhrmklx  -------> offset = 1 <-------
rjjy fy rnisnlmy  -------> offset = 2 <-------
skkz gz sojtomnz  -------> offset = 3 <-------
qiix ex qmhrmklx  -------> offset = 1 <-------
rjjy fy rnisnlmy  -------> offset = 2 <-------
skkz gz sojtomnz  -------> offset = 3 <-------
tlla ha tpkupnoa  -------> offset = 4 <-------

Instead of just going through once it is repeating ie loops to 1 then
loops to 2 then loops to 3 up till loops to 25. Cracks the message ok at
offset 23 but I don't want it looping all the time. Code follows, I am
on Linux 8.0 with Perl 5.8, there is probably better ways to do this but
it is driving me mad trying to see the logical problem, hoping someone
can help.

*******************************************************************
#!/usr/bin/perl -w

use strict;

my @alpha =
("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
my %alphat;
my $count;
my $ceascount;
my $op = "";

foreach (@alpha){                  # set up a lookup table for the array
offset
  $alphat{$alpha[$count]} = $count;
  $count++;
}

for ($ceascount = 1; $ceascount < ($#alpha +1);$ceascount++,print
$op){     # try all ceaser cipher offsets
  foreach (@ARGV){               # take in each command line arg
    for ($count=0; $count < length($_); $count++){       # go through
each arg and convert with cipher
        $op .= $alpha[($alphat{substr($_,$count,1)} + $ceascount) %
($#alpha+1)];  # conversion
    }
    $op .= " ";  # space between args
  }
  $op .= " -------> offset = $ceascount <-------\n";  # final result
}     # use another ceaser cipher offset



*******************************************************************


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to