Cracking Codes with Python. An Introduction to building and breaking Ciphers
Book information
Description
Brief Contents......Page 3 Contents......Page 5 Introduction......Page 15 Who Should Read This Book?......Page 16 What’s in This Book?......Page 17 Typing Source Code......Page 18 Downloading and Installing Python......Page 19 Downloading pyperclip.py......Page 20 Starting IDLE......Page 21 Summary......Page 22 1 Paper Cryptography Tools......Page 23 What Is Cryptography?......Page 24 Codes vs. Ciphers......Page 25 The Cipher Wheel......Page 26 Encrypting with the Cipher Wheel......Page 27 Decrypting with the Cipher Wheel......Page 28 Encrypting and Decrypting with Arithmetic......Page 29 Summary......Page 30 Practice Questions ......Page 31 2 Programming in the Interactive Shell......Page 32 Some Simple Math Expressions......Page 33 Expressions......Page 34 Evaluating Expressions......Page 35 Storing Values with Variables......Page 36 Overwriting Variables......Page 38 Summary......Page 39 Practice Questions ......Page 40 3 Strings & writing Programs......Page 41 Working with Text Using String Values......Page 42 String Concatenation with the + Operator......Page 43 Getting Characters from Strings Using Indexes......Page 44 Printing Values with the print() Function......Page 47 Printing Escape Characters......Page 48 Quotes and Double Quotes......Page 49 Writing Programs in IDLE’s File Editor......Page 50 Checking Your Source Code with the Online Diff Tool......Page 51 Saving Your Program......Page 52 Running Your Program......Page 53 Printing Directions to the User......Page 54 Ending the Program......Page 55 Summary......Page 56 Practice Questions ......Page 57 4 Reverse Cipher......Page 58 Sample Run of the Reverse Cipher Program......Page 59 Finding the Length of a String......Page 60 Introducing the while Loop......Page 61 Comparison Operators......Page 62 Blocks......Page 64 The while Loop Statement......Page 65 “Growing” a String......Page 66 Summary......Page 69 Practice Questions ......Page 70 5 Caesar Cipher......Page 71 Source Code for the Caesar Cipher Program......Page 72 Sample Run of the Caesar Cipher Program......Page 73 Importing Modules and Setting Up Variables......Page 74 Constants and Variables......Page 75 An Example for Loop......Page 76 The if Statement......Page 77 The else Statement......Page 78 The in and not in Operators......Page 79 The find() String Method......Page 80 Encrypting and Decrypting Symbols......Page 81 Handling Wraparound......Page 82 Displaying and Copying the Translated String......Page 83 Summary......Page 84 Practice Questions......Page 85 6 Hacking Caesar Cipher with Brute-Force......Page 87 Source Code for the Caesar Cipher Hacker Program......Page 88 Sample Run of the Caesar Cipher Hacker Program......Page 89 Looping with the range() Function......Page 90 Decrypting the Message......Page 91 Using String Formatting to Display the Key and Decrypted Messages......Page 93 Practice Question ......Page 94 7 Encrypting with Transposition Cipher......Page 95 How the Transposition Cipher Works......Page 96 Encrypting a Message by Hand......Page 97 Creating the Encryption Program......Page 98 Source Code for the Transposition Cipher Encryption Program......Page 99 Creating Your Own Functions with def Statements......Page 100 Defining a Function that Takes Arguments with Parameters......Page 101 Changes to Parameters Only Exist Inside the Function......Page 102 Defining the main() Function......Page 103 The List Data Type......Page 104 Reassigning the Items in Lists......Page 105 Lists of Lists......Page 106 List Concatenation and Replication with the + and * Operators......Page 107 The Transposition Encryption Algorithm......Page 108 Augmented Assignment Operators......Page 109 Moving currentIndex Through the Message......Page 110 The join() String Method......Page 111 A return Statement Example......Page 112 The __name__ Variable......Page 113 Summary......Page 114 Practice Questions ......Page 115 8 Decrypting with Transposition Cipher......Page 116 How to Decrypt with the Transposition Cipher on Paper......Page 117 Source Code for the Transposition Cipher Decryption Program......Page 118 Importing Modules and Setting Up the main() Function......Page 119 The round(), math.ceil(), and math.floor() Functions......Page 120 The decryptMessage() Function......Page 121 Boolean Operators......Page 123 Adjusting the column and row Variables......Page 126 Summary......Page 127 Practice Questions ......Page 128 9 Program to test your Program......Page 129 Source Code for the Transposition Cipher Tester Program......Page 130 Sample Run of the Transposition Cipher Tester Program......Page 131 Creating Pseudorandom Numbers......Page 132 Duplicating a String a Random Number of Times......Page 134 List Variables Use References......Page 135 Passing References......Page 137 The random.shuffle() Function......Page 138 Testing Each Message......Page 139 Calling the main() Function......Page 140 Summary......Page 141 Practice Questions ......Page 142 10 Encrypting & Decrypting Files......Page 143 Source Code for the Transposition File Cipher Program......Page 144 Working with Files......Page 146 Writing to and Closing Files......Page 147 Setting Up the main() Function......Page 148 The os.path.exists() Function......Page 149 The upper(), lower(), and title() String Methods......Page 150 Using These String Methods in the Program......Page 151 The time Module and time.time() Function......Page 152 Writing the Output File......Page 153 Summary......Page 154 Practice Questions ......Page 155 11 Detecting English Programmatically......Page 156 How Can a Computer Understand English?......Page 157 Source Code for the Detect English Module......Page 158 Instructions and Setting Up Constants......Page 160 The Dictionary Data Type......Page 161 Adding or Changing Items in a Dictionary......Page 162 Using the in Operator with Dictionaries......Page 163 Using for Loops with Dictionaries......Page 164 The split() Method......Page 165 Returning the Dictionary Data......Page 166 Divide-by-Zero Errors......Page 167 Counting the English Word Matches......Page 168 Finding the Ratio of English Words in the Message......Page 169 The append() List Method......Page 170 Detecting English Words......Page 171 Calculating Percentages......Page 172 Summary......Page 174 Practice Questions ......Page 175 12 Hacking the Transposition Cipher......Page 176 Source Code of the Transposition Cipher Hacker Program......Page 177 Sample Run of the Transposition Cipher Hacker Program......Page 178 Multiline Strings with Triple Quotes......Page 179 Displaying the Results of Hacking the Message......Page 180 Getting the Hacked Message......Page 181 The strip() String Method......Page 182 Failing to Hack the Message......Page 183 Practice Questions ......Page 184 13 Modular Arithmetic Module for the Affine Cipher......Page 185 Modular Arithmetic......Page 186 Finding Factors to Calculate the Greatest Common Divisor......Page 187 Multiple Assignment......Page 189 Euclid’s Algorithm for Finding the GCD......Page 190 Understanding How the Multiplicative and Affine Ciphers Work......Page 191 Choosing Valid Multiplicative Keys......Page 192 Decrypting with the Affine Cipher......Page 193 The Integer Division Operator......Page 195 Source Code for the Cryptomath Module......Page 196 Practice Questions ......Page 197 14 Programming the Affine Cipher......Page 198 Source Code for the Affine Cipher Program......Page 199 Setting Up Modules, Constants, and the main() Function......Page 201 Calculating and Validating the Keys......Page 202 Checking for Weak Keys......Page 203 How Many Keys Can the Affine Cipher Have?......Page 204 Writing the Encryption Function......Page 206 Writing the Decryption Function......Page 207 Generating Random Keys......Page 208 Practice Questions ......Page 209 15 Hacking the Affine Cipher......Page 210 Source Code for the Affine Cipher Hacker Program......Page 211 Sample Run of the Affine Cipher Hacker Program......Page 212 Setting Up Modules, Constants, and the main() Function......Page 213 Calculating the Total Number of Possible Keys......Page 214 The continue Statement......Page 215 Using continue to Skip Code......Page 216 Calling the main() Function......Page 217 Practice Questions ......Page 218 16 Programming the Simple Substitution Cipher......Page 219 How the Simple Substitution Cipher Works......Page 220 Source Code for the Simple Substitution Cipher Program......Page 221 Sample Run of the Simple Substitution Cipher Program......Page 222 Setting Up Modules, Constants, and the main() Function......Page 223 The sort() List Method......Page 224 Wrapper Functions......Page 225 The translateMessage() Function......Page 227 The isupper() and islower() String Methods......Page 228 Preserving Cases with isupper()......Page 229 Generating a Random Key......Page 230 Practice Questions ......Page 231 17 Hacking the Simple Substitution Cipher......Page 232 Finding Word Patterns......Page 233 Finding Potential Decryption Letters......Page 234 The Word Pattern Modules......Page 236 Source Code for the Simple Substitution Hacking Program......Page 237 Sample Run of the Simple Substitution Hacking Program......Page 240 Finding Characters with Regular Expressions......Page 241 Setting Up the main() Function......Page 242 Creating a Blank Mapping......Page 243 Adding Letters to a Mapping......Page 244 Intersecting Two Mappings......Page 245 How the Letter-Mapping Helper Functions Work......Page 246 Identifying Solved Letters in Mappings......Page 249 Testing the removeSolvedLetterFromMapping() Function......Page 251 The hackSimpleSub() Function......Page 252 Decrypting the Message......Page 254 Decrypting in the Interactive Shell......Page 255 Calling the main() Function......Page 256 Practice Questions ......Page 257 18 Programming the Vigenère Cipher......Page 258 Using Multiple Letter Keys in the Vigenère Cipher......Page 259 Longer Vigenère Keys Are More Secure......Page 260 Choosing a Key That Prevents Dictionary Attacks......Page 261 Source Code for the Vigenère Cipher Program......Page 262 Setting Up Modules, Constants, and the main() Function......Page 263 Building Strings with the List-Append-Join Process......Page 264 Encrypting and Decrypting the Message......Page 266 Summary......Page 268 Practice Questions ......Page 269 19 Frequency Analysis......Page 270 Analyzing the Frequency of Letters in Text......Page 271 Calculating the Frequency Match Score for the Simple Substitution Cipher......Page 273 Calculating the Frequency Match Score for the Transposition Cipher......Page 274 Using Frequency Analysis on the Vigenère Cipher......Page 275 Source Code for Matching Letter Frequencies......Page 276 Storing the Letters in ETAOIN Order......Page 277 Counting the Letters in a Message......Page 278 Ordering the Letters in the Message by Frequency......Page 279 Creating a Dictionary of Frequency Counts and Letter Lists......Page 280 Sorting the Letter Lists in Reverse ETAOIN Order......Page 281 Sorting the Dictionary Lists by Frequency......Page 285 Calculating the Frequency Match Score of the Message......Page 287 Summary......Page 288 Practice Questions ......Page 289 20 Hacking the Vigenère Cipher......Page 290 Source Code for the Vigenère Dictionary Hacker Program......Page 291 About the Vigenère Dictionary Hacker Program......Page 292 Finding Repeated Sequences......Page 293 Getting Factors of Spacings......Page 294 Getting Every Nth Letters from a String......Page 295 Using Frequency Analysis to Break Each Subkey......Page 296 Source Code for the Vigenère Hacking Program......Page 298 Sample Run of the Vigenère Hacking Program......Page 304 Finding Repeated Sequences......Page 305 Calculating the Factors of the Spacings......Page 308 Finding the Most Common Factors......Page 309 Finding the Most Likely Key Lengths......Page 311 Extending the repeatedSeqSpacings Dictionary......Page 312 Getting Letters Encrypted with the Same Subkey......Page 313 Attempting Decryption with a Likely Key Length......Page 314 Finding Possible Combinations of Subkeys......Page 317 Printing the Decrypted Text with the Correct Casing......Page 321 Breaking Out of the Loop When a Potential Key Is Found......Page 322 Brute-Forcing All Other Key Lengths......Page 323 Modifying the Constants of the Hacking Program......Page 324 Practice Questions ......Page 325 21 One-Time Pad Cipher......Page 326 Making Key Length Equal Message Length......Page 327 Making the Key Truly Random......Page 329 Why the Two-Time Pad Is the Vigenère Cipher......Page 330 Practice Questions ......Page 331 22 Finding & Generating Prime Numbers......Page 332 What Is a Prime Number?......Page 333 Source Code for the Prime Numbers Module......Page 335 How the Trial Division Algorithm Works......Page 337 The Sieve of Eratosthenes......Page 339 Generating Prime Numbers with the Sieve of Eratosthenes......Page 341 The Rabin-Miller Primality Algorithm......Page 342 Finding Large Prime Numbers......Page 343 Generating Large Prime Numbers......Page 344 Practice Questions ......Page 345 23 Generating Keys for Public Key Cipher......Page 346 Public Key Cryptography......Page 347 The Problem with Authentication......Page 348 Digital Signatures......Page 349 Beware the MITM Attack......Page 350 Source Code for the Public Key Generation Program......Page 351 Sample Run of the Public Key Generation Program......Page 353 Generating Keys with the generateKey() Function......Page 354 Calculating a d Value......Page 355 Creating Key Files with the makeKeyFiles() Function......Page 356 Hybrid Cryptosystems......Page 358 Practice Questions ......Page 359 24 Programming the Public Key Cipher......Page 360 Converting a String into a Block......Page 361 The Mathematics of Public Key Cipher Encryption and Decryption......Page 364 Converting a Block to a String......Page 365 Why We Can’t Hack the Public Key Cipher......Page 366 Source Code for the Public Key Cipher Program......Page 368 Sample Run of the Public Key Cipher Program......Page 371 How the Program Determines Whether to Encrypt or Decrypt......Page 373 Converting Strings to Blocks with getBlocksFromText()......Page 374 Storing Blocks in blockInt......Page 375 Using getTextFromBlocks() to Decrypt......Page 377 Writing the encryptMessage() Function......Page 378 Writing the decryptMessage() Function......Page 379 Writing the Encryption to a File......Page 380 Decrypting from a File......Page 382 Summary......Page 384 How the Debugger Works......Page 386 Debugging the Reverse Cipher Program......Page 388 Setting Breakpoints......Page 390 Summary......Page 391 Index ......Page 392
Similar books
The Big Book of Small Python Projects
2021 · AZW
Beyond the Basic Stuff with Python
2020 · AZW
Automate the Boring Stuff with Python, 3rd Edition (Early Access)
2024 · PDF
Automatyzacja nudnych zadań z Pythonem. Nauka programowania. Wydanie I
2017 · EPUB
Automatyzacja nudnych zadań z Pythonem. Nauka programowania
2017 · PDF
Invent Your Own Computer Games with Python 4th Edition
2017 · PDF
The Big Book of Small Python Projects: 81 Easy Practice Programs
2021 · PDF