Caeser Cipher II

07Jun13

In the twelve months from March 2013 to March 2014, I programmed solutions to the problems posted on the Contest Coding blog run by Lewis Cornwall, solving 33 problems (out of 47) and achieving a position of 4th on the leaderboard (out of 23).

As that blog has now been discontinued, I’m posting here the solutions I programmed to those problems.

Caeser Cipher II

Below is a paragraph of text encrypted using a Caesar cipher as described in an earlier puzzle. Differing from that earlier puzzle is that the decryption key is not known – however, it is known that the word “bird” appears in the text. Determine the decryption key and decrypt the text.

OVSTLZ ohk illu zlhalk mvy zvtl ovbyz pu zpslujl dpao opz svun, aopu ihjr jbyclk vcly h joltpjhs clzzls pu dopjo ol dhz iyldpun h whyapjbshysf thsvkvyvbz wyvkbja. Opz olhk dhz zbur bwvu opz iylhza, huk ol svvrlk myvt tf wvpua vm cpld sprl h zayhunl, shur ipyk, dpao kbss nylf wsbthnl huk h ishjr avw-ruva.

– This puzzle was suggested by Mark Bishop

Exemplar solution and answer (Caesar Cipher II.c):

Includes: seal_bool, printDateAndTime.

/*
Exemplar solution and answer for potential problem "Caesar Cipher II" of http://ContestCoding.WordPress.com/

Fri Jun 07 09:00:00 2013

Key: 19 (or -7).

HOLMES had been seated for some hours in silence with his long, thin back curved over a chemical vessel in which he was brewing a particularly malodorous product. His head was sunk upon his breast, and he looked from my point of view like a strange, lank bird, with dull grey plumage and a black top-knot.

Fri Jun 07 09:00:02 2013

Solution programmed in C using Leonardo IDE 3.4.1; solution took ~2s to run on a 80MHz PowerPC 601.
*/

#include "seal_bool.h"
#include "printDateAndTime.h"
#include <string.h>
#include <stdio.h>
#include <ctype.h>

#define k_KNOWN_WORD "bird"
#define k_KNOWN_WORD_LENGTH 4

bool f_TryToDetermineKey( char *p_word, short *p_key )
{
  for( *p_key = 1; *p_key < 26; ( *p_key ) ++ )
  {
    size_t i = 0;
    for( ; i < k_KNOWN_WORD_LENGTH; i ++ )
    {
      char c = p_word[ i ];
      c = c - 'a';
      c = c + 1;
      if( c >= 26 )
        c = c - 26;
      c = c + 'a';
      p_word[ i ] = c;
    }
    if( strcmp( p_word, k_KNOWN_WORD ) == 0 )
      return true;
  }
  return false;
}

void main( void )
{
  FILE *f = fopen( "Caesar Cipher II.txt", "r" );
  char c;
  bool wordBegun = false, wordMayBegin = true;
  size_t wordLength;
  char word[ k_KNOWN_WORD_LENGTH + 1 ];
  short key;

  p_PrintDateAndTime();
  printf( "\n" );

  while( fscanf( f, "%c", &c ) != EOF )
    if(( c >= 'a' ) && ( c <= 'z' ))
    {
      if( !wordBegun )
      {
        if( wordMayBegin )
        {
          wordBegun = true;
          wordMayBegin = false;
          wordLength = 0;
          word[ wordLength ++ ] = c;
        }
      }
      else
      {
        word[ wordLength ++ ] = c;
        if( wordLength == k_KNOWN_WORD_LENGTH + 1 )
          wordBegun = false;
      }
    }
    else
    {
      if( wordBegun && ( wordLength == k_KNOWN_WORD_LENGTH ))
      {
        word[ wordLength ] = '\0';
        if( f_TryToDetermineKey( word, &key ))
          break;
      }
      wordBegun = false;
      if( c == ' ' )
        wordMayBegin = true;
    }
  fclose( f );

  printf( "Key: %d (or %d).\n\n", key, -26 + key );
  f = fopen( "Caesar Cipher II.txt", "r" );
  while( fscanf( f, "%c", &c ) != EOF )
  {
    const bool uppercase = ( c >= 'A' ) && ( c <= 'Z' );
    c = tolower( c );
    if(( c >= 'a' ) && ( c <= 'z' ))
    {
      c = c - 'a';
      c = c + key;
      if( c >= 26 )
        c = c - 26;
      c = c + 'a';
      if( uppercase )
        c = toupper( c );
    }
    printf( "%c", c );
  }
  fclose( f );

  printf( "\n\n" );
  p_PrintDateAndTime();
}
Advertisements


No Responses Yet to “Caeser Cipher II”

  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: