Hacking Secret Ciphers with Python

(Ann) #1
Chapter 6 – The Caesar Cipher 87

Even though the value for LETTERS has to be the same when running the program for decryption
as when it encrypted the message, this value doesn’t have to be secret. Only the key needs to be
kept secret, while the rest of program (including the code for the Caesar cipher program) can be
shared with the world.


Summary


You’ve had to learn several programming concepts and read through quite a few chapters to get
to this point, but now you have a program that implements a secret cipher. And more importantly,
you can understand how this code works.


Modules are Python programs that contain useful functions we can use. To use these functions,
you must first import them with an import statement. To call functions in an imported module,
put the module name and a period before the function name, like: module.function().


Constant variables are by convention written in UPPERCASE. These variables are not meant to
have their value changed (although nothing prevents the programmer from writing code that does
this). Constants are helpful because they give a “name” to specific values in your program.


Methods are functions that are attached to a value of a certain data type. The upper() and
lower() string methods return an uppercase or lowercase version of the string they are called
on. The find() string method returns an integer of where the string argument passed to it can
be found in the string it is called on.


A for loop will iterate over all the characters in string value, setting a variable to each character
on each iteration. The if, elif, and else statements can execute blocks of code based on
whether a condition is True or False.


The in and not in operators can check if one string is or isn’t in another string, and evaluates
to True or False accordingly.


Knowing how to program gives you the power to take a process like the Caesar cipher and put it
down in a language that a computer can understand. And once the computer understands how to
do it, it can do it much faster than any human can and with no mistakes (unless there are mistakes
in your programming.) This is an incredibly useful skill, but it turns out the Caesar cipher can
easily be broken by someone who knows computer programming. In the next chapter we will use
our skills to write a Caesar cipher “hacker” so we can read ciphertext that other people encrypted.
So let’s move on to the next chapter, and learn how to hack encryption.

Free download pdf