Cracking codes with python by al sweigart

384 74 0
Cracking codes with python by al sweigart

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

CRACKING CODES WITH PYTHON CRACKING CODES WITH PYTHON An Introduction to Building and Breaking Ciphers by Al Sweigart San Francisco CRACKING CODES WITH PYTHON Copyright © 2018 by Al Sweigart Some rights reserved This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 United States License To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/us/ or send a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA ISBN-10: 1-59327-822-5 ISBN-13: 978-1-59327-822-9 Publisher: William Pollock Production Editor: Riley Hoffman Cover Illustration: Josh Ellingson Interior Design: Octopod Studios Developmental Editors: Jan Cash and Annie Choi Technical Reviewers: Ari Lacenski and Jean-Philippe Aumasson Copyeditor: Anne Marie Walker Compositors: Riley Hoffman and Meg Sneeringer Proofreader: Paula L Fleming For information on distribution, translations, or bulk sales, please contact No Starch Press, Inc directly: No Starch Press, Inc 245 8th Street, San Francisco, CA 94103 phone: 1.415.863.9900; info@nostarch.com www.nostarch.com Library of Congress Cataloging-in-Publication Data Names: Sweigart, Al, author Title: Cracking codes with Python : an introduction to building and breaking ciphers / Al Sweigart Description: San Francisco : No Starch Press,Inc., [2018] Identifiers: LCCN 2017035704 (print) | LCCN 2017047589 (ebook) | ISBN 9781593278694 (epub) | ISBN 1593278691 (epub) | ISBN 9781593278229 (pbk.) | ISBN 1593278225 (pbk.) Subjects: LCSH: Data encryption (Computer science) | Python (Computer program language) | Computer security | Hacking Classification: LCC QA76.9.A25 (ebook) | LCC QA76.9.A25 S9317 2018 (print) | DDC 005.8/7 dc23 LC record available at https://lccn.loc.gov/2017035704 No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc Other product and company names mentioned herein may be the trademarks of their respective owners Rather than use a trademark symbol with every occurrence of a trademarked name, we are using the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark The information in this book is distributed on an “As Is” basis, without warranty While every precaution has been taken in the preparation of this work, neither the author nor No Starch Press, Inc shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in it Dedicated to Aaron Swartz, 1986–2013 “Aaron was part of an army of citizens that believes democracy only works when the citizenry are informed, when we know about our rights—and our obligations An army that believes we must make justice and knowledge available to all—not just the well born or those that have grabbed the reins of power—so that we may govern ourselves more wisely When I see our army, I see Aaron Swartz and my heart is broken We have truly lost one of our better angels.” —Carl Malamud About the Author Al Sweigart is a software developer and tech book author living in San Francisco Python is his favorite programming language, and he is the developer of several open source modules for it His other books are freely available under a Creative Commons license on his website https://inventwithpython.com/ His cat weighs 12 pounds About the Technical Reviewers Ari Lacenski creates mobile apps and Python software She lives in Seattle Jean-Philippe Aumasson (Chapters 22–24) is Principal Research Engineer at Kudelski Security, Switzerland He speaks regularly at information security conferences such as Black Hat, DEF CON, Troopers, and Infiltrate He is the author of Serious Cryptography (No Starch Press, 2017) BRIEF CONTENTS Acknowledgments Introduction Chapter 1: Making Paper Cryptography Tools Chapter 2: Programming in the Interactive Shell Chapter 3: Strings and Writing Programs Chapter 4: The Reverse Cipher Chapter 5: The Caesar Cipher Chapter 6: Hacking the Caesar Cipher with Brute-Force Chapter 7: Encrypting with the Transposition Cipher Chapter 8: Decrypting with the Transposition Cipher Chapter 9: Programming a Program to Test Your Program Chapter 10: Encrypting and Decrypting Files Chapter 11: Detecting English Programmatically Chapter 12: Hacking the Transposition Cipher Chapter 13: A Modular Arithmetic Module for the Affine Cipher Chapter 14: Programming the Affine Cipher Chapter 15: Hacking the Affine Cipher Chapter 16: Programming the Simple Substitution Cipher Chapter 17: Hacking the Simple Substitution Cipher Chapter 18: Programming the Vigenère Cipher Chapter 19: Frequency Analysis Chapter 20: Hacking the Vigenère Cipher Chapter 21: The One-Time Pad Cipher Chapter 22: Finding and Generating Prime Numbers Chapter 23: Generating Keys for the Public Key Cipher Chapter 24: Programming the Public Key Cipher Appendix: Debugging Python Code Index CONTENTS IN DETAIL ACKNOWLEDGMENTS INTRODUCTION Who Should Read This Book? What’s in This Book? How to Use This Book Typing Source Code Checking for Typos Coding Conventions in This Book Online Resources Downloading and Installing Python Windows Instructions macOS Instructions Ubuntu Instructions Downloading pyperclip.py Starting IDLE Summary MAKING PAPER CRYPTOGRAPHY TOOLS What Is Cryptography? Codes vs Ciphers The Caesar Cipher The Cipher Wheel Encrypting with the Cipher Wheel Decrypting with the Cipher Wheel Encrypting and Decrypting with Arithmetic Why Double Encryption Doesn’t Work Summary Practice Questions PROGRAMMING IN THE INTERACTIVE SHELL Some Simple Math Expressions Integers and Floating-Point Values Expressions Order of Operations Evaluating Expressions Storing Values with Variables simple substitution cipher, 222–225, 241–245 transposition cipher, 166–168 Vigenère cipher, 280–282 hash mark (#), 34 hello.py, 31 hybrid cryptosystems, 347 I IDLE, xxvii, 12, 30 debugging with, 375–379 opening programs, 34 running programs, 33 saving programs, 32 if statement, 59–60 ImportError, 55 import statement, 55, 56 IndexError, 25 indexing, 24–27 infinite loop, 195 in operator, 61–62 in place modification, of lists, 122 input() function, 35, 50 insert() list method, 367 installing Python, xxv–xxvi integer division operator (//), 181 integer (int) data type, 13 interactive shell, xxvii, 12 intersected mapping, 225, 234–235 int() function, 154 islower() string method, 216–217 isupper() string method, 216–217 itertools module, 307–308 J join() string method, K 93–94, 253–254 Kasiski examination, 282–284 Kasiski, Friedrich, 282 Kerckhoffs, Auguste, 70 Kerckhoffs’s principle, 70 key, encryption, key keyword argument, for sort(), 273 L len() function, 41–42 using with dictionaries, 148 using with lists, 89 less than operator (

Ngày đăng: 06/03/2019, 10:35

Từ khóa liên quan

Mục lục

  • Title Page

  • Copyright Page

  • Dedication

  • About the Author

  • About the Technical Reviewers

  • Brief Contents

  • Contents in Detail

  • Acknowledgments

  • Introduction

    • Who Should Read This Book?

    • What’s in This Book?

    • How to Use This Book

    • Downloading and Installing Python

    • Downloading pyperclip.py

    • Starting IDLE

    • Summary

  • Chapter 1: Making Paper Cryptography Tools

    • What Is Cryptography?

    • Codes vs. Ciphers

    • The Caesar Cipher

    • Why Double Encryption Doesn’t Work

    • Summary

    • Practice Questions

  • Chapter 2: Programming in the Interactive Shell

    • Some Simple Math Expressions

    • Storing Values with Variables

    • Summary

    • Practice Questions

  • 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

    • 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

    • Introducing the while Loop

    • Improving the Program with an input⠀) Prompt

    • Summary

    • Practice Questions

  • 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

    • Source Code for the Caesar Cipher Hacker Program

    • Sample Run of the Caesar Cipher Hacker Program

    • Setting Up Variables

    • Looping with the range⠀) Function

    • Decrypting the Message

    • Using String Formatting to Display the Key and Decrypted Messages

    • Summary

    • Practice Question

  • 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

    • Source Code of the Transposition Cipher Hacker Program

    • Sample Run of the Transposition Cipher Hacker Program

    • Importing the Modules

    • Multiline Strings with Triple Quotes

    • Displaying the Results of Hacking the Message

    • Getting the Hacked Message

    • Calling the main⠀) Function

    • Summary

    • Practice Questions

  • Chapter 13: A Modular Arithmetic Module for the Affine Cipher

    • Modular Arithmetic

    • The Modulo Operator

    • Finding Factors to Calculate the Greatest Common Divisor

    • Multiple Assignment

    • Euclid’s Algorithm for Finding the GCD

    • Understanding How the Multiplicative and Affine Ciphers Work

    • Source Code for the Cryptomath Module

    • Summary

    • Practice Questions

  • 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

    • 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

    • Calling the main⠀) Function

    • Summary

    • Practice Questions

  • 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

    • The Unbreakable One-Time Pad Cipher

    • Summary

    • Practice Questions

  • 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

    • How the Debugger Works

    • Debugging the Reverse Cipher Program

    • Setting Breakpoints

    • Summary

  • Index

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan