WARNING "The Code Book" spoiler ahead

I'm sure quite a few of you guys have read/heard of Simon Singh's book The Code Book (if not it's well worth checking out).

Well here is my script that I used to crack the second puzzle in the book. It's nothing very elaborate but just in case anyone might be interested.

The code is

MHILY LZA ZBHL XBPZXBL MVYABUHL HWWPBZ JSHBKPBZ JHLJBZ KPJABT HYJHUBT LZA ULBAYVU
and it's a Caesar Shift code
Here's the script
use strict; open(CODE,"code2.txt"); my @code=<CODE>; close(CODE); my @alphabet = ("A".."Z"); my (%crypt); foreach my $try_number (0 .. 26){ print STDOUT "\n---- Shift of $try_number :\n"; foreach my $letter (A .. Z){ my $index= ($try_number++)%26; $crypt{$letter} = $alphabet[$index]; } foreach my $code_line (@code){ foreach my $letter_or_space (split(//,$code_line)){ if ($letter_or_space =~/\s+/){ #Got a space print STDOUT " "; }else{ print STDOUT $crypt{$letter_or_space}; } } } } print STDOUT "\n";
I also have a script for the first puzzle also if anyone is interested.

PS The script produces all the options you have to spot the correct solution. It should be pretty obvious :-)

Replies are listed 'Best First'.
Re: Caesar Shift Solution
by japhy (Canon) on May 14, 2001 at 18:07 UTC
    I hate to burst your bubble, but tr/// is made for this sort of thing.
    for (1 .. 26) { $str =~ tr/a-zA-Z/b-zaB-ZA/; print $str; }


    japhy -- Perl and Regex Hacker
Re: Caesar Shift Solution
by tachyon (Chancellor) on May 17, 2001 at 08:45 UTC
    FABER EST SUAE QUISQUE FORTUNAE APPIUS CLAUDIUS CAECUS DICTUM ARCANUM EST NEUTRON
    I really enjoyed this book. Have you cracked some of the harder ones?
    You could shorten your code quite a bit like this, not that it really matters
     
    tachyon
    use strict; my @code=<DATA>; my $code = join'', @code; my @alphabet = ("A".."Z"); my (%crypt); for my $try(0..26){ print "\n---- Shift of $try :\n"; for(A..Z){ my $index= ($try++)%26; $crypt{$_} = $alphabet[$index]; } for my $letter(split //,$code){ ($letter =~/\s+|\n/)? print " " :print $crypt{$letter}; } } print "\n"; __DATA__ MHILY LZA ZBHL XBPZXBL MVYABUHL HWWPBZ JSHBKPBZ JHLJBZ KPJABT HYJHUBT LZA ULBAYVU