Book description
"Learn how to program in Python while making and breaking ciphers—algorithms used to create and send secret messages!
After a crash course in Python programming basics, you’ll learn to make, test, and hack programs that encrypt text with classical ciphers like the transposition cipher and Vigenère cipher. You’ll begin with simple programs for the reverse and Caesar ciphers and then work your way up to public key cryptography, the type of encryption used to secure today’s online transactions, including digital signatures, email, and Bitcoin.
Each program includes the full code and a line-by-line explanation of how things work. By the end of the book, you’ll have learned how to code in Python and you’ll have the clever programs to prove it!
You’ll also learn how to:
• Combine loops, variables, and flow control statements into real working programs• Use dictionary files to instantly detect whether decrypted messages are valid English or gibberish• Create test programs to make sure that your code encrypts and decrypts correctly• Code (and hack!) a working example of the affine cipher, which uses modular arithmetic to encrypt a message• Break ciphers with techniques such as brute-force and frequency analysis
There’s no better way to learn to code than to play with real programs. Cracking Codes with Python makes the learning fun!"
Table of contents
- Cover Page
- Title Page
- Copyright Page
- Dedication
- About the Author
- About the Technical Reviewers
- Brief Contents
- Contents in Detail
- Acknowledgments
- Introduction
- Chapter 1: Making Paper Cryptography Tools
- Chapter 2: Programming in the Interactive Shell
-
Chapter 3: Strings and Writing Programs
- Working with Text Using String Values
- Printing Values with the print() Function
- Printing Escape Characters
- Quotes and Double Quotes
- 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
- How the “Hello, World!” Program Works
- Summary
- Practice Questions
- Chapter 4: The Reverse Cipher
-
Chapter 5: The Caesar Cipher
- Source Code for the Caesar Cipher Program
- Sample Run of the Caesar Cipher Program
- Importing Modules and Setting Up Variables
- Constants and Variables
- The for Loop Statement
- The if Statement
- The in and not in Operators
- The find() String Method
- Encrypting and Decrypting Symbols
- Displaying and Copying the Translated String
- Encrypting Other Symbols
- Summary
- Practice Questions
- Chapter 6: Hacking the Caesar Cipher with Brute-Force
-
Chapter 7: Encrypting with the Transposition Cipher
- How the Transposition Cipher Works
- Source Code for the Transposition Cipher Encryption Program
- Sample Run of the Transposition Cipher Encryption Program
- Creating Your Own Functions with def Statements
- Passing the Key and Message As Arguments
- The List Data Type
- The Transposition Encryption Algorithm
- Augmented Assignment Operators
- Moving currentIndex Through the Message
- The join() String Method
- Return Values and return Statements
- The __name__ Variable
- Summary
- Practice Questions
-
Chapter 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
- Calling the main() Function
- Summary
- Practice Questions
-
Chapter 9: Programming a Program to Test Your Program
- Source Code for the Transposition Cipher Tester Program
- Sample Run of the Transposition Cipher Tester Program
- Importing the Modules
- Creating Pseudorandom Numbers
- Creating a Random String
- Testing Each Message
- Checking Whether the Cipher Worked and Ending the Program
- Calling the main() Function
- Testing the Test Program
- Summary
- Practice Questions
-
Chapter 10: Encrypting and Decrypting Files
- Plain Text Files
- Source Code for the Transposition File Cipher Program
- Sample Run of the Transposition File Cipher Program
- Working with Files
- Setting Up the main() Function
- Checking Whether a File Exists
- Using String Methods to Make User Input More Flexible
- Reading the Input File
- Measuring the Time It Took to Encrypt or Decrypt
- Writing the Output File
- Calling the main() Function
- Summary
- Practice Questions
-
Chapter 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 Dictionary Data Type
- Implementing the Dictionary File
- Counting the Number of English Words in message
- Removing Non-Letter Characters
- Detecting English Words
- Summary
- Practice Questions
- Chapter 12: Hacking the Transposition Cipher
- Chapter 13: A Modular Arithmetic Module for the Affine Cipher
-
Chapter 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
- Writing the Encryption Function
- Writing the Decryption Function
- Generating Random Keys
- Calling the main() Function
- Summary
- Practice Questions
- Chapter 15: Hacking the Affine Cipher
-
Chapter 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 sort() List Method
- Wrapper Functions
- The translateMessage() Function
- Generating a Random Key
- Calling the main() Function
- Summary
- Practice Questions
-
Chapter 17: Hacking the Simple Substitution Cipher
- Using Word Patterns to Decrypt
- Overview of the Hacking Process
- The Word Pattern Modules
- 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
- The hackSimpleSub() Function
- Calling the main() Function
- Summary
- Practice Questions
-
Chapter 18: Programming the Vigenère Cipher
- Using Multiple Letter Keys in the Vigenère Cipher
- 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
- Calling the main() Function
- Summary
- Practice Questions
-
Chapter 19: Frequency Analysis
- Analyzing the Frequency of Letters in Text
- Matching Letter Frequencies
- 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
- Calculating the Frequency Match Score of the Message
- Summary
- Practice Questions
-
Chapter 20: Hacking the Vigenère Cipher
- 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
- Source Code for the Vigenère Hacking Program
- Sample Run of the Vigenère Hacking Program
- Importing Modules and Setting Up the main() Function
- Finding Repeated Sequences
- Calculating the Factors of the Spacings
- Finding the Most Likely Key Lengths
- Getting Letters Encrypted with the Same Subkey
- Attempting Decryption with a Likely Key Length
- Returning the Hacked Message
- Calling the main() Function
- Modifying the Constants of the Hacking Program
- Summary
- Practice Questions
- Chapter 21: The One-Time Pad Cipher
-
Chapter 22: Finding and Generating Prime Numbers
- What Is a Prime Number?
- 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
- The Sieve of Eratosthenes
- Generating Prime Numbers with the Sieve of Eratosthenes
- The Rabin-Miller Primality Algorithm
- Finding Large Prime Numbers
- Generating Large Prime Numbers
- Summary
- Practice Questions
-
Chapter 23: Generating Keys for the Public Key Cipher
- Public Key Cryptography
- 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
- Creating the main() Function
- Generating Keys with the generateKey() Function
- Creating Key Files with the makeKeyFiles() Function
- Calling the main() Function
- Hybrid Cryptosystems
- Summary
- Practice Questions
-
Chapter 24: Programming the Public Key Cipher
- How the Public Key Cipher Works
- Source Code for the Public Key Cipher Program
- Sample Run of the Public Key Cipher Program
- Setting Up the Program
- How the Program Determines Whether to Encrypt or Decrypt
- Converting Strings to Blocks with getBlocksFromText()
- Using getTextFromBlocks() to Decrypt
- 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
- Decrypting from a File
- Calling the main() Function
- Summary
- Appendix Debugging Python Code
- Index
- Resources
Product information
- Title: Cracking Codes with Python
- Author(s):
- Release date: January 2018
- Publisher(s): No Starch Press
- ISBN: 9781593278229
You might also like
book
TCP/IP Guide
The TCP/IP Guide is both an encyclopedic and comprehensible guide to the TCP/IP protocol suite that …
book
Learning Microsoft Azure
If your organization plans to modernize services and move to the cloud from legacy software or …
book
Bayesian Statistics the Fun Way
Probability and statistics are increasingly important in a huge range of professions. But many people use …
book
Building Microservices, 2nd Edition
As organizations shift from monolithic applications to smaller, self-contained microservices, distributed systems have become more fine-grained. …