Table of Contents for
Cracking Codes with Python

Version ebook / Retour

Cover image for bash Cookbook, 2nd Edition Cracking Codes with Python by Al Sweigart Published by No Starch Press, 2018
  1. Cover Page
  2. Cracking Codes with Python
  3. Title Page
  4. Copyright Page
  5. Dedication
  6. About the Author
  7. About the Technical Reviewers
  8. Brief Contents
  9. Contents in Detail
  10. Acknowledgments
  11. Introduction
  12. Chapter 1: Making Paper Cryptography Tools
  13. Chapter 2: Programming in the Interactive Shell
  14. Chapter 3: Strings and Writing Programs
  15. Chapter 4: The Reverse Cipher
  16. Chapter 5: The Caesar Cipher
  17. Chapter 6: Hacking the Caesar Cipher with Brute-Force
  18. Chapter 7: Encrypting with the Transposition Cipher
  19. Chapter 8: Decrypting with the Transposition Cipher
  20. Chapter 9: Programming a Program to Test Your Program
  21. Chapter 10: Encrypting and Decrypting Files
  22. Chapter 11: Detecting English Programmatically
  23. Chapter 12: Hacking the Transposition Cipher
  24. Chapter 13: A Modular Arithmetic Module for the Affine Cipher
  25. Chapter 14: Programming the Affine Cipher
  26. Chapter 15: Hacking the Affine Cipher
  27. Chapter 16: Programming the Simple Substitution Cipher
  28. Chapter 17: Hacking the Simple Substitution Cipher
  29. Chapter 18: Programming the Vigenère Cipher
  30. Chapter 19: Frequency Analysis
  31. Chapter 20: Hacking the Vigenère Cipher
  32. Chapter 21: The One-Time Pad Cipher
  33. Chapter 22: Finding and Generating Prime Numbers
  34. Chapter 23: Generating Keys for the Public Key Cipher
  35. Chapter 24: Programming the Public Key Cipher
  36. Appendix Debugging Python Code
  37. Index
  38. Resources
  39. Cracking Codes with Python

Contents in Detail

  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
  1. i
  2. ii
  3. iii
  4. iv
  5. v
  6. vi
  7. vii
  8. viii
  9. ix
  10. x
  11. xi
  12. xii
  13. xiii
  14. xiv
  15. xv
  16. xvi
  17. xvii
  18. xviii
  19. xix
  20. xx
  21. xxi
  22. xxii
  23. xxiii
  24. xxiv
  25. xxv
  26. xxvi
  27. xxvii
  28. xxviii
  29. 1
  30. 2
  31. 3
  32. 4
  33. 5
  34. 6
  35. 7
  36. 8
  37. 9
  38. 10
  39. 11
  40. 12
  41. 13
  42. 14
  43. 15
  44. 16
  45. 17
  46. 18
  47. 19
  48. 20
  49. 21
  50. 22
  51. 23
  52. 24
  53. 25
  54. 26
  55. 27
  56. 28
  57. 29
  58. 30
  59. 31
  60. 32
  61. 33
  62. 34
  63. 35
  64. 36
  65. 37
  66. 38
  67. 39
  68. 40
  69. 41
  70. 42
  71. 43
  72. 44
  73. 45
  74. 46
  75. 47
  76. 48
  77. 49
  78. 50
  79. 51
  80. 52
  81. 53
  82. 54
  83. 55
  84. 56
  85. 57
  86. 58
  87. 59
  88. 60
  89. 61
  90. 62
  91. 63
  92. 64
  93. 65
  94. 66
  95. 67
  96. 68
  97. 69
  98. 70
  99. 71
  100. 72
  101. 73
  102. 74
  103. 75
  104. 76
  105. 77
  106. 78
  107. 79
  108. 80
  109. 81
  110. 82
  111. 83
  112. 84
  113. 85
  114. 86
  115. 87
  116. 88
  117. 89
  118. 90
  119. 91
  120. 92
  121. 93
  122. 94
  123. 95
  124. 96
  125. 97
  126. 98
  127. 99
  128. 100
  129. 101
  130. 102
  131. 103
  132. 104
  133. 105
  134. 106
  135. 107
  136. 108
  137. 109
  138. 110
  139. 111
  140. 112
  141. 113
  142. 114
  143. 115
  144. 116
  145. 117
  146. 118
  147. 119
  148. 120
  149. 121
  150. 122
  151. 123
  152. 124
  153. 125
  154. 126
  155. 127
  156. 128
  157. 129
  158. 130
  159. 131
  160. 132
  161. 133
  162. 134
  163. 135
  164. 136
  165. 137
  166. 138
  167. 139
  168. 140
  169. 141
  170. 142
  171. 143
  172. 144
  173. 145
  174. 146
  175. 147
  176. 148
  177. 149
  178. 150
  179. 151
  180. 152
  181. 153
  182. 154
  183. 155
  184. 156
  185. 157
  186. 158
  187. 159
  188. 160
  189. 161
  190. 162
  191. 163
  192. 164
  193. 165
  194. 166
  195. 167
  196. 168
  197. 169
  198. 170
  199. 171
  200. 172
  201. 173
  202. 174
  203. 175
  204. 176
  205. 177
  206. 178
  207. 179
  208. 180
  209. 181
  210. 182
  211. 183
  212. 184
  213. 185
  214. 186
  215. 187
  216. 188
  217. 189
  218. 190
  219. 191
  220. 192
  221. 193
  222. 194
  223. 195
  224. 196
  225. 197
  226. 198
  227. 199
  228. 200
  229. 201
  230. 202
  231. 203
  232. 204
  233. 205
  234. 206
  235. 207
  236. 208
  237. 209
  238. 210
  239. 211
  240. 212
  241. 213
  242. 214
  243. 215
  244. 216
  245. 217
  246. 218
  247. 219
  248. 220
  249. 221
  250. 222
  251. 223
  252. 224
  253. 225
  254. 226
  255. 227
  256. 228
  257. 229
  258. 230
  259. 231
  260. 232
  261. 233
  262. 234
  263. 235
  264. 236
  265. 237
  266. 238
  267. 239
  268. 240
  269. 241
  270. 242
  271. 243
  272. 244
  273. 245
  274. 246
  275. 247
  276. 248
  277. 249
  278. 250
  279. 251
  280. 252
  281. 253
  282. 254
  283. 255
  284. 256
  285. 257
  286. 258
  287. 259
  288. 260
  289. 261
  290. 262
  291. 263
  292. 264
  293. 265
  294. 266
  295. 267
  296. 268
  297. 269
  298. 270
  299. 271
  300. 272
  301. 273
  302. 274
  303. 275
  304. 276
  305. 277
  306. 278
  307. 279
  308. 280
  309. 281
  310. 282
  311. 283
  312. 284
  313. 285
  314. 286
  315. 287
  316. 288
  317. 289
  318. 290
  319. 291
  320. 292
  321. 293
  322. 294
  323. 295
  324. 296
  325. 297
  326. 298
  327. 299
  328. 300
  329. 301
  330. 302
  331. 303
  332. 304
  333. 305
  334. 306
  335. 307
  336. 308
  337. 309
  338. 310
  339. 311
  340. 312
  341. 313
  342. 314
  343. 315
  344. 316
  345. 317
  346. 318
  347. 319
  348. 320
  349. 321
  350. 322
  351. 323
  352. 324
  353. 325
  354. 326
  355. 327
  356. 328
  357. 329
  358. 330
  359. 331
  360. 332
  361. 333
  362. 334
  363. 335
  364. 336
  365. 337
  366. 338
  367. 339
  368. 340
  369. 341
  370. 342
  371. 343
  372. 344
  373. 345
  374. 346
  375. 347
  376. 348
  377. 349
  378. 350
  379. 351
  380. 352
  381. 353
  382. 354
  383. 355
  384. 356
  385. 357
  386. 358
  387. 359
  388. 360
  389. 361
  390. 362
  391. 363
  392. 364
  393. 365
  394. 366
  395. 367
  396. 368
  397. 369
  398. 370
  399. 371
  400. 372
  401. 373
  402. 374
  403. 375
  404. 376
  405. 377
  406. 378
  407. 379
  408. 380
  409. 381
  410. 382
  411. 383
  412. 384
  413. 385
  414. 386
  415. 387
Back to top