So, yesterday the Perl 6 Advent Calendar had a challenge, to make a decent version of the ancient Caesar Cipher. Well, while looking into that, I found an article about the Vigenère cipher and thought, hey, that could be interesting. Further searches revealed that there was already a Perl 5 module that had been designed to implement the cipher. I took it upon myself to port Crypt::Vigenere to Perl 6, and think the results are pretty cool. It's not a CPAN-ready module by any stretch, but it gives you an idea of what is possible using Perl 6, right now, today.

So, if you download the script, and run it, you will see it works fairly well.

  $ ./vigenere.p6 -E LEMON ATTACKATDAWN
  LXFOPVEFRNHR

  $ ./vigenere.p6 -D LEMON LXFOPVEFRNHR
  ATTACKATDAWN

Now, there are a few more features, and it's not an exact port of the CPAN module. For one thing, it will handle non-alphabetic characters in the string, by (yes, you guessed it) not encoding them... however, their length WILL affect the encoding/decoding algorithm, so you probably wouldn't want to use them anyway, but the ability is there if you need it.

Now, I did find an interesting thing with the original Perl 5 version. It used the eval() method to attempt a transliteration of the characters. Now in the Vigenère cipher, A = A (see also: Atlas Shrugged) but the code used to generate the transliteration tables, generated a very broken when if it encoutered 'a'. This isn't noticed in the code, as by using eval, the code fails silently, and that particular character is not transliterated.

Well, due to the way I implemented it in Perl 6, that technique wasn't really applicable, so I had to change the implementation slightly, to pass a Pair object of "a" => "a" to the trans() method in the case of the letter "a" showing up. By some interesting stroke of luck, it actually works.

The script runs with the current master branch of Rakudo (not working in 'ng' branch yet) and isn't meant to be a serious implementation, but a fun experiment.

So, that said, have fun. Next up: an Autokey cipher. While the encoding part will be straightforward, the decoding part will be a bit trickier, as it will need to use the supplied keyword to decode the next portion of the decoding key. Each segment of the keyword decoded will be added to the decoding key, until the entire message is decoded. That should make for an interesting challenge. See you soon!

Changelog

Dec 09, 2009
Last update prior to importation to GreyNoise.
Dec 09, 2009
Initial version.