TABLE OF CONTENTS
About This Book ....................................................................................................................... 6
Table of Contents ..................................................................................................................... 7
Chapter 1 - Making Paper Cryptography Tools ...................................................................... 1
What is Cryptography? ................................................................................................................................................ 2
Codes vs. Ciphers ........................................................................................................................................................ 3
Making a Paper Cipher Wheel ..................................................................................................................................... 4
A Virtual Cipher Wheel ............................................................................................................................................... 7
How to Encrypt with the Cipher Wheel ....................................................................................................................... 8
How to Decrypt with the Cipher Wheel ....................................................................................................................... 9
A Different Cipher Tool: The St. Cyr Slide ............................................................................................................... 10
Practice Exercises, Chapter 1, Set A .......................................................................................................................... 11
Doing Cryptography without Paper Tools ................................................................................................................. 11
Practice Exercises, Chapter 1, Set B .......................................................................................................................... 13
Double-Strength Encryption?..................................................................................................................................... 13
Programming a Computer to do Encryption .............................................................................................................. 14
Chapter 2 - Installing Python ................................................................................................. 16
Downloading and Installing Python ........................................................................................................................... 17
Downloading pyperclip.py ......................................................................................................................................... 18
Starting IDLE ............................................................................................................................................................. 18
The Featured Programs .............................................................................................................................................. 19
Line Numbers and Spaces .......................................................................................................................................... 20
Text Wrapping in This Book ..................................................................................................................................... 20
Tracing the Program Online ....................................................................................................................................... 21
Checking Your Typed Code with the Online Diff Tool ............................................................................................. 21
Copying and Pasting Text .......................................................................................................................................... 21
More Info Links ......................................................................................................................................................... 22
Programming and Cryptography ................................................................................................................................ 22
Chapter 3 - The Interactive Shell .......................................................................................... 26
Some Simple Math Stuff ............................................................................................................................................ 26
Integers and Floating Point Values ............................................................................................................................ 27
Expressions ................................................................................................................................................................ 27
Order of Operations ................................................................................................................................................... 28
Evaluating Expressions .............................................................................................................................................. 29
Errors are Okay! ......................................................................................................................................................... 29
Practice Exercises, Chapter 3, Set A .......................................................................................................................... 30
Every Value has a Data Type ..................................................................................................................................... 30
Storing Values in Variables with Assignment Statements ......................................................................................... 30
Overwriting Variables ................................................................................................................................................ 32
Using More Than One Variable ................................................................................................................................. 33
Variable Names .......................................................................................................................................................... 34
Practice Exercises, Chapter 3, Set B .......................................................................................................................... 35
Summary - But When Are We Going to Start Hacking?............................................................................................ 35
Chapter 4 - Strings and Writing Programs ........................................................................... 36
Strings ........................................................................................................................................................................ 36
String Concatenation with the + Operator ................................................................................................................. 38
String Replication with the * Operator ...................................................................................................................... 39
Printing Values with the print() Function ............................................................................................................ 39
Escape Characters ...................................................................................................................................................... 40
Quotes and Double Quotes ........................................................................................................................................ 41
Practice Exercises, Chapter 4, Set A .......................................................................................................................... 42
Indexing ..................................................................................................................................................................... 42
Negative Indexes ........................................................................................................................................................ 44
Slicing ........................................................................................................................................................................ 44
Blank Slice Indexes.................................................................................................................................................... 45
Practice Exercises, Chapter 4, Set B .......................................................................................................................... 46
Writing Programs in IDLE’s File Editor .................................................................................................................... 46
Hello World! .............................................................................................................................................................. 47
Source Code of Hello World ...................................................................................................................................... 47
Saving Your Program ................................................................................................................................................ 48
Running Your Program .............................................................................................................................................. 49
Opening The Programs You’ve Saved ....................................................................................................................... 50
How the “Hello World” Program Works ................................................................................................................... 50
Comments .................................................................................................................................................................. 50
Functions.................................................................................................................................................................... 51
The print() function ............................................................................................................................................. 51
The input() function ............................................................................................................................................. 51
Ending the Program ................................................................................................................................................... 52
Practice Exercises, Chapter 4, Set C .......................................................................................................................... 52
Summary .................................................................................................................................................................... 53
Chapter 5 - The Reverse Cipher ............................................................................................ 54
The Reverse Cipher.................................................................................................................................................... 54
Source Code of the Reverse Cipher Program ............................................................................................................. 55
Sample Run of the Reverse Cipher Program .............................................................................................................. 55
Checking Your Source Code with the Online Diff Tool ............................................................................................ 56
How the Program Works............................................................................................................................................ 56
The len() Function ................................................................................................................................................. 57
Introducing the while Loop ..................................................................................................................................... 58
The Boolean Data Type ............................................................................................................................................. 59
Comparison Operators ............................................................................................................................................... 59
Conditions .................................................................................................................................................................. 62
Blocks ........................................................................................................................................................................ 62
The while Loop Statement ...................................................................................................................................... 63
“Growing” a String .................................................................................................................................................... 64
Tracing Through the Program, Step by Step .............................................................................................................. 67
Using input() In Our Programs ............................................................................................................................. 68
Practice Exercises, Chapter 5, Section A ................................................................................................................... 69
Summary .................................................................................................................................................................... 69
Chapter 6 - The Caesar Cipher .............................................................................................. 70
Implementing a Program ............................................................................................................................................ 70
Source Code of the Caesar Cipher Program ............................................................................................................... 71
Sample Run of the Caesar Cipher Program ................................................................................................................ 72
Checking Your Source Code with the Online Diff Tool ............................................................................................ 73
Practice Exercises, Chapter 6, Set A .......................................................................................................................... 73
How the Program Works............................................................................................................................................ 73
Importing Modules with the import Statement ....................................................................................................... 73
Constants.................................................................................................................................................................... 74
The upper() and lower() String Methods ........................................................................................................ 75
The for Loop Statement ........................................................................................................................................... 76
A while Loop Equivalent of a for Loop ................................................................................................................ 77
Practice Exercises, Chapter 6, Set B .......................................................................................................................... 78
The if Statement ...................................................................................................................................................... 78
The else Statement .................................................................................................................................................. 79
The elif Statement .................................................................................................................................................. 79
The in and not in Operators ................................................................................................................................. 80
The find() String Method ...................................................................................................................................... 81
Practice Exercises, Chapter 6, Set C .......................................................................................................................... 82
Back to the Code ........................................................................................................................................................ 82
Displaying and Copying the Encrypted/Decrypted String ......................................................................................... 85
Encrypt Non-Letter Characters .................................................................................................................................. 86
Summary .................................................................................................................................................................... 87
Chapter 7 - Hacking the Caesar Cipher with the Brute-Force Technique .......................... 88
Hacking Ciphers ........................................................................................................................................................ 88
The Brute-Force Attack ............................................................................................................................................. 89
Source Code of the Caesar Cipher Hacker Program .................................................................................................. 89
Sample Run of the Caesar Cipher Hacker Program ................................................................................................... 90
How the Program Works............................................................................................................................................ 91
The range() Function ............................................................................................................................................ 91
Back to the Code ........................................................................................................................................................ 93
String Formatting ....................................................................................................................................................... 94
Practice Exercises, Chapter 7, Set A .......................................................................................................................... 95
Summary .................................................................................................................................................................... 95
Chapter 8 - Encrypting with the Transposition Cipher ........................................................ 96
Encrypting with the Transposition Cipher ................................................................................................................. 96
Practice Exercises, Chapter 8, Set A .......................................................................................................................... 98
A Transposition Cipher Encryption Program ............................................................................................................. 98
Source Code of the Transposition Cipher Encryption Program ................................................................................. 98
Sample Run of the Transposition Cipher Encryption Program .................................................................................. 99
How the Program Works.......................................................................................................................................... 100
Creating Your Own Functions with def Statements ............................................................................................... 100
The Program’s main() Function ........................................................................................................................... 101
Parameters ................................................................................................................................................................ 102
Variables in the Global and Local Scope ................................................................................................................. 104
The global Statement ........................................................................................................................................... 104
Practice Exercises, Chapter 8, Set B ........................................................................................................................ 106
The List Data Type .................................................................................................................................................. 106
Using the list() Function to Convert Range Objects to Lists ............................................................................. 109
Reassigning the Items in Lists.................................................................................................................................. 110
Reassigning Characters in Strings ............................................................................................................................ 110
Lists of Lists ............................................................................................................................................................ 110
Practice Exercises, Chapter 8, Set C ........................................................................................................................ 111
Using len() and the in Operator with Lists ......................................................................................................... 111
List Concatenation and Replication with the + and * Operators.............................................................................. 112
Practice Exercises, Chapter 8, Set D ........................................................................................................................ 113
The Transposition Encryption Algorithm ................................................................................................................ 113
Augmented Assignment Operators .......................................................................................................................... 115
Back to the Code ...................................................................................................................................................... 116
The join() String Method .................................................................................................................................... 118
Return Values and return Statements .................................................................................................................. 119
Practice Exercises, Chapter 8, Set E ........................................................................................................................ 120
Back to the Code ...................................................................................................................................................... 120
The Special __name__ Variable ............................................................................................................................ 120
Key Size and Message Length ................................................................................................................................. 121
Summary .................................................................................................................................................................. 122
Chapter 9 - Decrypting with the Transposition Cipher ...................................................... 123
Decrypting with the Transposition Cipher on Paper ................................................................................................ 124
Practice Exercises, Chapter 9, Set A ........................................................................................................................ 125
A Transposition Cipher Decryption Program........................................................................................................... 126
Source Code of the Transposition Cipher Decryption Program ............................................................................... 126
How the Program Works.......................................................................................................................................... 127
The math.ceil(), math.floor() and round() Functions ......................................................................... 128
The and and or Boolean Operators ........................................................................................................................ 132
Practice Exercises, Chapter 9, Set B ........................................................................................................................ 133
Truth Tables ............................................................................................................................................................. 133
The and and or Operators are Shortcuts ................................................................................................................ 134
Order of Operations for Boolean Operators ............................................................................................................. 135
Back to the Code ...................................................................................................................................................... 135
Practice Exercises, Chapter 9, Set C ........................................................................................................................ 137
Summary .................................................................................................................................................................. 137
Chapter 10 - Programming a Program to Test Our Program ............................................ 138
Source Code of the Transposition Cipher Tester Program ....................................................................................... 139
Sample Run of the Transposition Cipher Tester Program ........................................................................................ 140
How the Program Works.......................................................................................................................................... 141
Pseudorandom Numbers and the random.seed() Function ............................................................................... 141
The random.randint() Function ..................................................................................................................... 143
References ................................................................................................................................................................ 143
The copy.deepcopy() Functions ...................................................................................................................... 147
Practice Exercises, Chapter 10, Set A ...................................................................................................................... 148
The random.shuffle() Function ..................................................................................................................... 148
Randomly Scrambling a String ................................................................................................................................ 149
Back to the Code ...................................................................................................................................................... 149
The sys.exit() Function ................................................................................................................................... 150
Testing Our Test Program ........................................................................................................................................ 151
Summary .................................................................................................................................................................. 152
Chapter 11 - Encrypting and Decrypting Files ................................................................... 153
Plain Text Files ........................................................................................................................................................ 154
Source Code of the Transposition File Cipher Program ........................................................................................... 154
Sample Run of the Transposition File Cipher Program ........................................................................................... 157
Reading From Files .................................................................................................................................................. 157
Writing To Files ....................................................................................................................................................... 158
How the Program Works.......................................................................................................................................... 159
The os.path.exists() Function ..................................................................................................................... 160
The startswith() and endswith() String Methods ..................................................................................... 161
The title() String Method ................................................................................................................................. 162
The time Module and time.time() Function .................................................................................................. 163
Back to the Code ...................................................................................................................................................... 164
Practice Exercises, Chapter 11, Set A ...................................................................................................................... 165
About This Book ....................................................................................................................... 6
Table of Contents ..................................................................................................................... 7
Chapter 1 - Making Paper Cryptography Tools ...................................................................... 1
What is Cryptography? ................................................................................................................................................ 2
Codes vs. Ciphers ........................................................................................................................................................ 3
Making a Paper Cipher Wheel ..................................................................................................................................... 4
A Virtual Cipher Wheel ............................................................................................................................................... 7
How to Encrypt with the Cipher Wheel ....................................................................................................................... 8
How to Decrypt with the Cipher Wheel ....................................................................................................................... 9
A Different Cipher Tool: The St. Cyr Slide ............................................................................................................... 10
Practice Exercises, Chapter 1, Set A .......................................................................................................................... 11
Doing Cryptography without Paper Tools ................................................................................................................. 11
Practice Exercises, Chapter 1, Set B .......................................................................................................................... 13
Double-Strength Encryption?..................................................................................................................................... 13
Programming a Computer to do Encryption .............................................................................................................. 14
Chapter 2 - Installing Python ................................................................................................. 16
Downloading and Installing Python ........................................................................................................................... 17
Downloading pyperclip.py ......................................................................................................................................... 18
Starting IDLE ............................................................................................................................................................. 18
The Featured Programs .............................................................................................................................................. 19
Line Numbers and Spaces .......................................................................................................................................... 20
Text Wrapping in This Book ..................................................................................................................................... 20
Tracing the Program Online ....................................................................................................................................... 21
Checking Your Typed Code with the Online Diff Tool ............................................................................................. 21
Copying and Pasting Text .......................................................................................................................................... 21
More Info Links ......................................................................................................................................................... 22
Programming and Cryptography ................................................................................................................................ 22
Chapter 3 - The Interactive Shell .......................................................................................... 26
Some Simple Math Stuff ............................................................................................................................................ 26
Integers and Floating Point Values ............................................................................................................................ 27
Expressions ................................................................................................................................................................ 27
Order of Operations ................................................................................................................................................... 28
Evaluating Expressions .............................................................................................................................................. 29
Errors are Okay! ......................................................................................................................................................... 29
Practice Exercises, Chapter 3, Set A .......................................................................................................................... 30
Every Value has a Data Type ..................................................................................................................................... 30
Storing Values in Variables with Assignment Statements ......................................................................................... 30
Overwriting Variables ................................................................................................................................................ 32
Using More Than One Variable ................................................................................................................................. 33
Variable Names .......................................................................................................................................................... 34
Practice Exercises, Chapter 3, Set B .......................................................................................................................... 35
Summary - But When Are We Going to Start Hacking?............................................................................................ 35
Chapter 4 - Strings and Writing Programs ........................................................................... 36
Strings ........................................................................................................................................................................ 36
String Concatenation with the + Operator ................................................................................................................. 38
String Replication with the * Operator ...................................................................................................................... 39
Printing Values with the print() Function ............................................................................................................ 39
Escape Characters ...................................................................................................................................................... 40
Quotes and Double Quotes ........................................................................................................................................ 41
Practice Exercises, Chapter 4, Set A .......................................................................................................................... 42
Indexing ..................................................................................................................................................................... 42
Negative Indexes ........................................................................................................................................................ 44
Slicing ........................................................................................................................................................................ 44
Blank Slice Indexes.................................................................................................................................................... 45
Practice Exercises, Chapter 4, Set B .......................................................................................................................... 46
Writing Programs in IDLE’s File Editor .................................................................................................................... 46
Hello World! .............................................................................................................................................................. 47
Source Code of Hello World ...................................................................................................................................... 47
Saving Your Program ................................................................................................................................................ 48
Running Your Program .............................................................................................................................................. 49
Opening The Programs You’ve Saved ....................................................................................................................... 50
How the “Hello World” Program Works ................................................................................................................... 50
Comments .................................................................................................................................................................. 50
Functions.................................................................................................................................................................... 51
The print() function ............................................................................................................................................. 51
The input() function ............................................................................................................................................. 51
Ending the Program ................................................................................................................................................... 52
Practice Exercises, Chapter 4, Set C .......................................................................................................................... 52
Summary .................................................................................................................................................................... 53
Chapter 5 - The Reverse Cipher ............................................................................................ 54
The Reverse Cipher.................................................................................................................................................... 54
Source Code of the Reverse Cipher Program ............................................................................................................. 55
Sample Run of the Reverse Cipher Program .............................................................................................................. 55
Checking Your Source Code with the Online Diff Tool ............................................................................................ 56
How the Program Works............................................................................................................................................ 56
The len() Function ................................................................................................................................................. 57
Introducing the while Loop ..................................................................................................................................... 58
The Boolean Data Type ............................................................................................................................................. 59
Comparison Operators ............................................................................................................................................... 59
Conditions .................................................................................................................................................................. 62
Blocks ........................................................................................................................................................................ 62
The while Loop Statement ...................................................................................................................................... 63
“Growing” a String .................................................................................................................................................... 64
Tracing Through the Program, Step by Step .............................................................................................................. 67
Using input() In Our Programs ............................................................................................................................. 68
Practice Exercises, Chapter 5, Section A ................................................................................................................... 69
Summary .................................................................................................................................................................... 69
Chapter 6 - The Caesar Cipher .............................................................................................. 70
Implementing a Program ............................................................................................................................................ 70
Source Code of the Caesar Cipher Program ............................................................................................................... 71
Sample Run of the Caesar Cipher Program ................................................................................................................ 72
Checking Your Source Code with the Online Diff Tool ............................................................................................ 73
Practice Exercises, Chapter 6, Set A .......................................................................................................................... 73
How the Program Works............................................................................................................................................ 73
Importing Modules with the import Statement ....................................................................................................... 73
Constants.................................................................................................................................................................... 74
The upper() and lower() String Methods ........................................................................................................ 75
The for Loop Statement ........................................................................................................................................... 76
A while Loop Equivalent of a for Loop ................................................................................................................ 77
Practice Exercises, Chapter 6, Set B .......................................................................................................................... 78
The if Statement ...................................................................................................................................................... 78
The else Statement .................................................................................................................................................. 79
The elif Statement .................................................................................................................................................. 79
The in and not in Operators ................................................................................................................................. 80
The find() String Method ...................................................................................................................................... 81
Practice Exercises, Chapter 6, Set C .......................................................................................................................... 82
Back to the Code ........................................................................................................................................................ 82
Displaying and Copying the Encrypted/Decrypted String ......................................................................................... 85
Encrypt Non-Letter Characters .................................................................................................................................. 86
Summary .................................................................................................................................................................... 87
Chapter 7 - Hacking the Caesar Cipher with the Brute-Force Technique .......................... 88
Hacking Ciphers ........................................................................................................................................................ 88
The Brute-Force Attack ............................................................................................................................................. 89
Source Code of the Caesar Cipher Hacker Program .................................................................................................. 89
Sample Run of the Caesar Cipher Hacker Program ................................................................................................... 90
How the Program Works............................................................................................................................................ 91
The range() Function ............................................................................................................................................ 91
Back to the Code ........................................................................................................................................................ 93
String Formatting ....................................................................................................................................................... 94
Practice Exercises, Chapter 7, Set A .......................................................................................................................... 95
Summary .................................................................................................................................................................... 95
Chapter 8 - Encrypting with the Transposition Cipher ........................................................ 96
Encrypting with the Transposition Cipher ................................................................................................................. 96
Practice Exercises, Chapter 8, Set A .......................................................................................................................... 98
A Transposition Cipher Encryption Program ............................................................................................................. 98
Source Code of the Transposition Cipher Encryption Program ................................................................................. 98
Sample Run of the Transposition Cipher Encryption Program .................................................................................. 99
How the Program Works.......................................................................................................................................... 100
Creating Your Own Functions with def Statements ............................................................................................... 100
The Program’s main() Function ........................................................................................................................... 101
Parameters ................................................................................................................................................................ 102
Variables in the Global and Local Scope ................................................................................................................. 104
The global Statement ........................................................................................................................................... 104
Practice Exercises, Chapter 8, Set B ........................................................................................................................ 106
The List Data Type .................................................................................................................................................. 106
Using the list() Function to Convert Range Objects to Lists ............................................................................. 109
Reassigning the Items in Lists.................................................................................................................................. 110
Reassigning Characters in Strings ............................................................................................................................ 110
Lists of Lists ............................................................................................................................................................ 110
Practice Exercises, Chapter 8, Set C ........................................................................................................................ 111
Using len() and the in Operator with Lists ......................................................................................................... 111
List Concatenation and Replication with the + and * Operators.............................................................................. 112
Practice Exercises, Chapter 8, Set D ........................................................................................................................ 113
The Transposition Encryption Algorithm ................................................................................................................ 113
Augmented Assignment Operators .......................................................................................................................... 115
Back to the Code ...................................................................................................................................................... 116
The join() String Method .................................................................................................................................... 118
Return Values and return Statements .................................................................................................................. 119
Practice Exercises, Chapter 8, Set E ........................................................................................................................ 120
Back to the Code ...................................................................................................................................................... 120
The Special __name__ Variable ............................................................................................................................ 120
Key Size and Message Length ................................................................................................................................. 121
Summary .................................................................................................................................................................. 122
Chapter 9 - Decrypting with the Transposition Cipher ...................................................... 123
Decrypting with the Transposition Cipher on Paper ................................................................................................ 124
Practice Exercises, Chapter 9, Set A ........................................................................................................................ 125
A Transposition Cipher Decryption Program........................................................................................................... 126
Source Code of the Transposition Cipher Decryption Program ............................................................................... 126
How the Program Works.......................................................................................................................................... 127
The math.ceil(), math.floor() and round() Functions ......................................................................... 128
The and and or Boolean Operators ........................................................................................................................ 132
Practice Exercises, Chapter 9, Set B ........................................................................................................................ 133
Truth Tables ............................................................................................................................................................. 133
The and and or Operators are Shortcuts ................................................................................................................ 134
Order of Operations for Boolean Operators ............................................................................................................. 135
Back to the Code ...................................................................................................................................................... 135
Practice Exercises, Chapter 9, Set C ........................................................................................................................ 137
Summary .................................................................................................................................................................. 137
Chapter 10 - Programming a Program to Test Our Program ............................................ 138
Source Code of the Transposition Cipher Tester Program ....................................................................................... 139
Sample Run of the Transposition Cipher Tester Program ........................................................................................ 140
How the Program Works.......................................................................................................................................... 141
Pseudorandom Numbers and the random.seed() Function ............................................................................... 141
The random.randint() Function ..................................................................................................................... 143
References ................................................................................................................................................................ 143
The copy.deepcopy() Functions ...................................................................................................................... 147
Practice Exercises, Chapter 10, Set A ...................................................................................................................... 148
The random.shuffle() Function ..................................................................................................................... 148
Randomly Scrambling a String ................................................................................................................................ 149
Back to the Code ...................................................................................................................................................... 149
The sys.exit() Function ................................................................................................................................... 150
Testing Our Test Program ........................................................................................................................................ 151
Summary .................................................................................................................................................................. 152
Chapter 11 - Encrypting and Decrypting Files ................................................................... 153
Plain Text Files ........................................................................................................................................................ 154
Source Code of the Transposition File Cipher Program ........................................................................................... 154
Sample Run of the Transposition File Cipher Program ........................................................................................... 157
Reading From Files .................................................................................................................................................. 157
Writing To Files ....................................................................................................................................................... 158
How the Program Works.......................................................................................................................................... 159
The os.path.exists() Function ..................................................................................................................... 160
The startswith() and endswith() String Methods ..................................................................................... 161
The title() String Method ................................................................................................................................. 162
The time Module and time.time() Function .................................................................................................. 163
Back to the Code ...................................................................................................................................................... 164
Practice Exercises, Chapter 11, Set A ...................................................................................................................... 165
Back to the Code ......................................................................................................................................................
165
Chapter 12 - Detecting English Programmatically ............................................................ 166
How Can a Computer Understand English? ............................................................................................................. 167
Practice Exercises, Chapter 12, Section A ............................................................................................................... 169
The Detect English Module ..................................................................................................................................... 169
Source Code for the Detect English Module ............................................................................................................ 169
How the Program Works.......................................................................................................................................... 170
Dictionaries and the Dictionary Data Type .............................................................................................................. 171
Adding or Changing Items in a Dictionary .............................................................................................................. 172
Practice Exercises, Chapter 12, Set B ...................................................................................................................... 173
Using the len() Function with Dictionaries .......................................................................................................... 173
Using the in Operator with Dictionaries ................................................................................................................. 173
Using for Loops with Dictionaries ........................................................................................................................ 174
Practice Exercises, Chapter 12, Set C ...................................................................................................................... 174
The Difference Between Dictionaries and Lists ....................................................................................................... 174
Finding Items is Faster with Dictionaries Than Lists ............................................................................................... 175
The split() Method ............................................................................................................................................ 175
The None Value ...................................................................................................................................................... 176
Back to the Code ...................................................................................................................................................... 177
“Divide by Zero” Errors ........................................................................................................................................... 179
The float(), int(), and str() Functions and Integer Division ..................................................................... 179
Practice Exercises, Chapter 12, Set D ...................................................................................................................... 180
Back to the Code ...................................................................................................................................................... 180
The append() List Method ..................................................................................................................................... 182
Default Arguments ................................................................................................................................................... 183
Calculating Percentage ............................................................................................................................................. 184
Practice Exercises, Chapter 12, Set E ...................................................................................................................... 185
Summary .................................................................................................................................................................. 186
Chapter 13 - Hacking the Transposition Cipher ................................................................ 187
Source Code of the Transposition Cipher Hacker Program ..................................................................................... 187
Sample Run of the Transposition Breaker Program ................................................................................................. 189
How the Program Works.......................................................................................................................................... 190
Multi-line Strings with Triple Quotes ...................................................................................................................... 190
The First Affine Key Problem.........................
Chapter 12 - Detecting English Programmatically ............................................................ 166
How Can a Computer Understand English? ............................................................................................................. 167
Practice Exercises, Chapter 12, Section A ............................................................................................................... 169
The Detect English Module ..................................................................................................................................... 169
Source Code for the Detect English Module ............................................................................................................ 169
How the Program Works.......................................................................................................................................... 170
Dictionaries and the Dictionary Data Type .............................................................................................................. 171
Adding or Changing Items in a Dictionary .............................................................................................................. 172
Practice Exercises, Chapter 12, Set B ...................................................................................................................... 173
Using the len() Function with Dictionaries .......................................................................................................... 173
Using the in Operator with Dictionaries ................................................................................................................. 173
Using for Loops with Dictionaries ........................................................................................................................ 174
Practice Exercises, Chapter 12, Set C ...................................................................................................................... 174
The Difference Between Dictionaries and Lists ....................................................................................................... 174
Finding Items is Faster with Dictionaries Than Lists ............................................................................................... 175
The split() Method ............................................................................................................................................ 175
The None Value ...................................................................................................................................................... 176
Back to the Code ...................................................................................................................................................... 177
“Divide by Zero” Errors ........................................................................................................................................... 179
The float(), int(), and str() Functions and Integer Division ..................................................................... 179
Practice Exercises, Chapter 12, Set D ...................................................................................................................... 180
Back to the Code ...................................................................................................................................................... 180
The append() List Method ..................................................................................................................................... 182
Default Arguments ................................................................................................................................................... 183
Calculating Percentage ............................................................................................................................................. 184
Practice Exercises, Chapter 12, Set E ...................................................................................................................... 185
Summary .................................................................................................................................................................. 186
Chapter 13 - Hacking the Transposition Cipher ................................................................ 187
Source Code of the Transposition Cipher Hacker Program ..................................................................................... 187
Sample Run of the Transposition Breaker Program ................................................................................................. 189
How the Program Works.......................................................................................................................................... 190
Multi-line Strings with Triple Quotes ...................................................................................................................... 190
The First Affine Key Problem.........................
191
The strip() String Method ................................................................................................................................. 193
Practice Exercises, Chapter 13, Set A ...................................................................................................................... 195
Summary .................................................................................................................................................................. 195
Chapter 14 - Modular Arithmetic with the Multiplicative and Affine Ciphers ................. 196
Oh No Math! ............................................................................................................................................................ 197
Math Oh Yeah! ........................................................................................................................................................ 197
Modular Arithmetic (aka Clock Arithmetic) ............................................................................................................ 197
The % Mod Operator ............................................................................................................................................... 199
Practice Exercises, Chapter 14, Set A ...................................................................................................................... 199
GCD: Greatest Common Divisor (aka Greatest Common Factor) ........................................................................... 199
Visualize Factors and GCD with Cuisenaire Rods ................................................................................................... 200
Practice Exercises, Chapter 14, Set B ...................................................................................................................... 202
Multiple Assignment ................................................................................................................................................ 202
Swapping Values with the Multiple Assignment Trick ............................................................................................ 203
Euclid’s Algorithm for Finding the GCD of Two Numbers..................................................................................... 203
“Relatively Prime” ................................................................................................................................................... 205
Practice Exercises, Chapter 14, Set C ...................................................................................................................... 205
The Multiplicative Cipher ........................................................................................................................................ 205
Practice Exercises, Chapter 14, Set D ...................................................................................................................... 207
Multiplicative Cipher + Caesar Cipher = The Affine Cipher ................................................................................... 207
Summary .................................................................................................................................................................. ......................................................................................................... 207
Decrypting with the Affine Cipher........................................................................................................................... 208
Finding Modular Inverses ........................................................................................................................................ 209
The // Integer Division Operator ........................................................................................................................... 210
Source Code of the cryptomath Module ............................................................................................................. 210
Practice Exercises, Chapter 14, Set E ...................................................................................................................... 211
Summary .................................................................................................................................................................. 211
Chapter 15 - The Affine Cipher ........................................................................................... 213
Source Code of the Affine Cipher Program ............................................................................................................. 214
Sample Run of the Affine Cipher Program .............................................................................................................. 216
Practice Exercises, Chapter 15, Set A ...................................................................................................................... 216
How the Program Works.......................................................................................................................................... 216
Splitting One Key into Two Keys ............................................................................................................................ 218
How the Program Works..........................................................................................................................................
The strip() String Method ................................................................................................................................. 193
Practice Exercises, Chapter 13, Set A ...................................................................................................................... 195
Summary .................................................................................................................................................................. 195
Chapter 14 - Modular Arithmetic with the Multiplicative and Affine Ciphers ................. 196
Oh No Math! ............................................................................................................................................................ 197
Math Oh Yeah! ........................................................................................................................................................ 197
Modular Arithmetic (aka Clock Arithmetic) ............................................................................................................ 197
The % Mod Operator ............................................................................................................................................... 199
Practice Exercises, Chapter 14, Set A ...................................................................................................................... 199
GCD: Greatest Common Divisor (aka Greatest Common Factor) ........................................................................... 199
Visualize Factors and GCD with Cuisenaire Rods ................................................................................................... 200
Practice Exercises, Chapter 14, Set B ...................................................................................................................... 202
Multiple Assignment ................................................................................................................................................ 202
Swapping Values with the Multiple Assignment Trick ............................................................................................ 203
Euclid’s Algorithm for Finding the GCD of Two Numbers..................................................................................... 203
“Relatively Prime” ................................................................................................................................................... 205
Practice Exercises, Chapter 14, Set C ...................................................................................................................... 205
The Multiplicative Cipher ........................................................................................................................................ 205
Practice Exercises, Chapter 14, Set D ...................................................................................................................... 207
Multiplicative Cipher + Caesar Cipher = The Affine Cipher ................................................................................... 207
Summary .................................................................................................................................................................. ......................................................................................................... 207
Decrypting with the Affine Cipher........................................................................................................................... 208
Finding Modular Inverses ........................................................................................................................................ 209
The // Integer Division Operator ........................................................................................................................... 210
Source Code of the cryptomath Module ............................................................................................................. 210
Practice Exercises, Chapter 14, Set E ...................................................................................................................... 211
Summary .................................................................................................................................................................. 211
Chapter 15 - The Affine Cipher ........................................................................................... 213
Source Code of the Affine Cipher Program ............................................................................................................. 214
Sample Run of the Affine Cipher Program .............................................................................................................. 216
Practice Exercises, Chapter 15, Set A ...................................................................................................................... 216
How the Program Works.......................................................................................................................................... 216
Splitting One Key into Two Keys ............................................................................................................................ 218
How the Program Works..........................................................................................................................................
The
Tuple
Data Type
...............................................................................................................................................
218
Input Validation on the Keys ................................................................................................................................... 219
The Affine Cipher Encryption Function .................................................................................................................. 220
The Affine Cipher Decryption Function .................................................................................................................. 221
Generating Random Keys ........................................................................................................................................ 222
The Second Affine Key Problem: How Many Keys Can the Affine Cipher Have? ................................................. 223
Summary .................................................................................................................................................................. 225
Chapter 16 - Hacking the Affine Cipher ............................................................................. 226
Source Code of the Affine Cipher Hacker Program ................................................................................................. 226
Sample Run of the Affine Cipher Hacker Program .................................................................................................. 228
How the Program Works.......................................................................................................................................... 228
The Affine Cipher Hacking Function ....................................................................................................................... 230
The ** Exponent Operator ...................................................................................................................................... 230
The continue Statement ...................................................................................................................................... 231
Practice Exercises, Chapter 16, Set A ...................................................................................................................... 234
Summary .................................................................................................................................................................. 234
Chapter 17 - The Simple Substitution Cipher .................................................................... 235
The Simple Substitution Cipher with Paper and Pencil ........................................................................................... 236
Practice Exercises, Chapter 17, Set A ...................................................................................................................... 236
Source Code of the Simple Substitution Cipher ....................................................................................................... 237
Sample Run of the Simple Substitution Cipher Program ......................................................................................... 239
Input Validation on the Keys ................................................................................................................................... 219
The Affine Cipher Encryption Function .................................................................................................................. 220
The Affine Cipher Decryption Function .................................................................................................................. 221
Generating Random Keys ........................................................................................................................................ 222
The Second Affine Key Problem: How Many Keys Can the Affine Cipher Have? ................................................. 223
Summary .................................................................................................................................................................. 225
Chapter 16 - Hacking the Affine Cipher ............................................................................. 226
Source Code of the Affine Cipher Hacker Program ................................................................................................. 226
Sample Run of the Affine Cipher Hacker Program .................................................................................................. 228
How the Program Works.......................................................................................................................................... 228
The Affine Cipher Hacking Function ....................................................................................................................... 230
The ** Exponent Operator ...................................................................................................................................... 230
The continue Statement ...................................................................................................................................... 231
Practice Exercises, Chapter 16, Set A ...................................................................................................................... 234
Summary .................................................................................................................................................................. 234
Chapter 17 - The Simple Substitution Cipher .................................................................... 235
The Simple Substitution Cipher with Paper and Pencil ........................................................................................... 236
Practice Exercises, Chapter 17, Set A ...................................................................................................................... 236
Source Code of the Simple Substitution Cipher ....................................................................................................... 237
Sample Run of the Simple Substitution Cipher Program ......................................................................................... 239
239
The Program’s main() Function ........................................................................................................................... 240
The sort() List Method ....................................................................................................................................... 241
Wrapper Functions ................................................................................................................................................... 242
The Program’s translateMessage() Function ............................................................................................... 243
The isupper() and islower() String Methods .............................................................................................. 245
Practice Exercises, Chapter 17, Set B ...................................................................................................................... 247
Generating a Random Key ....................................................................................................................................... 247
Encrypting Spaces and Punctuation ......................................................................................................................... 248
Practice Exercises, Chapter 17, Set C ...................................................................................................................... 249
Summary .................................................................................................................................................................. 249
Chapter 18 - Hacking the Simple Substitution Cipher ...................................................... 250
Computing Word Patterns ........................................................................................................................................ 251
Getting a List of Candidates for a Cipherword ........................................................................................................ 252
Practice Exercises, Chapter 18, Set A ...................................................................................................................... 253
Source Code of the Word Pattern Module ............................................................................................................... 253
Sample Run of the Word Pattern Module ................................................................................................................ 255
How the Program Works.......................................................................................................................................... 256
The pprint.pprint() and pprint.pformat() Functions ........................................................................ 256
Building Strings in Python with Lists ...................................................................................................................... 257
Calculating the Word Pattern ................................................................................................................................... 258
The Word Pattern Program’s main() Function ..................................................................................................... 259
Hacking the Simple Substitution Cipher .................................................................................................................. 262
Source Code of the Simple Substitution Hacking Program...................................................................................... 262
Hacking the Simple Substitution Cipher (in Theory) ............................................................................................... 266
Explore the Hacking Functions with the Interactive Shell ....................................................................................... 266
How the Program Works.......................................................................................................................................... 271
Import All the Things ............................................................................................................................................... 272
A Brief Intro to Regular Expressions and the sub() Regex Method ......................................................................... 272
The Hacking Program’s main() Function ............................................................................................................. 273
Partially Hacking the Cipher .................................................................................................................................... 274
Blank Cipherletter Mappings ................................................................................................................................... 275
Adding Letters to a Cipherletter Mapping ............................................................................................................... 276
Intersecting Two Letter Mappings ........................................................................................................................... 277
Removing Solved Letters from the Letter Mapping ................................................................................................. 278
Hacking the Simple Substitution Cipher .................................................................................................................. 281
Creating a Key from a Letter Mapping .................................................................................................................... 283
Couldn’t We Just Encrypt the Spaces Too? ............................................................................................................. 285
Summary .................................................................................................................................................................. 286
Chapter 19 - The Vigenère Cipher ...................................................................................... 287
Le Chiffre Indéchiffrable ......................................................................................................................................... 288
Multiple “Keys” in the Vigenère Key ...................................................................................................................... 288
Source Code of Vigenère Cipher Program ............................................................................................................... 291
Sample Run of the Vigenère Cipher Program .......................................................................................................... 294
How the Program Works.......................................................................................................................................... 294
Summary .................................................................................................................................................................. 298
Chapter 20 - Frequency Analysis ........................................................................................ 299
The Code for Matching Letter Frequencies ............................................................................................................. 304
How the Program Works.......................................................................................................................................... 306
The Most Common Letters, “ETAOIN” .................................................................................................................. 307
The Program’s getLettersCount() Function ................................................................................................. 307
The Program’s getItemAtIndexZero() Function .......................................................................................... 308
The Program’s getFrequencyOrder() Function ............................................................................................ 308
The sort() Method’s key and reverse Keyword Arguments ......................................................................... 310
Passing Functions as Values .................................................................................................................................... 311
Converting Dictionaries to Lists with the keys(), values(), items() Dictionary Methods ......................................... 313
Sorting the Items from a Dictionary ......................................................................................................................... 315
The Program’s englishFreqMatchScore() Function ................................................................................... 316
Summary .................................................................................................................................................................. 317
Chapter 21 - Hacking the Vigenère Cipher ........................................................................ 318
The Dictionary Attack .............................................................................................................................................. 319
Source Code for a Vigenère Dictionary Attack Program ......................................................................................... 319
Sample Run of the Vigenère Dictionary Hacker Program ....................................................................................... 320
The readlines() File Object Method ................................................................................................................ 321
The Babbage Attack & Kasiski Examination ........................................................................................................... 321
Kasiski Examination, Step 1 – Find Repeat Sequences’ Spacings ........................................................................... 321
Kasiski Examination, Step 2 – Get Factors of Spacings .......................................................................................... 322
Get Every Nth Letters from a String ........................................................................................................................ 323
Frequency Analysis .................................................................................................................................................. 323
Brute-Force through the Possible Keys .................................................................................................................... 325
Source Code for the Vigenère Hacking Program ..................................................................................................... 326
Sample Run of the Vigenère Hacking Program ....................................................................................................... 332
How the Program Works.......................................................................................................................................... 334
Finding Repeated Sequences ................................................................................................................................... 335
Calculating Factors .................................................................................................................................................. 337
Removing Duplicates with the set() Function .......................................................................................................... 338
The Kasiski Examination Algorithm........................................................................................................................ 341
The extend() List Method ................................................................................................................................... 342
The end Keyword Argument for print() ........................................................................................................... 347
The itertools.product() Function .............................................................................................................. 348
The break Statement ............................................................................................................................................. 352
Practice Exercises, Chapter 21, Set A ...................................................................................................................... 354
Modifying the Constants of the Hacking Program ................................................................................................... 354
Summary .................................................................................................................................................................. 355
Chapter 22 - The One-Time Pad Cipher .............................................................................. 356
The Unbreakable One-Time Pad Cipher .................................................................................................................. 357
Why the One-Time Pad is Unbreakable ................................................................................................................... 357
Beware Pseudorandomness ...................................................................................................................................... 358
Beware the Two-Time Pad ...................................................................................................................................... 358
The Two-Time Pad is the Vigenère Cipher .............................................................................................................. 359
Practice Exercises, Chapter 22, Set A ...................................................................................................................... 360
Summary .................................................................................................................................................................. 360
Chapter 23 - Finding Prime Numbers ................................................................................. 361
Prime Numbers ........................................................................................................................................................ 362
Composite Numbers................................................................................................................................................. 363
Source Code for The Prime Sieve Module ............................................................................................................... 363
How the Program Works.......................................................................................................................................... 364
How to Calculate if a Number is Prime ................................................................................................................... 365
The Sieve of Eratosthenes ........................................................................................................................................ 366
The primeSieve() Function............................................................................................................................... 368
Detecting Prime Numbers ........................................................................................................................................ 369
Source Code for the Rabin-Miller Module ............................................................................................................... 370
Sample Run of the Rabin Miller Module ................................................................................................................. 372
How the Program Works.......................................................................................................................................... 372
The Rabin-Miller Primality Algorithm .................................................................................................................... 372
The New and Improved isPrime() Function ...................................................................................................... 373
Summary .................................................................................................................................................................. 375
Chapter 24 - Public Key Cryptography and the RSA Cipher .............................................. 378
Public Key Cryptography......................................................................................................................................... 379
The Dangers of “Textbook” RSA ............................................................................................................................ 381
A Note About Authentication .................................................................................................................................. 381
The Man-In-The-Middle Attack .............................................................................................................................. 382
Generating Public and Private Keys......................................................................................................................... 383
Source Code for the RSA Key Generation Program ................................................................................................ 383
Sample Run of the RSA Key Generation Program .................................................................................................. 385
How the Key Generation Program Works ............................................................................................................... 386
The Program’s generateKey() Function ........................................................................................................... 387
RSA Key File Format .............................................................................................................................................. 390
Hybrid Cryptosystems ............................................................................................................................................. 391
Source Code for the RSA Cipher Program .............................................................................................................. 391
Sample Run of the RSA Cipher Program ................................................................................................................. 395
Practice Exercises, Chapter 24, Set A ...................................................................................................................... 397
Digital Signatures .................................................................................................................................................... 397
How the RSA Cipher Program Works ..................................................................................................................... 398
ASCII: Using Numbers to Represent Characters ..................................................................................................... 400
The chr() and ord() Functions .......................................................................................................................... 400
Practice Exercises, Chapter 24, Set B ...................................................................................................................... 401
Blocks ...................................................................................................................................................................... 401
Converting Strings to Blocks with getBlocksFromText() ............................................................................. 404
The encode() String Method and the Bytes Data Type ....................................................................................... 405
The bytes() Function and decode() Bytes Method ........................................................................................ 405
Practice Exercises, Chapter 24, Set C ...................................................................................................................... 406
Back to the Code ...................................................................................................................................................... 406
The min() and max() Functions .......................................................................................................................... 407
The insert() List Method ................................................................................................................................... 410
The Mathematics of RSA Encrypting and Decrypting ............................................................................................. 411
The pow() Function ............................................................................................................................................... 411
Reading in the Public & Private Keys from their Key Files ..................................................................................... 413
The Full RSA Encryption Process ........................................................................................................................... 413
The Full RSA Decryption Process ........................................................................................................................... 416
Practice Exercises, Chapter 24, Set D ...................................................................................................................... 418
Why Can’t We Hack the RSA Cipher ...................................................................................................................... 418
Summary .................................................................................................................................................................. 420
About the Author .................................................................................................................. 422
The Program’s main() Function ........................................................................................................................... 240
The sort() List Method ....................................................................................................................................... 241
Wrapper Functions ................................................................................................................................................... 242
The Program’s translateMessage() Function ............................................................................................... 243
The isupper() and islower() String Methods .............................................................................................. 245
Practice Exercises, Chapter 17, Set B ...................................................................................................................... 247
Generating a Random Key ....................................................................................................................................... 247
Encrypting Spaces and Punctuation ......................................................................................................................... 248
Practice Exercises, Chapter 17, Set C ...................................................................................................................... 249
Summary .................................................................................................................................................................. 249
Chapter 18 - Hacking the Simple Substitution Cipher ...................................................... 250
Computing Word Patterns ........................................................................................................................................ 251
Getting a List of Candidates for a Cipherword ........................................................................................................ 252
Practice Exercises, Chapter 18, Set A ...................................................................................................................... 253
Source Code of the Word Pattern Module ............................................................................................................... 253
Sample Run of the Word Pattern Module ................................................................................................................ 255
How the Program Works.......................................................................................................................................... 256
The pprint.pprint() and pprint.pformat() Functions ........................................................................ 256
Building Strings in Python with Lists ...................................................................................................................... 257
Calculating the Word Pattern ................................................................................................................................... 258
The Word Pattern Program’s main() Function ..................................................................................................... 259
Hacking the Simple Substitution Cipher .................................................................................................................. 262
Source Code of the Simple Substitution Hacking Program...................................................................................... 262
Hacking the Simple Substitution Cipher (in Theory) ............................................................................................... 266
Explore the Hacking Functions with the Interactive Shell ....................................................................................... 266
How the Program Works.......................................................................................................................................... 271
Import All the Things ............................................................................................................................................... 272
A Brief Intro to Regular Expressions and the sub() Regex Method ......................................................................... 272
The Hacking Program’s main() Function ............................................................................................................. 273
Partially Hacking the Cipher .................................................................................................................................... 274
Blank Cipherletter Mappings ................................................................................................................................... 275
Adding Letters to a Cipherletter Mapping ............................................................................................................... 276
Intersecting Two Letter Mappings ........................................................................................................................... 277
Removing Solved Letters from the Letter Mapping ................................................................................................. 278
Hacking the Simple Substitution Cipher .................................................................................................................. 281
Creating a Key from a Letter Mapping .................................................................................................................... 283
Couldn’t We Just Encrypt the Spaces Too? ............................................................................................................. 285
Summary .................................................................................................................................................................. 286
Chapter 19 - The Vigenère Cipher ...................................................................................... 287
Le Chiffre Indéchiffrable ......................................................................................................................................... 288
Multiple “Keys” in the Vigenère Key ...................................................................................................................... 288
Source Code of Vigenère Cipher Program ............................................................................................................... 291
Sample Run of the Vigenère Cipher Program .......................................................................................................... 294
How the Program Works.......................................................................................................................................... 294
Summary .................................................................................................................................................................. 298
Chapter 20 - Frequency Analysis ........................................................................................ 299
The Code for Matching Letter Frequencies ............................................................................................................. 304
How the Program Works.......................................................................................................................................... 306
The Most Common Letters, “ETAOIN” .................................................................................................................. 307
The Program’s getLettersCount() Function ................................................................................................. 307
The Program’s getItemAtIndexZero() Function .......................................................................................... 308
The Program’s getFrequencyOrder() Function ............................................................................................ 308
The sort() Method’s key and reverse Keyword Arguments ......................................................................... 310
Passing Functions as Values .................................................................................................................................... 311
Converting Dictionaries to Lists with the keys(), values(), items() Dictionary Methods ......................................... 313
Sorting the Items from a Dictionary ......................................................................................................................... 315
The Program’s englishFreqMatchScore() Function ................................................................................... 316
Summary .................................................................................................................................................................. 317
Chapter 21 - Hacking the Vigenère Cipher ........................................................................ 318
The Dictionary Attack .............................................................................................................................................. 319
Source Code for a Vigenère Dictionary Attack Program ......................................................................................... 319
Sample Run of the Vigenère Dictionary Hacker Program ....................................................................................... 320
The readlines() File Object Method ................................................................................................................ 321
The Babbage Attack & Kasiski Examination ........................................................................................................... 321
Kasiski Examination, Step 1 – Find Repeat Sequences’ Spacings ........................................................................... 321
Kasiski Examination, Step 2 – Get Factors of Spacings .......................................................................................... 322
Get Every Nth Letters from a String ........................................................................................................................ 323
Frequency Analysis .................................................................................................................................................. 323
Brute-Force through the Possible Keys .................................................................................................................... 325
Source Code for the Vigenère Hacking Program ..................................................................................................... 326
Sample Run of the Vigenère Hacking Program ....................................................................................................... 332
How the Program Works.......................................................................................................................................... 334
Finding Repeated Sequences ................................................................................................................................... 335
Calculating Factors .................................................................................................................................................. 337
Removing Duplicates with the set() Function .......................................................................................................... 338
The Kasiski Examination Algorithm........................................................................................................................ 341
The extend() List Method ................................................................................................................................... 342
The end Keyword Argument for print() ........................................................................................................... 347
The itertools.product() Function .............................................................................................................. 348
The break Statement ............................................................................................................................................. 352
Practice Exercises, Chapter 21, Set A ...................................................................................................................... 354
Modifying the Constants of the Hacking Program ................................................................................................... 354
Summary .................................................................................................................................................................. 355
Chapter 22 - The One-Time Pad Cipher .............................................................................. 356
The Unbreakable One-Time Pad Cipher .................................................................................................................. 357
Why the One-Time Pad is Unbreakable ................................................................................................................... 357
Beware Pseudorandomness ...................................................................................................................................... 358
Beware the Two-Time Pad ...................................................................................................................................... 358
The Two-Time Pad is the Vigenère Cipher .............................................................................................................. 359
Practice Exercises, Chapter 22, Set A ...................................................................................................................... 360
Summary .................................................................................................................................................................. 360
Chapter 23 - Finding Prime Numbers ................................................................................. 361
Prime Numbers ........................................................................................................................................................ 362
Composite Numbers................................................................................................................................................. 363
Source Code for The Prime Sieve Module ............................................................................................................... 363
How the Program Works.......................................................................................................................................... 364
How to Calculate if a Number is Prime ................................................................................................................... 365
The Sieve of Eratosthenes ........................................................................................................................................ 366
The primeSieve() Function............................................................................................................................... 368
Detecting Prime Numbers ........................................................................................................................................ 369
Source Code for the Rabin-Miller Module ............................................................................................................... 370
Sample Run of the Rabin Miller Module ................................................................................................................. 372
How the Program Works.......................................................................................................................................... 372
The Rabin-Miller Primality Algorithm .................................................................................................................... 372
The New and Improved isPrime() Function ...................................................................................................... 373
Summary .................................................................................................................................................................. 375
Chapter 24 - Public Key Cryptography and the RSA Cipher .............................................. 378
Public Key Cryptography......................................................................................................................................... 379
The Dangers of “Textbook” RSA ............................................................................................................................ 381
A Note About Authentication .................................................................................................................................. 381
The Man-In-The-Middle Attack .............................................................................................................................. 382
Generating Public and Private Keys......................................................................................................................... 383
Source Code for the RSA Key Generation Program ................................................................................................ 383
Sample Run of the RSA Key Generation Program .................................................................................................. 385
How the Key Generation Program Works ............................................................................................................... 386
The Program’s generateKey() Function ........................................................................................................... 387
RSA Key File Format .............................................................................................................................................. 390
Hybrid Cryptosystems ............................................................................................................................................. 391
Source Code for the RSA Cipher Program .............................................................................................................. 391
Sample Run of the RSA Cipher Program ................................................................................................................. 395
Practice Exercises, Chapter 24, Set A ...................................................................................................................... 397
Digital Signatures .................................................................................................................................................... 397
How the RSA Cipher Program Works ..................................................................................................................... 398
ASCII: Using Numbers to Represent Characters ..................................................................................................... 400
The chr() and ord() Functions .......................................................................................................................... 400
Practice Exercises, Chapter 24, Set B ...................................................................................................................... 401
Blocks ...................................................................................................................................................................... 401
Converting Strings to Blocks with getBlocksFromText() ............................................................................. 404
The encode() String Method and the Bytes Data Type ....................................................................................... 405
The bytes() Function and decode() Bytes Method ........................................................................................ 405
Practice Exercises, Chapter 24, Set C ...................................................................................................................... 406
Back to the Code ...................................................................................................................................................... 406
The min() and max() Functions .......................................................................................................................... 407
The insert() List Method ................................................................................................................................... 410
The Mathematics of RSA Encrypting and Decrypting ............................................................................................. 411
The pow() Function ............................................................................................................................................... 411
Reading in the Public & Private Keys from their Key Files ..................................................................................... 413
The Full RSA Encryption Process ........................................................................................................................... 413
The Full RSA Decryption Process ........................................................................................................................... 416
Practice Exercises, Chapter 24, Set D ...................................................................................................................... 418
Why Can’t We Hack the RSA Cipher ...................................................................................................................... 418
Summary .................................................................................................................................................................. 420
About the Author .................................................................................................................. 422
Enjoy.....
0 comments:
Post a Comment