That's the entire Caesar Cipher. However, while this cipher may fool some people who don't understand cryptography, it won't keep a message secret from someone who knows cryptanalysis. While cryptography is the science of making codes, cryptanalysis is the study of breaking codes.
Do you wish to encrypt or decrypt a message? encrypt
Enter your message:
The door key will be hidden under the mat until the fourth of July.
Enter the key number (1-26) 8
Your translated text is:
Bpm lwwz smg eqtt jm pqllmv cvlmz bpm uib cvbqt bpm nwczbp wn Rctg.
The whole point of cryptography is that so if someone else gets their hands on the encrypted message, they cannot figure out the original unencrypted message from it. So we pretend we are the attacker and all we have is the encrypted text:
Bpm lwwz smg eqtt jm pqllmv cvlmz bpm uib cvbqt bpm nwczbp wn Rctg.
One method of cryptanalysis is called brute force. Brute force is the technique of trying every single possible key. If the cryptanalyst knows the cipher that the message uses (or at least guesses it), they can just go through every possible key. Because there are only 26 possible keys, it would be easy for a cryptanalyst to write a program than prints the decrypted ciphertext of every possible key and see if any of the outputs make sense. Let's add a brute force feature to our program.
First, change lines 7, 9, and 12 (which are in the cryptanalysis to look like the following (the changes are in bold):
7. print 'Do you wish to encrypt or decrypt or brute force a message?'
9. if mode in 'encrypt e decrypt d brute b'.split
10. return mode[0]
12. print 'Enter either "encrypt" or "e" or
"decrypt" or "d" or "brute" or "b".'
This will let us select "brute force" as a mode for our program. Then modify and add the following changes to the main part of the program:
57. print 'Your translated text is:' 58 . if mode[0] != 'b':
59. print getTranslatedMessage(mode, message, key)
62. print key, getTranslatedMessage("decrypt1, message, key)
These changes make our program ask the user for a key if they are not in "brute force" mode. If they ire not in "brute force" mode, then the original getTranslatedMessage ( ) call is made and the ranslated string is printed.
However, otherwise we are in "brute force" mode, and we run a getTranslatedMessage ( ) loop hat iterates from 1 all the way up to MAX_KEY_SIZE (which is 2 6). Remember that when the range () function returns a list of integers up to but not including the second parameter, which is why we have ^ 1. This program will print out every possible translation of the message (including the key number used in the translation). Here is a sample run of this modified program:
Do you wish to encrypt or decrypt or brute force a message?
brute
Enter your message:
Bpm lwwz smg eqtt jm pqllmv cvlmz bpm uib cvbqt bpm nwczbp wn Rctg.
Your translated text is:
1 Aol kvvy rlf dpss il opkklu bukly aol tha buaps aol mvbyao vm Qbsf.
2 Znk juux qke corr hk nojjkt atjkx znk sgz atzor znk luaxzn ul Pare.
3 Ymj ittw pjd bnqq gj mniijs zsijw ymj rfy zsynq ymj ktzwym tk Ozqd.
4 Xli hssv oic ampp fi lmhhir yrhiv xli qex yrxmp xli jsyvxl sj Nypc.
5 Wkh grru nhb zloo eh klgghq xqghu wkh pdw xqwlo wkh irxuwk ri Mxob.
6 Vjg fqqt mga yknn dg jkffgp wpfgt vjg ocv wpvkn vjg hqwtvj qh Lwna.
7 Uif epps lfz xjmm cf ijeefo voefs uif nbu voujm uif gpvsui pg Kvmz.
8 The door key will be hidden under the mat until the fourth of July.
9 Sgd cnnq jdx vhkk ad ghccdm tmcdq sgd lzs tmshk sgd entqsg ne Itkx.
10 Rfc bmmp icw ugjj zc fgbbcl slbcp rfc kyr slrgj rfc dmsprf md Hsjw.
11 Qeb allo hbv tfii yb efaabk rkabo qeb jxq rkqfi qeb clroqe lc Griv.
12 Pda zkkn gau sehh xa dezzaj qjzan pda iwp qjpeh pda bkqnpd kb Fqhu.
13 Ocz yjjm fzt rdgg wz cdyyzi piyzm ocz hvo piodg ocz ajpmoc ja Epgt.
14 |
Nby |
xiil |
eys |
qcff |
vy |
bcxxyh |
ohxyl |
nby |
gun |
ohncf |
nby |
ziolnb |
iz |
Dofs |
15 |
Max |
whhk |
dxr |
pbee |
ux |
abwwxg |
ngwxk |
max |
ftm |
ngmbe |
max |
yhnkma |
hy |
Cner |
16 |
Lzw |
vggj |
cwq |
oadd |
tw |
zavvwf |
mfvwj |
l zw |
esl |
mflad |
l zw |
xgmjlz |
gx |
Bmdq |
17 |
Kyv |
uffi |
bvp |
nzcc |
sv |
yzuuve |
leuvi |
kyv |
drk |
lekzc |
kyv |
wfliky |
fw Alcp | |
18 |
Jxu |
teeh |
auo |
mybb |
ru |
xyttud |
kdtuh |
j xu |
cqj |
kdjyb |
j xu |
vekhj x |
ev |
Zkbo |
19 |
Iwt |
sddg |
ztn |
lxaa |
qt |
wxsstc |
jcstg |
iwt |
bpi |
j cixa |
iwt |
udj giw |
du |
Yj an |
20 |
Hvs |
rccf |
ysm |
kwzz |
ps |
vwrrsb |
ibrsf |
hvs |
aoh |
ibhwz |
hvs |
tcifhv |
ct |
Xizm |
21 |
Gur |
qbbe |
xrl |
j vyy |
or |
uvqqra |
haqre |
gur |
zng |
hagvy |
gur |
sbhegu |
bs |
Whyl |
22 |
Ftq |
paad |
wqk |
iuxx |
nq |
tuppqz |
gzpqd |
ftq |
ymf |
gzfux |
ftq |
ragdft |
ar |
Vgxk |
23 |
Esp |
ozzc |
vpj |
htww |
mp |
stoopy |
fyopc |
esp |
xle |
fyetw |
esp |
qzfces |
zq |
Ufwj |
24 |
Dro |
nyyb |
uoi |
gsvv |
lo |
rsnnox |
exnob |
dro |
wkd |
exdsv |
dro |
pyebdr |
yp |
Tevi |
25 |
Cqn |
mxxa |
tnh |
fruu |
kn |
qrmmnw |
dwmna |
cqn |
vj c |
dwcru |
cqn |
oxdacq |
xo |
Sduh |
26 |
Bpm |
lwwz |
smg |
eqtt |
jm |
pqllmv |
cvlmz |
bpm |
uib |
cvbqt |
bpm |
nwczbp |
wn |
Rctg |
After looking over each row, you can see that the 8th message is not garbage, but plain English! The cryptanalyst can deduce that the original key for this encrypted text must have been 8. This brute force would have been difficult to do back in the days of Caesars and the Roman Empire, but today we have computers that can quickly go through millions or even billions or keys. You can even write a program that can recognize when it has found a message in English, so you don't have read through all the garbage text. (In fact, we will do this in the next Invent Your Own Computer Games with Python book!)
Things Covered In This Chapter:
• Cryptography and ciphers
• Encrypting and decrypting
• Ciphertext, plaintext, keys, and symbols
• The Caesar Cipher
• ASCII ordinal values
• The isalpha() string method
• The isupper() and islower() string methods
• Cryptanalysis
• The brute force technique
Was this article helpful?