Cracking Codes with Python: An Introduction to Building and Breaking Ciphers
Chapter 16 Programming the Simple Substitution Cipher covers writ ing a simple substitution cipher encryption program Chapter 17 Hacking the Simple Substitution Cipher explains how to write a program to hack the simple substitution cipher Chapter 18 Programming the Vigenère Cipher explains a program for the Vigenère cipher a more complex substitution cipher Chapter 19 Frequency Analysis explores the structure of English words and how to use it to hack the Vigenère cipher Chapter 20 Hacking the Vigenère Cipher covers a program for hack ing the Vigenère cipher Chapter 21 The One-Time Pad Cipher explains the one-time pad cipher and why it’s mathematically impossible to hack Chapter 22 Finding and Generating Prime Numbers covers how to write a program that quickly determines whether a number is prime Chapter 23 Generating Keys for the Public Key Cipher describes pub lic key cryptography and how to write a program that generates public and private keys Chapter 24 Programming the Public Key Cipher explains how to write a program for a public key cipher which you can’t hack using a mere laptop The appendix Debugging Python Code shows you how to use IDLE’s debugger to find and fix bugs in your programs how to use this Book Cracking Codes with Python is different from other programming books because it focuses on the source code of complete programs Instead of teaching you programming concepts and leaving it up to you to figure out how to make your own programs this book shows you complete programs and explains how they work In general you should read the chapters in this book in order The programming concepts build on those in the previous chapters However Python is such a readable language that after the first few chapters you can probably jump ahead to later chapters and piece together what the code does If you jump ahead and feel lost return to earlier chapters Typing Source Code As you read through this book I encourage you to manually type the source code from this book into Python Doing so will definitely help you understand the code better When typing the source code don’t include the line numbers that appear at the beginning of each line These numbers are not part of the actual programs and we use them only to refer to specific lines in the code But aside from the line numbers be sure to enter the code exactly as it appears including the uppercase and lowercase letters xxiv Introduction
You’ll also notice that some of the lines don’t begin at the leftmost edge of the page but are indented by four eight or more spaces Be sure to enter the correct number of spaces at the beginning of each line to avoid errors But if you would rather not type the code you can download the source code files from this book’s website at https www nostarch com crackingcodes Checking for Typos Although manually entering the source code for the programs is helpful for learning Python you might occasionally make typos that cause errors These typos can be difficult to spot especially when your source code is very long To quickly and easily check for mistakes in your typed source code you can copy and paste the text into the online diff tool on the book’s website at https www nostarch com crackingcodes The diff tool shows any differences between the source code in the book and yours Coding Conventions in This Book This book is not designed to be a reference manual it’s a hands-on guide for beginners For this reason the coding style sometimes goes against best practices but that’s a conscious decision to make the code easier to learn This book also skips theoretical computer science concepts Veteran programmers may point out ways the code in this book could be changed to improve efficiency but this book is mostly concerned with getting programs to work with the least amount of effort Online Resources This book’s website https www nostarch com crackingcodes includes many useful resources including downloadable files of the programs and sample solutions to the practice questions This book covers classical ciphers thor oughly but because there is always more to learn I’ve also included sugges tions for further reading on many of the topics introduced in this book downloading and installing Python Before you can begin programming you’ll need to install the Python inter preter which is software that executes the instructions you’ll write in the Python language I’ll refer to the Python interpreter as Python from now on Download Python for Windows macOS and Ubuntu for free from https www python org downloads If you download the latest version all of the programs in this book should work no t e Be sure to download a version of Python 3 such as 3 6 The programs in this book are written to run on Python 3 and may not run correctly if at all on Python 2 Introduction xxv
Windows Instructions On Windows download the Python installer which should have a filename ending with msi and double-click it Follow the instructions the installer displays on the screen to install Python as listed here 1 Select Install Now to begin the installation 2 When the installation is finished click Close macOS Instructions On macOS download the dmg file for your version of macOS from the web site and double-click it Follow the instructions the installer displays on the screen to install Python as listed here 1 When the DMG package opens in a new window double-click the Python mpkg file You may have to enter your computer’s administrator password 2 Click Continue through the Welcome section and click Agree to accept the license 3 Select HD Macintosh or the name of your hard drive and click Install Ubuntu Instructions If you’re running Ubuntu install Python from the Ubuntu Software Center by following these steps 1 Open the Ubuntu Software Center 2 Type Python in the search box in the top-right corner of the window 3 Select IDLE using Python 3 6 or whatever is the latest version 4 Click Install You may have to enter the administrator password to complete the installation downloading pyperclip py Almost every program in this book uses a custom module I wrote called pyperclip py This module provides functions that let your programs copy and paste text to the clipboard It doesn’t come with Python so you’ll need to download it from https www nostarch com crackingcodes This file must be in the same folder also called directory as the Python program files you write Otherwise you’ll see the following error message when you try to run your programs ImportError No module named pyperclip Now that you’ve downloaded and installed the Python interpreter and the pyperclip py module let’s look at where you’ll be writing your programs xxvi Introduction
starting idle While the Python interpreter is the software that runs your Python pro grams the interactive development environment IDLE software is where you’ll write your programs much like a word processor IDLE is installed when you install Python To start IDLE follow these steps On Windows 7 or newer click the Start icon in the lower-left corner of your screen enter IDLE in the search box and select IDLE Python 3 6 64-bit On macOS open Finder click Applications click Python 3 6 and then click the IDLE icon On Ubuntu select Applications Accessories Terminal and then enter idle3 You may also be able to click Applications at the top of the screen select Programming and then click IDLE 3 No matter which operating system you’re running the IDLE window should look something like Figure 1 The header text may be slightly differ ent depending on your specific version of Python Figure 1 The IDLE window This window is called the interactive shell A shell is a program that lets you type instructions into the computer much like the Terminal on macOS or the Windows Command Prompt Sometimes you’ll want to run short snippets of code instead of writing a full program Python’s interactive shell lets you enter instructions for the Python interpreter software which the computer reads and runs immediately For example type the following into the interactive shell next to the prompt print Hello world Press enter and the interactive shell should display this in response Hello world Introduction xxvii