Game Development Reference
In-Depth Information
14 - Caesar Cipher
To get each shifted letter, draw out a row of boxes with each letter of the alphabet. Then
draw a second row of boxes under it, but start a certain number of spaces over. When you
get to the leftover letters at the end, wrap around back to the start of the boxes. Here is an
example with the letters shifted by three spaces:
Figure 14-2: The entire alphabet shifted by three spaces.
The number of spaces we shift is the key in the Caesar Cipher. The example above
shows the letter translations for the key 3.
Using a key of 3, if we encrypt the plaintext "Howdy", then the "H" becomes "E". The
letter "o" becomes "l". The letter "w" becomes "t". The letter "d" becomes "a". And the
letter "y" becomes "v". The ciphertext of "Hello" with key 3 becomes "Eltav".
We will keep any non-letter characters the same. In order to decrypt "Eltav" with the key
3, we just go from the bottom boxes back to the top. The letter "E" becomes "H", the letter
"l" becomes "o", the letter "t" becomes "w", the letter "a" becomes "d", and the letter "v"
becomes "y" to form "Howdy".
You can find out more about the Caesar Cipher from Wikipedia at
http://en.wikipedia.org/wiki/Caesar_cipher
ASCII, and Using Numbers for Letters
How do we implement this shifting of the letters in our program? We can do this by
representing each letter as a number (called an ordinal ), and then adding or subtracting
from this number to form a new number (and a new letter). ASCII (pronounced "ask-ee"
and stands for American Standard Code for Information Interchange) is a code that
connects each character to a number between 32 and 127. The numbers less than 32 refer to
"unprintable" characters, so we will not be using them.
The capital letters "A" through "Z" have the ASCII numbers 65 through 90. The
lowercase letters "a" through "z" have the ASCII numbers 97 through 122. The numeric
digits "0" through "9" have the ASCII numbers 48 through 57.
Search WWH ::




Custom Search