Project Euler Problem 59
Project Euler Problem 59 was actually a really nice change of pace. We're given a crypted chunk of data, told that it was produced using an XOR cypher with a three-character key consisting of characters a-z, that the plain-text is english-language, and that we should brute-force it.
Given that the plain-text is english-language, I was going to get fancy and use frequency analysis, but since I didn't have a pen or paper on my couch, I decided that I'd stick with the spirit of the problem and go ahead with a brute-force attack.
It would have taken longer to come up with an 'is this english-language?' heuristic to short-circuit a key than it would to use my brain and vim, so the solution takes awhile to come up with, but it's correct.
... and given that I've seen "Hackers" a thousand times, I probably should have just guessed the key. Oh well; read on for the code.
Oh, and yes, I could have folded the summation of the plain-text into the decryption function, but I forgot that's what the solution was going to be, so I just tacked it on at the end. Don't hate me.
usage: ./p59.pl < ciphertext > outfile
- #!/usr/bin/perl -w
- my @cipher;
- while ( <STDIN> ) {
- }
- #
- # i don't feel like writing a heuristic to determine
- # if this is english language, as I have access to
- # vim
- #
- my $i = 0;
- my $candidate = crack( $key );
- $i++;
- }
- #-------------------------------------------
- sub sum {
- my $sum = 0;
- foreach my $chr ( @arr ) {
- }
- }
- #-------------------------------------------
- sub crack {
- my $string;
- for ( my $i = 0; $i < @cipher; $i++ ) {
- }
- $string .= "\n";
- }
- #-------------------------------------------
- # all possible permutations of a 3-character a-z key
- sub getKeys {
- my $min = 97; #ascii 'a'
- my $max = 122; #ascii 'z'
- for ( my $i = $min; $i <= $max; $i++ ) {
- for ( my $j = $min; $j <= $max; $j++ ) {
- for ( my $k = $min; $k <= $max; $k++ ) {
- }
- }
- }
- }