Coding Conventions in This Book
Downloading and Installing Python
1
MAKING PAPER CRYPTOGRAPHY TOOLS
Encrypting with the Cipher Wheel
Decrypting with the Cipher Wheel
Encrypting and Decrypting with Arithmetic
Why Double Encryption Doesn’t Work
2
PROGRAMMING IN THE INTERACTIVE SHELL
Integers and Floating-Point Values
Working with Text Using String Values
String Concatenation with the + Operator
String Replication with the * Operator
Getting Characters from Strings Using Indexes
Printing Values with the print() Function
Writing Programs in IDLE’s File Editor
Source Code for the “Hello, World!” Program
Checking Your Source Code with the Online Diff Tool
Using IDLE to Access Your Program Later
Opening the Programs You’ve Saved
How the “Hello, World!” Program Works
Printing Directions to the User
Source Code for the Reverse Cipher Program
Sample Run of the Reverse Cipher Program
Setting Up Comments and Variables
Finding the Length of a String
Improving the Program with an input() Prompt
Source Code for the Caesar Cipher Program
Sample Run of the Caesar Cipher Program
Importing Modules and Setting Up Variables
A while Loop Equivalent of a for Loop
Encrypting and Decrypting Symbols
Handling Symbols Outside of the Symbol Set
Displaying and Copying the Translated String
6
HACKING THE CAESAR CIPHER WITH BRUTE-FORCE
Source Code for the Caesar Cipher Hacker Program
Sample Run of the Caesar Cipher Hacker Program
Looping with the range() Function
Using String Formatting to Display the Key and Decrypted Messages
7
ENCRYPTING WITH THE TRANSPOSITION CIPHER
How the Transposition Cipher Works
Creating the Encryption Program
Source Code for the Transposition Cipher Encryption Program
Sample Run of the Transposition Cipher Encryption Program
Creating Your Own Functions with def Statements
Defining a Function that Takes Arguments with Parameters
Changes to Parameters Exist Only Inside the Function
Passing the Key and Message As Arguments
Reassigning the Items in Lists
Using len() and the in Operator with Lists
List Concatenation and Replication with the + and * Operators
The Transposition Encryption Algorithm
Augmented Assignment Operators
Moving currentIndex Through the Message
Return Values and return Statements
Returning the Encrypted Ciphertext
8
DECRYPTING WITH THE TRANSPOSITION CIPHER
How to Decrypt with the Transposition Cipher on Paper
Source Code for the Transposition Cipher Decryption Program
Sample Run of the Transposition Cipher Decryption Program
Importing Modules and Setting Up the main() Function
Decrypting the Message with the Key
The round(), math.ceil(), and math.floor() Functions
Adjusting the column and row Variables
9
PROGRAMMING A PROGRAM TO TEST YOUR PROGRAM
Source Code for the Transposition Cipher Tester Program
Sample Run of the Transposition Cipher Tester Program
Duplicating a String a Random Number of Times
Using copy.deepcopy() to Duplicate a List
Checking Whether the Cipher Worked and Ending the Program
10
ENCRYPTING AND DECRYPTING FILES
Source Code for the Transposition File Cipher Program
Sample Run of the Transposition File Cipher Program
Setting Up the main() Function
Checking Whether a File Exists
Checking Whether the Input File Exists with the os.path.exists() Function
Using String Methods to Make User Input More Flexible
The upper(), lower(), and title() String Methods
The startswith() and endswith() String Methods
Using These String Methods in the Program
Measuring the Time It Took to Encrypt or Decrypt
The time Module and time.time() Function
Using the time.time() Function in the Program
11
DETECTING ENGLISH PROGRAMMATICALLY
How Can a Computer Understand English?
Source Code for the Detect English Module
Sample Run of the Detect English Module
Instructions and Setting Up Constants
The Difference Between Dictionaries and Lists
Adding or Changing Items in a Dictionary
Using the len() Function with Dictionaries
Using the in Operator with Dictionaries
Finding Items Is Faster with Dictionaries than with Lists
Using for Loops with Dictionaries
Implementing the Dictionary File
Splitting the Dictionary File into Individual Words
Counting the Number of English Words in message
Counting the English Word Matches
The float(), int(), and str() Functions and Integer Division
Finding the Ratio of English Words in the Message
Removing Non-Letter Characters
12
HACKING THE TRANSPOSITION CIPHER
Source Code of the Transposition Cipher Hacker Program
Sample Run of the Transposition Cipher Hacker Program
Multiline Strings with Triple Quotes
Displaying the Results of Hacking the Message
Applying the strip() String Method
13
A MODULAR ARITHMETIC MODULE FOR THE AFFINE CIPHER
Finding Factors to Calculate the Greatest Common Divisor
Euclid’s Algorithm for Finding the GCD
Understanding How the Multiplicative and Affine Ciphers Work
Choosing Valid Multiplicative Keys
Encrypting with the Affine Cipher
Decrypting with the Affine Cipher
Source Code for the Cryptomath Module
14
PROGRAMMING THE AFFINE CIPHER
Source Code for the Affine Cipher Program
Sample Run of the Affine Cipher Program
Setting Up Modules, Constants, and the main() Function
Calculating and Validating the Keys
How Many Keys Can the Affine Cipher Have?
Writing the Encryption Function
Writing the Decryption Function
Source Code for the Affine Cipher Hacker Program
Sample Run of the Affine Cipher Hacker Program
Setting Up Modules, Constants, and the main() Function
The Affine Cipher Hacking Function
Calculating the Total Number of Possible Keys
16
PROGRAMMING THE SIMPLE SUBSTITUTION CIPHER
How the Simple Substitution Cipher Works
Source Code for the Simple Substitution Cipher Program
Sample Run of the Simple Substitution Cipher Program
Setting Up Modules, Constants, and the main() Function
The translateMessage() Function
The isupper() and islower() String Methods
Preserving Cases with isupper()
17
HACKING THE SIMPLE SUBSTITUTION CIPHER
Using Word Patterns to Decrypt
Finding Potential Decryption Letters
Overview of the Hacking Process
Source Code for the Simple Substitution Hacking Program
Sample Run of the Simple Substitution Hacking Program
Setting Up Modules and Constants
Finding Characters with Regular Expressions
Setting Up the main() Function
Displaying Hacking Results to the User
Creating a Cipherletter Mapping
How the Letter-Mapping Helper Functions Work
Identifying Solved Letters in Mappings
Testing the removeSolvedLetterFromMapping() Function
Decrypting in the Interactive Shell
18
PROGRAMMING THE VIGENÈRE CIPHER
Using Multiple Letter Keys in the Vigenère Cipher
Longer Vigenère Keys Are More Secure
Choosing a Key That Prevents Dictionary Attacks
Source Code for the Vigenère Cipher Program
Sample Run of the Vigenère Cipher Program
Setting Up Modules, Constants, and the main() Function
Building Strings with the List-Append-Join Process
Encrypting and Decrypting the Message
Analyzing the Frequency of Letters in Text
Calculating the Frequency Match Score for the Simple Substitution Cipher
Calculating the Frequency Match Score for the Transposition Cipher
Using Frequency Analysis on the Vigenère Cipher
Source Code for Matching Letter Frequencies
Storing the Letters in ETAOIN Order
Counting the Letters in a Message
Getting the First Member of a Tuple
Ordering the Letters in the Message by Frequency
Counting the Letters with getLetterCount()
Creating a Dictionary of Frequency Counts and Letter Lists
Sorting the Letter Lists in Reverse ETAOIN Order
Sorting the Dictionary Lists by Frequency
Creating a List of the Sorted Letters
Calculating the Frequency Match Score of the Message
Using a Dictionary Attack to Brute-Force the Vigenère Cipher
Source Code for the Vigenère Dictionary Hacker Program
Sample Run of the Vigenère Dictionary Hacker Program
About the Vigenère Dictionary Hacker Program
Using Kasiski Examination to Find the Key’s Length
Getting Every Nth Letters from a String
Using Frequency Analysis to Break Each Subkey
Brute-Forcing Through the Possible Keys
Source Code for the Vigenère Hacking Program
Sample Run of the Vigenère Hacking Program
Importing Modules and Setting Up the main() Function
Calculating the Factors of the Spacings
Removing Duplicates with the set() Function
Removing Duplicate Factors and Sorting the List
Finding the Most Common Factors
Finding the Most Likely Key Lengths
Extending the repeatedSeqSpacings Dictionary
Getting the Factors from factorsByCount
Getting Letters Encrypted with the Same Subkey
Attempting Decryption with a Likely Key Length
The end Keyword Argument for print()
Running the Program in Silent Mode or Printing Information to the User
Finding Possible Combinations of Subkeys
Printing the Decrypted Text with the Correct Casing
Breaking Out of the Loop When a Potential Key Is Found
Brute-Forcing All Other Key Lengths
Modifying the Constants of the Hacking Program
The Unbreakable One-Time Pad Cipher
Making Key Length Equal Message Length
Why the Two-Time Pad Is the Vigenère Cipher
22
FINDING AND GENERATING PRIME NUMBERS
Source Code for the Prime Numbers Module
Sample Run of the Prime Numbers Module
How the Trial Division Algorithm Works
Implementing the Trial Division Algorithm Test
Generating Prime Numbers with the Sieve of Eratosthenes
The Rabin-Miller Primality Algorithm
Generating Large Prime Numbers
23
GENERATING KEYS FOR THE PUBLIC KEY CIPHER
The Problem with Authentication
Steps for Generating Public and Private Keys
Source Code for the Public Key Generation Program
Sample Run of the Public Key Generation Program
Generating Keys with the generateKey() Function
Creating Key Files with the makeKeyFiles() Function
24
PROGRAMMING THE PUBLIC KEY CIPHER
How the Public Key Cipher Works
Converting a String into a Block
The Mathematics of Public Key Cipher Encryption and Decryption
Converting a Block to a String
Why We Can’t Hack the Public Key Cipher
Source Code for the Public Key Cipher Program
Sample Run of the Public Key Cipher Program
How the Program Determines Whether to Encrypt or Decrypt
Converting Strings to Blocks with getBlocksFromText()
Using getTextFromBlocks() to Decrypt
Using the insert() List Method
Merging the Message List into One String
Writing the encryptMessage() Function
Writing the decryptMessage() Function
Reading in the Public and Private Keys from Their Key Files
Writing the Encryption to a File