Caesar Cipher

10May13

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.

Caesar Cipher

A Caesar cipher is a very simple encryption technique which involves replacing every letter by a letter some fixed number of positions down the alphabet. For example, when “hello” is used with a shift of 3, “khoor” would be formed. The alphabet wraps, so “z” with a shift of 2 would become “b”. The following passage is encrypted using a shift of 16:

Yj mqi jxu ruij ev jycui, yj mqi jxu mehij ev jycui, yj mqi jxu qwu ev myitec, yj mqi jxu qwu ev veebyixduii, yj mqi jxu ufesx ev rubyuv, yj mqi jxu ufesx ev ydshutkbyjo, yj mqi jxu iuqied ev Bywxj, yj mqi jxu iuqied ev Tqhaduii, yj mqi jxu ifhydw ev xefu, yj mqi jxu mydjuh ev tuifqyh, mu xqt uluhojxydw ruvehu ki, mu xqt dejxydw ruvehu ki, mu muhu qbb weydw tyhusj je Xuqlud, mu muhu qbb weydw tyhusj jxu ejxuh mqo. Yd ixehj, jxu fuhyet mqi ie vqh byau jxu fhuiudj fuhyet, jxqj iecu ev yji deyiyuij qkjxehyjyui ydiyijut ed yji ruydw husuylut, veh weet eh veh ulyb, yd jxu ikfuhbqjylu tuwhuu ev secfqhyied edbo.

Decrypt the passage of the text above.

Solution and answer (Caesar Cipher.pas):

{$R+}
program Caesar_Cipher( output );
{
Solution and answer for problem "Caesar Cipher" (10th May 2013) of http://ContestCoding.WordPress.com/

It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness, it was the epoch of belief, it was the epoch of incredulity, it was the season of Light, it was the season of Darkness, it was the spring of hope, it was the winter of despair, we had everything before us, we had nothing before us, we were all going direct to Heaven, we were all going direct the other way. In short, the period was so far like the present period, that some of its noisiest authorities insisted on its being received, for good or for evil, in the superlative degree of comparison only.

Solution programmed in Pascal using Metrowerks CodeWarrior IDE 2.1 (Discover Programming Edition); solution took ~6s to run on a 80MHz PowerPC 601.
}
const
  KEY = 16;
var
  f : text;
  c : char;
  i : integer;
  uppercase : boolean;
begin
  assign( f, 'Caesar Cipher.txt' );
  reset( f );
  while not eof( f ) do begin
    if eoln( f ) then begin
      readln( f );
      writeln
    end;
    read( f, c );
    if not ( c in [ 'a'..'z', 'A'..'Z' ] ) then
      write( c )
    else begin
      i := ord( c );
      uppercase := c in [ 'A'..'Z' ];
      if uppercase then
        i := i - ord( 'A' )
      else
        i := i - ord( 'a' );
      i := i - KEY;
      if i < 0 then
        i := i + 26;
      if uppercase then
        i := i + ord( 'A' )
      else
        i := i + ord( 'a' );
      c := chr( i );
      write( c )
    end
  end;
  close( f )
end.
Advertisements


No Responses Yet to “Caesar Cipher”

  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: