Game Development Reference
In-Depth Information
14 - Caesar Cipher
>>> chr(65+8)
'I'
>>> chr(52)
'4'
>>> chr(ord('F'))
'F'
>>> ord(chr(68))
68
>>>
On the third line, chr(65+8) evaluates to chr(73) . If you look at the ASCII table,
you can see that 73 is the ordinal for the capital letter "I". On the fifth line, chr(ord
('F')) evaluates to chr(70) which evaluates to 'F' . Feeding the result of ord() to
chr() will give you back the original argument. The same goes for feeding the result of
chr() to ord() , as shown by the sixth line.
Using chr() and ord() will come in handy for our Caesar Cipher program. They are
also helpful when we need to convert strings to numbers and numbers to strings.
Sample Run of Caesar Cipher
Here is a sample run of the Caesar Cipher program, encrypting a message:
Do you wish to encrypt or decrypt a message?
encrypt
Enter your message:
The sky above the port was the color of television, tuned to a
dead channel.
Enter the key number (1-26)
13
Your translated text is:
Gur fxl nobir gur cbeg jnf gur pbybe bs gryrivfvba, gharq gb n
qrnq punaary.
Now we will run the program and decrypt the text that we just
encrypted.
Do you wish to encrypt or decrypt a message?
decrypt
Enter your message:
Gur fxl nobir gur cbeg jnf gur pbybe bs gryrivfvba, gharq gb n
qrnq punaary.
Enter the key number (1-26)
13
Your translated text is:
The sky above the port was the color of television, tuned to a
dead channel.
On this run we will try to decrypt the text that was encrypted, but we will use the wrong
 
Search WWH ::




Custom Search