O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Cracking Codes with Python

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

  1. Cover Page
  2. Title Page
  3. Copyright Page
  4. Dedication
  5. About the Author
  6. About the Technical Reviewers
  7. Brief Contents
  8. Contents in Detail
  9. Acknowledgments
  10. Introduction
    1. Who Should Read This Book?
    2. What’s in This Book?
    3. How to Use This Book
    4. Downloading and Installing Python
    5. Downloading pyperclip.py
    6. Starting IDLE
    7. Summary
  11. Chapter 1: Making Paper Cryptography Tools
    1. What Is Cryptography?
    2. Codes vs. Ciphers
    3. The Caesar Cipher
    4. Why Double Encryption Doesn’t Work
    5. Summary
    6. Practice Questions
  12. Chapter 2: Programming in the Interactive Shell
    1. Some Simple Math Expressions
    2. Storing Values with Variables
    3. Summary
    4. Practice Questions
  13. Chapter 3: Strings and Writing Programs
    1. Working with Text Using String Values
    2. Printing Values with the print() Function
    3. Printing Escape Characters
    4. Quotes and Double Quotes
    5. Writing Programs in IDLE’s File Editor
    6. Source Code for the “Hello, World!” Program
    7. Checking Your Source Code with the Online Diff Tool
    8. Using IDLE to Access Your Program Later
    9. How the “Hello, World!” Program Works
    10. Summary
    11. Practice Questions
  14. Chapter 4: The Reverse Cipher
    1. Source Code for the Reverse Cipher Program
    2. Sample Run of the Reverse Cipher Program
    3. Setting Up Comments and Variables
    4. Finding the Length of a String
    5. Introducing the while Loop
    6. Improving the Program with an input() Prompt
    7. Summary
    8. Practice Questions
  15. Chapter 5: The Caesar Cipher
    1. Source Code for the Caesar Cipher Program
    2. Sample Run of the Caesar Cipher Program
    3. Importing Modules and Setting Up Variables
    4. Constants and Variables
    5. The for Loop Statement
    6. The if Statement
    7. The in and not in Operators
    8. The find() String Method
    9. Encrypting and Decrypting Symbols
    10. Displaying and Copying the Translated String
    11. Encrypting Other Symbols
    12. Summary
    13. Practice Questions
  16. Chapter 6: Hacking the Caesar Cipher with Brute-Force
    1. Source Code for the Caesar Cipher Hacker Program
    2. Sample Run of the Caesar Cipher Hacker Program
    3. Setting Up Variables
    4. Looping with the range() Function
    5. Decrypting the Message
    6. Using String Formatting to Display the Key and Decrypted Messages
    7. Summary
    8. Practice Question
  17. Chapter 7: Encrypting with the Transposition Cipher
    1. How the Transposition Cipher Works
    2. Source Code for the Transposition Cipher Encryption Program
    3. Sample Run of the Transposition Cipher Encryption Program
    4. Creating Your Own Functions with def Statements
    5. Passing the Key and Message As Arguments
    6. The List Data Type
    7. The Transposition Encryption Algorithm
    8. Augmented Assignment Operators
    9. Moving currentIndex Through the Message
    10. The join() String Method
    11. Return Values and return Statements
    12. The __name__ Variable
    13. Summary
    14. Practice Questions
  18. Chapter 8: Decrypting with the Transposition Cipher
    1. How to Decrypt with the Transposition Cipher on Paper
    2. Source Code for the Transposition Cipher Decryption Program
    3. Sample Run of the Transposition Cipher Decryption Program
    4. Importing Modules and Setting Up the main() Function
    5. Decrypting the Message with the Key
    6. Calling the main() Function
    7. Summary
    8. Practice Questions
  19. Chapter 9: Programming a Program to Test Your Program
    1. Source Code for the Transposition Cipher Tester Program
    2. Sample Run of the Transposition Cipher Tester Program
    3. Importing the Modules
    4. Creating Pseudorandom Numbers
    5. Creating a Random String
    6. Testing Each Message
    7. Checking Whether the Cipher Worked and Ending the Program
    8. Calling the main() Function
    9. Testing the Test Program
    10. Summary
    11. Practice Questions
  20. Chapter 10: Encrypting and Decrypting Files
    1. Plain Text Files
    2. Source Code for the Transposition File Cipher Program
    3. Sample Run of the Transposition File Cipher Program
    4. Working with Files
    5. Setting Up the main() Function
    6. Checking Whether a File Exists
    7. Using String Methods to Make User Input More Flexible
    8. Reading the Input File
    9. Measuring the Time It Took to Encrypt or Decrypt
    10. Writing the Output File
    11. Calling the main() Function
    12. Summary
    13. Practice Questions
  21. Chapter 11: Detecting English Programmatically
    1. How Can a Computer Understand English?
    2. Source Code for the Detect English Module
    3. Sample Run of the Detect English Module
    4. Instructions and Setting Up Constants
    5. The Dictionary Data Type
    6. Implementing the Dictionary File
    7. Counting the Number of English Words in message
    8. Removing Non-Letter Characters
    9. Detecting English Words
    10. Summary
    11. Practice Questions
  22. Chapter 12: Hacking the Transposition Cipher
    1. Source Code of the Transposition Cipher Hacker Program
    2. Sample Run of the Transposition Cipher Hacker Program
    3. Importing the Modules
    4. Multiline Strings with Triple Quotes
    5. Displaying the Results of Hacking the Message
    6. Getting the Hacked Message
    7. Calling the main() Function
    8. Summary
    9. Practice Questions
  23. Chapter 13: A Modular Arithmetic Module for the Affine Cipher
    1. Modular Arithmetic
    2. The Modulo Operator
    3. Finding Factors to Calculate the Greatest Common Divisor
    4. Multiple Assignment
    5. Euclid’s Algorithm for Finding the GCD
    6. Understanding How the Multiplicative and Affine Ciphers Work
    7. Source Code for the Cryptomath Module
    8. Summary
    9. Practice Questions
  24. Chapter 14: Programming the Affine Cipher
    1. Source Code for the Affine Cipher Program
    2. Sample Run of the Affine Cipher Program
    3. Setting Up Modules, Constants, and the main() Function
    4. Calculating and Validating the Keys
    5. Writing the Encryption Function
    6. Writing the Decryption Function
    7. Generating Random Keys
    8. Calling the main() Function
    9. Summary
    10. Practice Questions
  25. Chapter 15: Hacking the Affine Cipher
    1. Source Code for the Affine Cipher Hacker Program
    2. Sample Run of the Affine Cipher Hacker Program
    3. Setting Up Modules, Constants, and the main() Function
    4. The Affine Cipher Hacking Function
    5. Calling the main() Function
    6. Summary
    7. Practice Questions
  26. Chapter 16: Programming the Simple Substitution Cipher
    1. How the Simple Substitution Cipher Works
    2. Source Code for the Simple Substitution Cipher Program
    3. Sample Run of the Simple Substitution Cipher Program
    4. Setting Up Modules, Constants, and the main() Function
    5. The sort() List Method
    6. Wrapper Functions
    7. The translateMessage() Function
    8. Generating a Random Key
    9. Calling the main() Function
    10. Summary
    11. Practice Questions
  27. Chapter 17: Hacking the Simple Substitution Cipher
    1. Using Word Patterns to Decrypt
    2. Overview of the Hacking Process
    3. The Word Pattern Modules
    4. Source Code for the Simple Substitution Hacking Program
    5. Sample Run of the Simple Substitution Hacking Program
    6. Setting Up Modules and Constants
    7. Finding Characters with Regular Expressions
    8. Setting Up the main() Function
    9. Displaying Hacking Results to the User
    10. Creating a Cipherletter Mapping
    11. The hackSimpleSub() Function
    12. Calling the main() Function
    13. Summary
    14. Practice Questions
  28. Chapter 18: Programming the Vigenère Cipher
    1. Using Multiple Letter Keys in the Vigenère Cipher
    2. Source Code for the Vigenère Cipher Program
    3. Sample Run of the Vigenère Cipher Program
    4. Setting Up Modules, Constants, and the main() Function
    5. Building Strings with the List-Append-Join Process
    6. Encrypting and Decrypting the Message
    7. Calling the main() Function
    8. Summary
    9. Practice Questions
  29. Chapter 19: Frequency Analysis
    1. Analyzing the Frequency of Letters in Text
    2. Matching Letter Frequencies
    3. Source Code for Matching Letter Frequencies
    4. Storing the Letters in ETAOIN Order
    5. Counting the Letters in a Message
    6. Getting the First Member of a Tuple
    7. Ordering the Letters in the Message by Frequency
    8. Calculating the Frequency Match Score of the Message
    9. Summary
    10. Practice Questions
  30. Chapter 20: Hacking the Vigenère Cipher
    1. Using a Dictionary Attack to Brute-Force the Vigenère Cipher
    2. Source Code for the Vigenère Dictionary Hacker Program
    3. Sample Run of the Vigenère Dictionary Hacker Program
    4. About the Vigenère Dictionary Hacker Program
    5. Using Kasiski Examination to Find the Key’s Length
    6. Source Code for the Vigenère Hacking Program
    7. Sample Run of the Vigenère Hacking Program
    8. Importing Modules and Setting Up the main() Function
    9. Finding Repeated Sequences
    10. Calculating the Factors of the Spacings
    11. Finding the Most Likely Key Lengths
    12. Getting Letters Encrypted with the Same Subkey
    13. Attempting Decryption with a Likely Key Length
    14. Returning the Hacked Message
    15. Calling the main() Function
    16. Modifying the Constants of the Hacking Program
    17. Summary
    18. Practice Questions
  31. Chapter 21: The One-Time Pad Cipher
    1. The Unbreakable One-Time Pad Cipher
    2. Summary
    3. Practice Questions
  32. Chapter 22: Finding and Generating Prime Numbers
    1. What Is a Prime Number?
    2. Source Code for the Prime Numbers Module
    3. Sample Run of the Prime Numbers Module
    4. How the Trial Division Algorithm Works
    5. Implementing the Trial Division Algorithm Test
    6. The Sieve of Eratosthenes
    7. Generating Prime Numbers with the Sieve of Eratosthenes
    8. The Rabin-Miller Primality Algorithm
    9. Finding Large Prime Numbers
    10. Generating Large Prime Numbers
    11. Summary
    12. Practice Questions
  33. Chapter 23: Generating Keys for the Public Key Cipher
    1. Public Key Cryptography
    2. The Problem with Authentication
    3. Steps for Generating Public and Private Keys
    4. Source Code for the Public Key Generation Program
    5. Sample Run of the Public Key Generation Program
    6. Creating the main() Function
    7. Generating Keys with the generateKey() Function
    8. Creating Key Files with the makeKeyFiles() Function
    9. Calling the main() Function
    10. Hybrid Cryptosystems
    11. Summary
    12. Practice Questions
  34. Chapter 24: Programming the Public Key Cipher
    1. How the Public Key Cipher Works
    2. Source Code for the Public Key Cipher Program
    3. Sample Run of the Public Key Cipher Program
    4. Setting Up the Program
    5. How the Program Determines Whether to Encrypt or Decrypt
    6. Converting Strings to Blocks with getBlocksFromText()
    7. Using getTextFromBlocks() to Decrypt
    8. Writing the encryptMessage() Function
    9. Writing the decryptMessage() Function
    10. Reading in the Public and Private Keys from Their Key Files
    11. Writing the Encryption to a File
    12. Decrypting from a File
    13. Calling the main() Function
    14. Summary
  35. Appendix Debugging Python Code
    1. How the Debugger Works
    2. Debugging the Reverse Cipher Program
    3. Setting Breakpoints
    4. Summary
  36. Index
  37. Resources