Chapter 1 - Making Paper Cryptography Tools
Chapter 1 - Making Paper Cryptography Tools
What Is Cryptography?
Historically, anyone who has needed to share secrets with others, such as spies, soldiers, hackers, pirates, merchants,
tyrants, and political activists, has relied on cryptography to make sure their secrets stay secret. Cryptography is the
science of using secret codes. To understand what cryptography looks like, look at the following two pieces of text:
The text on the left is a secret message that has been encrypted, or turned into a secret code. It’s completely
unreadable to anyone who doesn’t know how to decrypt it, or turn it back into the original English message. The
message on the right is random gibberish with no hidden meaning. Encryption keeps a message secret from other people
who can’t decipher it, even if they get their hands on the encrypted message. An encrypted message looks exactly like
random nonsense.
A cryptographer uses and studies secret codes. Of course, these secret messages don’t always remain secret.
A cryptanalyst, also called a code breaker or hacker, can hack secret codes and read other people’s encrypted messages.
This book teaches you how to encrypt and decrypt messages using various techniques. But unfortunately (or
fortunately), the type of hacking you’ll learn in this book isn’t dangerous enough to get you in trouble with the law.
https://inventwithpython.com/cracking/chapter1.html 1/6
3/5/2023 Chapter 1 - Making Paper Cryptography Tools
By tapping dots and dashes with a one-button telegraph, a telegraph operator could communicate an English message
to someone on the other side of the world almost instantly! (To learn more about Morse code,
visit https://www.nostarch.com/crackingcodes/.)
In contrast with codes, a cipher is a specific type of code meant to keep messages secret. You can use a cipher to turn
understandable English text, called plaintext, into gibberish that hides a secret message, called the ciphertext. A cipher is
a set of rules for converting between plaintext and ciphertext. These rules often use a secret key to encrypt or decrypt
that only the communicators know. In this book, you’ll learn several ciphers and write programs to use these ciphers to
encrypt and decrypt text. But first, let’s encrypt messages by hand using simple paper tools.
https://inventwithpython.com/cracking/chapter1.html 2/6
3/5/2023 Chapter 1 - Making Paper Cryptography Tools
A printable paper cipher wheel is also available from the book’s website. Cut out the two circles and lay them on top
of each other, placing the smaller one in the middle of the larger one. Insert a pin or brad through the center of both
circles so you can spin them around in place.
Using either the paper or the virtual wheel, you can encrypt secret messages by hand.
For each letter in the message, find it in the outer circle and replace it with the corresponding letter in the inner circle.
In this example, the first letter in the message is T (the first T in “THE SECRET…”), so find the letter T in the outer
circle and then find the corresponding letter in the inner circle, which is the letter B. So the secret message always
replaces a T with a B. (If you were using a different encryption key, each T in the plaintext would be replaced with a
different letter.) The next letter in the message is H, which turns into P. The letter E turns into M. Each letter on the outer
wheel always encrypts to the same letter on the inner wheel. To save time, after you look up the first T in “THE
https://inventwithpython.com/cracking/chapter1.html 3/6
3/5/2023 Chapter 1 - Making Paper Cryptography Tools
SECRET…” and see that it encrypts to B, you can replace every T in the message with B, so you only need to look up a
letter once.
After you encrypt the entire message, the original message, THE SECRET PASSWORD IS ROSEBUD, becomes
BPM AMKZMB XIAAEWZL QA ZWAMJCL. Notice that non-letter characters, such as the spaces, are not changed.
Now you can send this encrypted message to someone (or keep it for yourself), and nobody will be able to read it
unless you tell them the secret encryption key. Be sure to keep the encryption key a secret; the ciphertext can be read by
anyone who knows that the message was encrypted with key 8.
Now you can line up the letter A on the outer circle (the one with the dot below it) over the letter on the inner circle
that has the number 15 (which is the letter P). Then, find the first letter in the secret message on the inner circle, which is
I, and look at the corresponding letter on the outer circle, which is T. The second letter in the ciphertext, W, decrypts to
the letter H. Decrypt the rest of the letters in the ciphertext back to the plaintext, and you’ll get the message THE NEW
PASSWORD IS SWORDFISH, as shown in Figure 1-4.
If you used an incorrect key, like 16, the decrypted message would be SGD MDV OZRRVNQC HR RVNQCEHRG,
which is unreadable. Unless the correct key is used, the decrypted message won’t be understandable.
https://inventwithpython.com/cracking/chapter1.html 4/6
3/5/2023 Chapter 1 - Making Paper Cryptography Tools
You can use this letters-to-numbers code to represent letters. This is a powerful concept, because it allows you to do
math on letters. For example, if you represent the letters CAT as the numbers 2, 0, and 19, you can add 3 to get the
numbers 5, 3, and 22. These new numbers represent the letters FDW, as shown in Figure 1-5. You have just “added” 3 to
the word cat! Later, we’ll be able to program a computer to do this math for us.
To use arithmetic to encrypt with the Caesar cipher, find the number under the letter you want to encrypt and add the
key number to it. The resulting sum is the number under the encrypted letter. For example, let’s encrypt HELLO. HOW
ARE YOU? using the key 13. (You can use any number from 1 to 25 for the key.) First, find the number under H, which
is 7. Then add 13 to this number: 7 + 13 = 20. Because the number 20 is under the letter U, the letter H encrypts to U.
Similarly, to encrypt the letter E (4), add 4 + 13 = 17. The number above 17 is R, so E gets encrypted to R, and so on.
This process works fine until the letter O. The number under O is 14. But 14 plus 13 is 27, and the list of numbers
only goes up to 25. If the sum of the letter’s number and the key is 26 or more, you need to subtract 26 from it. In this
case, 27 – 26 = 1. The letter above the number 1 is B, so O encrypts to B using the key 13. When you encrypt each letter
in the message, the ciphertext will be URYYB. UBJ NER LBH?
To decrypt the ciphertext, subtract the key instead of adding it. The number of the ciphertext letter B is 1. Subtract 13
from 1 to get –12. Like our “subtract 26” rule for encrypting, when the result is less than 0 when decrypting, we need to
add 26. Because –12 + 26 = 14, the ciphertext letter B decrypts to O.
NOTE
If you don’t know how to add and subtract with negative numbers, you can read about it
athttps://www.nostarch.com/crackingcodes/.
As you can see, you don’t need a cipher wheel to use the Caesar cipher. All you need is a pencil, a piece of paper, and
some simple arithmetic!
Summary
The Caesar cipher and other ciphers like it were used to encrypt secret information for several centuries. But if you
wanted to encrypt a long message—say, an entire book—it could take days or weeks to encrypt it all by hand. This is
where programming can help. A computer can encrypt and decrypt a large amount of text in less than a second!
To use a computer for encryption, you need to learn how to program, or instruct, the computer to do the same steps
we just did using a language the computer can understand. Fortunately, learning a programming language like Python
isn’t nearly as difficult as learning a foreign language like Japanese or Spanish. You also don’t need to know much math
besides addition, subtraction, and multiplication. All you need is a computer and this book!
Let’s move on to Chapter 2, where we’ll learn how to use Python’s interactive shell to explore code one line at a
time.
PRACTICE QUESTIONS
Answers to the practice questions can be found on the book’s website at https://www.nostarch.com/crackingcodes/.
1. Encrypt the following entries from Ambrose Bierce’s The Devil’s Dictionary with the given keys:
https://inventwithpython.com/cracking/chapter1.html 5/6
3/5/2023 Chapter 1 - Making Paper Cryptography Tools
a. With key 4: “AMBIDEXTROUS: Able to pick with equal skill a right-hand pocket or a left.”
b. With key 17: “GUILLOTINE: A machine which makes a Frenchman shrug his shoulders with good
reason.”
c. With key 21: “IMPIETY: Your irreverence toward my deity.”
2. Decrypt the following ciphertexts with the given keys:
a. With key 15: “ZXAI: P RDHIJBT HDBTIXBTH LDGC QN HRDIRWBTC XC PBTGXRP PCS
PBTGXRPCH XC HRDIAPCS.”
b. With key 4: “MQTSWXSV: E VMZEP EWTMVERX XS TYFPMG LSRSVW.”
3. Encrypt the following sentence with the key 0: “This is a silly example.”
4. Here are some words and their encryptions. Which key was used for each word?
a. ROSEBUD – LIMYVOX
b. YAMAMOTO – PRDRDFKF
c. ASTRONOMY – HZAYVUVTF
5. What does this sentence encrypted with key 8 decrypt to with key 9? “UMMSVMAA: Cvkwuuwv
xibqmvkm qv xtivvqvo i zmdmvom bpib qa ewzbp epqtm.”
https://inventwithpython.com/cracking/chapter1.html 6/6