adamcrussell

Perl Weekly Challenge 015

Sometimes I work on these challenges a little bit at a time over the course of several days. Other times I do the work all in one shot in the dead of night right before the deadline after getting home from a long vacation. That is what happened this week and something similar happened last week as well. As a result I think my code has a certain ... look to it. As of this writing I have a few hours to finalize and clean up the code but I think I'll leave things as is as a reminder of the time and space in which the code was written!

As with the previous weekly challenges the problem statements are short and are included in the first comment of the code. The code blocks  shown  link to GitHub Gists. (I prefer to use the gists rather than linking to the code in the main repo since it seems that the gist URLs are more permanent. The main repo is a fork which might get deleted or substantially re-organized at some point.)

Part1

Sample Run

The Code

I re-used a substantial portion of the code for retrieving the pre-computed list of primes from my solution to Challenge 012. That part seems reasonable enough I suppose. The find_weak and find_strong functions are very straightforward, I don't think I would change anything there. Looking at how I shift primes out of the main list and create the test arrays seems a little clunky to me after some further review. Like I said in the introduction I'll leave it as I wrote it late at night but if I had more time I would have liked to have used threads (maybe even just co-operative threads) to organize the code differently and have the list of weak and strong primes filled independently. Having them both filled at the same time and then having to check to see if one is already full is, again, a little clunky for my tastes.

Part 2

Sample Run

$ perl perl5/ch-2.pl
ATTACKATDAWN is encoded as pxklroreseny
PXKLRORESENY is decoded as attackatdawn

The Code

The encode() and decode() functions are come almost directly from the definition of the methods where encoding is C_i = M_i + K_i mod 26 and encoding is
M_i = C_i - K_i mod 26 where C_i is a character of encrypted text (cypher text) M_i is a character of message text (plain text) and K_i is a character of the keyword. The example of ATTACKATDAWN is a classic and so I use it here to demonstrate that everything works as expected.

Extra Information 

The Vigenère cipher, although it seems simple to a modern audience, was considered highly secure up through the 19th century. Successful attacks are mainly based on letter frequency analysis and determination of the keyword length. In particular the Kasiski Attack is an interesting case study in that it can be used against the Vigenère cipher and is representative of more sophisticated techniques. Perhaps an attack implementation and discussion will be something I will write more about in the future. Garrett's Making, Breaking Codes is an excellent text I would recommend for anyone interested in deeper treatment of these topics.

Comments for this post were locked by the author