On the recommendation of my friend Dan, I’m going to try to do some of these programming puzzles from time to time. I’m also going to try work these problems using Perl. Today’s assignment is to make a simple Autokey Cipher, which I accomplished over my lunch break.

<br /> use List::Util qw/sum/;</p> <p>if ($#ARGV != 1 ) {<br /> print "usage: autokey.pl plainText key\n";<br /> exit;<br /> }</p> <p>$plainText = uc $ARGV[0];<br /> $keyBase = uc $ARGV[1];</p> <p>$key = $keyBase.$plainText;</p> <p>print $key;</p> <p>#$plainText = "ACOLLECTIONOFETUDES";<br /> #$key = "PRAXISACOLLECTIONOF";<br /> $cipherText = "";</p> <p>sub encipher{<br /> my($thisPlainText, $thisKey) = @_;</p> <p> #Clear Cipher'd text<br /> $thisCipherText = "";</p> <p> #Clear i<br /> $i=0;</p> <p> @arrayPlainText = split //, $thisPlainText;<br /> @arrayKey = split //, $thisKey;</p> <p> for($i = 0; $i < scalar @arrayPlainText; $i++){<br /> if(@h{@arrayPlainText[$i]} + @h{@arrayKey[$i]} > 27){<br /> $thisCipherText .= @k[@h{@arrayPlainText[$i]} + @h{@arrayKey[$i]} - 27];<br /> #print @h{@arrayPlainText[$i]} + @h{@arrayKey[$i]} - 26 . "\n";<br /> #print @k[@h{@arrayPlainText[$i]} + @h{@arrayKey[$i]} - 26] . "\n";<br /> }<br /> else{<br /> $thisCipherText .= @k[@h{@arrayPlainText[$i]} + @h{@arrayKey[$i]} - 1];<br /> #print @h{@arrayPlainText[$i]} + @h{@arrayKey[$i]} . "\n";<br /> #print @k[@h{@arrayPlainText[$i]} + @h{@arrayKey[$i]}] . "\n";<br /> }</p> <p> }</p> <p> return $thisCipherText;</p> <p>}</p> <p>sub decipher{<br /> my($thisCipherText, $thisKey) = @_;</p> <p> @arrayCipherText = split //, $thisCipherText;<br /> @arrayKey = split //, $thisKey;</p> <p> for($i = 0; $i < scalar @arrayCipherText; $i++){<br /> if(@h{@arrayCipherText[$i]} - @h{@arrayKey[$i]} < 0){<br /> $thisPlainText .= @k[@h{@arrayCipherText[$i]} - @h{@arrayKey[$i]} + 27];<br /> }<br /> else{<br /> $thisPlainText .= @k[@h{@arrayCipherText[$i]} - @h{@arrayKey[$i]} + 1];<br /> }<br /> }<br /> return $thisPlainText;<br /> }</p> <p>@h{'A' .. 'Z'} = (1 .. 26);<br /> @k[1 .. 26] = ('A' .. 'Z');</p> <p>$cipherText = &encipher($plainText, $key);</p> <p>$decipherText = &decipher($cipherText, $key);</p> <p>print " Plain text is: " . $plainText . "\n";<br /> print "Cipher text is: " . $cipherText . "\n";<br /> print "Decipher text is: " . $decipherText . "\n";</p> <p>