Hacking Secret Ciphers with Python

(Ann) #1

  • About This Book

  • Table of Contents

  • Chapter 1 - Making Paper Cryptography Tools

    • What is Cryptography?

    • Codes vs. Ciphers

    • Making a Paper Cipher Wheel

    • A Virtual Cipher Wheel

    • How to Encrypt with the Cipher Wheel

    • How to Decrypt with the Cipher Wheel

    • A Different Cipher Tool: The St. Cyr Slide

    • Practice Exercises, Chapter 1, Set A

    • Doing Cryptography without Paper Tools

    • Practice Exercises, Chapter 1, Set B

    • Double-Strength Encryption?.....................................................................................................................................

    • Programming a Computer to do Encryption



  • Chapter 2 - Installing Python

    • Downloading and Installing Python

    • Downloading pyperclip.py

    • Starting IDLE

    • The Featured Programs

    • Line Numbers and Spaces

    • Text Wrapping in This Book

    • Tracing the Program Online

    • Checking Your Typed Code with the Online Diff Tool

    • Copying and Pasting Text

    • More Info Links

    • Programming and Cryptography



  • Chapter 3 - The Interactive Shell

    • Some Simple Math Stuff

    • Integers and Floating Point Values

    • Expressions

    • Order of Operations

    • Evaluating Expressions

    • Errors are Okay!

    • Practice Exercises, Chapter 3, Set A

    • Every Value has a Data Type

    • Storing Values in Variables with Assignment Statements

    • Overwriting Variables

    • Using More Than One Variable

    • Variable Names

    • Practice Exercises, Chapter 3, Set B

    • Summary - But When Are We Going to Start Hacking?............................................................................................



  • Chapter 4 - Strings and Writing Programs

    • Strings

    • String Concatenation with the + Operator

    • String Replication with the * Operator

    • Printing Values with the print() Function

    • Escape Characters

    • Quotes and Double Quotes

    • Practice Exercises, Chapter 4, Set A

    • Indexing

    • Negative Indexes

    • Slicing

    • Blank Slice Indexes....................................................................................................................................................

    • Practice Exercises, Chapter 4, Set B

    • Writing Programs in IDLE’s File Editor

    • Hello World!

    • Source Code of Hello World

    • Saving Your Program

    • Running Your Program

    • Opening The Programs You’ve Saved

    • How the “Hello World” Program Works

    • Comments

    • Functions....................................................................................................................................................................

    • The print() function

    • The input() function

    • Ending the Program

    • Practice Exercises, Chapter 4, Set C

    • Summary



  • Chapter 5 - The Reverse Cipher

    • The Reverse Cipher....................................................................................................................................................

    • Source Code of the Reverse Cipher Program

    • Sample Run of the Reverse Cipher Program

    • Checking Your Source Code with the Online Diff Tool

    • How the Program Works............................................................................................................................................

    • The len() Function

    • Introducing the while Loop

    • The Boolean Data Type

    • Comparison Operators

    • Conditions

    • Blocks

    • The while Loop Statement

    • “Growing” a String

    • Tracing Through the Program, Step by Step

    • Using input() In Our Programs

    • Practice Exercises, Chapter 5, Section A

    • Summary



  • Chapter 6 - The Caesar Cipher

    • Implementing a Program

    • Source Code of the Caesar Cipher Program

    • Sample Run of the Caesar Cipher Program

    • Checking Your Source Code with the Online Diff Tool

    • Practice Exercises, Chapter 6, Set A

    • How the Program Works............................................................................................................................................

    • Importing Modules with the import Statement

    • Constants....................................................................................................................................................................

    • The upper() and lower() String Methods

    • The for Loop Statement

    • A while Loop Equivalent of a for Loop

    • Practice Exercises, Chapter 6, Set B

    • The if Statement

    • The else Statement

    • The elif Statement

    • The in and not in Operators

    • The find() String Method

    • Practice Exercises, Chapter 6, Set C

    • Back to the Code

    • Displaying and Copying the Encrypted/Decrypted String

    • Encrypt Non-Letter Characters

    • Summary



  • Chapter 7 - Hacking the Caesar Cipher with the Brute-Force Technique

    • Hacking Ciphers

    • The Brute-Force Attack

    • Source Code of the Caesar Cipher Hacker Program

    • Sample Run of the Caesar Cipher Hacker Program

    • How the Program Works............................................................................................................................................

    • The range() Function

    • Back to the Code

    • String Formatting

    • Practice Exercises, Chapter 7, Set A

    • Summary



  • Chapter 8 - Encrypting with the Transposition Cipher

    • Encrypting with the Transposition Cipher

    • Practice Exercises, Chapter 8, Set A

    • A Transposition Cipher Encryption Program

    • Source Code of the Transposition Cipher Encryption Program

    • Sample Run of the Transposition Cipher Encryption Program

    • How the Program Works..........................................................................................................................................

    • Creating Your Own Functions with def Statements

    • The Program’s main() Function

    • Parameters

    • Variables in the Global and Local Scope

    • The global Statement

    • Practice Exercises, Chapter 8, Set B

    • The List Data Type

    • Using the list() Function to Convert Range Objects to Lists

    • Reassigning the Items in Lists..................................................................................................................................

    • Reassigning Characters in Strings

    • Lists of Lists

    • Practice Exercises, Chapter 8, Set C

    • Using len() and the in Operator with Lists

    • List Concatenation and Replication with the + and * Operators..............................................................................

    • Practice Exercises, Chapter 8, Set D

    • The Transposition Encryption Algorithm

    • Augmented Assignment Operators

    • Back to the Code

    • The join() String Method

    • Return Values and return Statements

    • Practice Exercises, Chapter 8, Set E

    • Back to the Code

    • The Special name Variable

    • Key Size and Message Length

    • Summary



  • Chapter 9 - Decrypting with the Transposition Cipher

    • Decrypting with the Transposition Cipher on Paper

    • Practice Exercises, Chapter 9, Set A

    • A Transposition Cipher Decryption Program...........................................................................................................

    • Source Code of the Transposition Cipher Decryption Program

    • How the Program Works..........................................................................................................................................

    • The math.ceil(), math.floor() and round() Functions

    • The and and or Boolean Operators

    • Practice Exercises, Chapter 9, Set B

    • Truth Tables

    • The and and or Operators are Shortcuts

    • Order of Operations for Boolean Operators

    • Back to the Code

    • Practice Exercises, Chapter 9, Set C

    • Summary



  • Chapter 10 - Programming a Program to Test Our Program

    • Source Code of the Transposition Cipher Tester Program

    • Sample Run of the Transposition Cipher Tester Program

    • How the Program Works..........................................................................................................................................

    • Pseudorandom Numbers and the random.seed() Function

    • The random.randint() Function

    • References

    • The copy.deepcopy() Functions

    • Practice Exercises, Chapter 10, Set A

    • The random.shuffle() Function

    • Randomly Scrambling a String

    • Back to the Code

    • The sys.exit() Function

    • Testing Our Test Program

    • Summary



  • Chapter 11 - Encrypting and Decrypting Files

    • Plain Text Files

    • Source Code of the Transposition File Cipher Program

    • Sample Run of the Transposition File Cipher Program

    • Reading From Files

    • Writing To Files

    • How the Program Works..........................................................................................................................................

    • The os.path.exists() Function

    • The startswith() and endswith() String Methods

    • The title() String Method

    • The time Module and time.time() Function

    • Back to the Code

    • Practice Exercises, Chapter 11, Set A

    • Summary



  • Chapter 12 - Detecting English Programmatically

    • How Can a Computer Understand English?

    • Practice Exercises, Chapter 12, Section A

    • The Detect English Module

    • Source Code for the Detect English Module

    • How the Program Works..........................................................................................................................................

    • Dictionaries and the Dictionary Data Type

    • Adding or Changing Items in a Dictionary

    • Practice Exercises, Chapter 12, Set B

    • Using the len() Function with Dictionaries

    • Using the in Operator with Dictionaries

    • Using for Loops with Dictionaries

    • Practice Exercises, Chapter 12, Set C

    • The Difference Between Dictionaries and Lists

    • Finding Items is Faster with Dictionaries Than Lists

    • The split() Method

    • The None Value

    • Back to the Code

    • “Divide by Zero” Errors

    • The float(), int(), and str() Functions and Integer Division

    • Practice Exercises, Chapter 12, Set D

    • Back to the Code

    • The append() List Method

    • Default Arguments

    • Calculating Percentage

    • Practice Exercises, Chapter 12, Set E

    • Summary



  • Chapter 13 - Hacking the Transposition Cipher

    • Source Code of the Transposition Cipher Hacker Program

    • Sample Run of the Transposition Breaker Program

    • How the Program Works..........................................................................................................................................

    • Multi-line Strings with Triple Quotes

    • Back to the Code

    • The strip() String Method

    • Practice Exercises, Chapter 13, Set A

    • Summary



  • Chapter 14 - Modular Arithmetic with the Multiplicative and Affine Ciphers

    • Oh No Math!

    • Math Oh Yeah!

    • Modular Arithmetic (aka Clock Arithmetic)

    • The % Mod Operator

    • Practice Exercises, Chapter 14, Set A

    • GCD: Greatest Common Divisor (aka Greatest Common Factor)

    • Visualize Factors and GCD with Cuisenaire Rods

    • Practice Exercises, Chapter 14, Set B

    • Multiple Assignment

    • Swapping Values with the Multiple Assignment Trick

    • Euclid’s Algorithm for Finding the GCD of Two Numbers.....................................................................................

    • “Relatively Prime”

    • Practice Exercises, Chapter 14, Set C

    • The Multiplicative Cipher

    • Practice Exercises, Chapter 14, Set D

    • Multiplicative Cipher + Caesar Cipher = The Affine Cipher

    • The First Affine Key Problem..................................................................................................................................

    • Decrypting with the Affine Cipher...........................................................................................................................

    • Finding Modular Inverses

    • The // Integer Division Operator

    • Source Code of the cryptomath Module

    • Practice Exercises, Chapter 14, Set E

    • Summary



  • Chapter 15 - The Affine Cipher

    • Source Code of the Affine Cipher Program

    • Sample Run of the Affine Cipher Program

    • Practice Exercises, Chapter 15, Set A

    • How the Program Works..........................................................................................................................................

    • Splitting One Key into Two Keys

    • The Tuple Data Type

    • Input Validation on the Keys

    • The Affine Cipher Encryption Function

    • The Affine Cipher Decryption Function

    • Generating Random Keys

    • The Second Affine Key Problem: How Many Keys Can the Affine Cipher Have?

    • Summary



  • Chapter 16 - Hacking the Affine Cipher

    • Source Code of the Affine Cipher Hacker Program

    • Sample Run of the Affine Cipher Hacker Program

    • How the Program Works..........................................................................................................................................

    • The Affine Cipher Hacking Function

    • The ** Exponent Operator

    • The continue Statement

    • Practice Exercises, Chapter 16, Set A

    • Summary



  • Chapter 17 - The Simple Substitution Cipher

    • The Simple Substitution Cipher with Paper and Pencil

    • Practice Exercises, Chapter 17, Set A

    • Source Code of the Simple Substitution Cipher

    • Sample Run of the Simple Substitution Cipher Program

    • How the Program Works..........................................................................................................................................

    • The Program’s main() Function

    • The sort() List Method

    • Wrapper Functions

    • The Program’s translateMessage() Function

    • The isupper() and islower() String Methods

    • Practice Exercises, Chapter 17, Set B

    • Generating a Random Key

    • Encrypting Spaces and Punctuation

    • Practice Exercises, Chapter 17, Set C

    • Summary



  • Chapter 18 - Hacking the Simple Substitution Cipher

    • Computing Word Patterns

    • Getting a List of Candidates for a Cipherword

    • Practice Exercises, Chapter 18, Set A

    • Source Code of the Word Pattern Module

    • Sample Run of the Word Pattern Module

    • How the Program Works..........................................................................................................................................

    • The pprint.pprint() and pprint.pformat() Functions

    • Building Strings in Python with Lists

    • Calculating the Word Pattern

    • The Word Pattern Program’s main() Function

    • Hacking the Simple Substitution Cipher

    • Source Code of the Simple Substitution Hacking Program......................................................................................

    • Hacking the Simple Substitution Cipher (in Theory)

    • Explore the Hacking Functions with the Interactive Shell

    • How the Program Works..........................................................................................................................................

    • Import All the Things

    • A Brief Intro to Regular Expressions and the sub() Regex Method

    • The Hacking Program’s main() Function

    • Partially Hacking the Cipher

    • Blank Cipherletter Mappings

    • Adding Letters to a Cipherletter Mapping

    • Intersecting Two Letter Mappings

    • Removing Solved Letters from the Letter Mapping

    • Hacking the Simple Substitution Cipher

    • Creating a Key from a Letter Mapping

    • Couldn’t We Just Encrypt the Spaces Too?

    • Summary



  • Chapter 19 - The Vigenère Cipher

    • Le Chiffre Indéchiffrable

    • Multiple “Keys” in the Vigenère Key

    • Source Code of Vigenère Cipher Program

    • Sample Run of the Vigenère Cipher Program

    • How the Program Works..........................................................................................................................................

    • Summary



  • Chapter 20 - Frequency Analysis

    • The Code for Matching Letter Frequencies

    • How the Program Works..........................................................................................................................................

    • The Most Common Letters, “ETAOIN”

    • The Program’s getLettersCount() Function

    • The Program’s getItemAtIndexZero() Function

    • The Program’s getFrequencyOrder() Function

    • The sort() Method’s key and reverse Keyword Arguments

    • Passing Functions as Values

    • Converting Dictionaries to Lists with the keys(), values(), items() Dictionary Methods

    • Sorting the Items from a Dictionary

    • The Program’s englishFreqMatchScore() Function

    • Summary



  • Chapter 21 - Hacking the Vigenère Cipher

    • The Dictionary Attack

    • Source Code for a Vigenère Dictionary Attack Program

    • Sample Run of the Vigenère Dictionary Hacker Program

    • The readlines() File Object Method

    • The Babbage Attack & Kasiski Examination

    • Kasiski Examination, Step 1 – Find Repeat Sequences’ Spacings

    • Kasiski Examination, Step 2 – Get Factors of Spacings

    • Get Every Nth Letters from a String

    • Frequency Analysis

    • Brute-Force through the Possible Keys

    • Source Code for the Vigenère Hacking Program

    • Sample Run of the Vigenère Hacking Program

    • How the Program Works..........................................................................................................................................

    • Finding Repeated Sequences

    • Calculating Factors

    • Removing Duplicates with the set() Function

    • The Kasiski Examination Algorithm........................................................................................................................

    • The extend() List Method

    • The end Keyword Argument for print()

    • The itertools.product() Function

    • The break Statement

    • Practice Exercises, Chapter 21, Set A

    • Modifying the Constants of the Hacking Program

    • Summary



  • Chapter 22 - The One-Time Pad Cipher

    • The Unbreakable One-Time Pad Cipher

    • Why the One-Time Pad is Unbreakable

    • Beware Pseudorandomness

    • Beware the Two-Time Pad

    • The Two-Time Pad is the Vigenère Cipher

    • Practice Exercises, Chapter 22, Set A

    • Summary



  • Chapter 23 - Finding Prime Numbers

    • Prime Numbers

    • Composite Numbers.................................................................................................................................................

    • Source Code for The Prime Sieve Module

    • How the Program Works..........................................................................................................................................

    • How to Calculate if a Number is Prime

    • The Sieve of Eratosthenes

    • The primeSieve() Function...............................................................................................................................

    • Detecting Prime Numbers

    • Source Code for the Rabin-Miller Module

    • Sample Run of the Rabin Miller Module

    • How the Program Works..........................................................................................................................................

    • The Rabin-Miller Primality Algorithm

    • The New and Improved isPrime() Function

    • Summary



  • Chapter 24 - Public Key Cryptography and the RSA Cipher

    • Public Key Cryptography.........................................................................................................................................

    • The Dangers of “Textbook” RSA

    • A Note About Authentication

    • The Man-In-The-Middle Attack

    • Generating Public and Private Keys.........................................................................................................................

    • Source Code for the RSA Key Generation Program

    • Sample Run of the RSA Key Generation Program

    • How the Key Generation Program Works

    • The Program’s generateKey() Function

    • RSA Key File Format

    • Hybrid Cryptosystems

    • Source Code for the RSA Cipher Program

    • Sample Run of the RSA Cipher Program

    • Practice Exercises, Chapter 24, Set A

    • Digital Signatures

    • How the RSA Cipher Program Works

    • ASCII: Using Numbers to Represent Characters

    • The chr() and ord() Functions

    • Practice Exercises, Chapter 24, Set B

    • Blocks

    • Converting Strings to Blocks with getBlocksFromText()

    • The encode() String Method and the Bytes Data Type

    • The bytes() Function and decode() Bytes Method

    • Practice Exercises, Chapter 24, Set C

    • Back to the Code

    • The min() and max() Functions

    • The insert() List Method

    • The Mathematics of RSA Encrypting and Decrypting

    • The pow() Function

    • Reading in the Public & Private Keys from their Key Files

    • The Full RSA Encryption Process

    • The Full RSA Decryption Process

    • Practice Exercises, Chapter 24, Set D

    • Why Can’t We Hack the RSA Cipher

    • Summary



  • About the Author

Free download pdf