INDEX

Symbols

+ (addition and concatenation operator), 12, 2324, 8990

+= (augmented addition assignment operator), 9192

/= (augmented division assignment operator), 92

*= (augmented multiplication assignment operator), 92

-= (augmented subtraction assignment operator), 92

\ (backslash), 2829

: (colon), 26

/ (division operator), 12

" (double quote), 2930

__ (double underscore), 95

== (equal to comparison operator), 4345

** (exponent operator), 201

> (greater than comparison operator), 4344

>= (greater than or equal to comparison operator), 4344

# (hash mark), 34

// (integer division operator), 181

< (less than comparison operator), 4344

<= (less than or equal to comparison operator), 4344

% (modulo operator), 173

* (multiplication and replication operator), 12, 24, 8990

\n (newline escape character), 2829

!= (not equal to comparison operator), 4345

() (parentheses), 14, 28, 35

' (single quote), 22, 2830

[] (square brackets), 26

%s (string formatting), 75

- (subtraction operator), 12

\t (tab escape character), 2829

''' or """ (triple quotes), 164165

A

absolute path, 131

addition operator (+), 12

Adleman, Leonard, 337

affine cipher, 177181

decrypting with, 179180, 194195

encrypting with, 179, 193194

hacking, 201204

affineCipher.py, 186187

affineHacker.py, 198199

affineKeyTest.py, 192

al_sweigart_privkey.txt, 342, 343

al_sweigart_pubkey.txt, 342, 343, 346, 363

and operator, 106, 108

append() list method, 121, 155156, 253254

arguments, 28

default, 157

defining functions with parameters, 8384

for file permissions, 131, 132

keyword

end, for print(), 306

key, for sort(), 273

repeat, for itertools.product(), 307

reverse, for sort(), 273, 275

lists as, 121122, 301

optional, 131

passing references as, 121, 234

assignment statements, 16

augmented assignment operators, 9192

authentication, 337338

B

Babbage, Charles, 247, 282

backslash (\), 2829

Bellaso, Giovan Battista, 247

Bernstein, Daniel J., xxi–xxii

blocks

of code, 4546

in cryptography, 350

converting block to string, 354355

converting string to block, 350352

Boolean operators, 106108

Boolean values, 43

breakpoints, for debugging, 379380

break statement, 311312

brute-force attack, 69

built-in functions, 56

C

Caesar cipher, 4

decrypting with, 7

encrypting with, 7

hacking, 69

caesarCipher.py, 5455

caesarHacker.py, 7071

candidates (possible cipherwords), 223

case (of letters)

converting with string methods, 134, 216217

sensitivity, in Python, 18

ceil() function (math module), 103104

chiffre indéchiffrable, 247

cipher disk, 4

cipherletters, 222

cipherletter mappings, 224, 232241

adding letters to, 233234

intersecting, 234235

ciphers, vs. codes, 34

ciphertext, 4

cipher wheel, 4

decrypting with, 67

encrypting with, 56

cipherwords, 222

clock arithmetic, 172173

close() file object method, 132

code breaker, 2

codes, vs. ciphers, 34

colon (:), 26

comments, 34

comparison operators, 4345

composite numbers, 323, 326327

concatenation operator (+), 2324, 8990

condition, 4243

confidentiality, 338

constants, 57, 313

continue statement, 202204

copy.deepcopy() function, 122

copy() function (pyperclip module), 56, 65

cryptanalyst, 2

cryptographers, 2

Adleman, Leonard, 337

Bellaso, Giovan Battista, 247

Kerckhoffs, Auguste, 70

Rivest, Ron, 337

Schneier, Bruce, 337

Shamir, Adi, 337

Turing, Alan, 188, 268, 352

Vigenère, Blaise de, 247

cryptography, 2

cryptomath.py, 182

Cuisenaire rods, 174175

D

data types, 13

Boolean, 43

dictionary, 146150

floating-point, 13

integer, 13

list, 8690

string, 22

tuple, 190

debugging

in IDLE, 375379

setting breakpoints, 379380

decoding, 3

decrypting, 2

with the affine cipher, 179180, 194195

with the Caesar cipher, 7

with a cipher wheel, 67

with the public key cipher, 368

with the reverse cipher, 39

with the simple substitution cipher, 216

with the transposition cipher, 100101

with the Vigenère cipher, 248249

deepcopy() function (copy module), 122

def statement, 8284

detectEnglish.py, 143144

dictionaries, 146150

adding or changing items in, 147

in operator and, 148

len() function and, 148

lists, differences from, 147

dictionary attacks, 250251, 280

dictionary.txt, 280

diff tool, checking source code with, 3132

digital signatures, 338

division operator (/), 12

integer (//), 181

divisors. See factors

double encryption, 8

double quote ("), 2930

dunder (__, double underscore), 95

dunder name dunder (__name__) variable, 9596

duplicating lists, 122

E

elif statement, 61

else statement, 60

encoding, 3

encrypting

with the affine cipher, 179, 193194

with the Caesar cipher, 7

with a cipher wheel, 56

with the public key cipher, 367368

with the reverse cipher, 39

with the simple substitution cipher, 215

with the tranposition cipher, 7880

with the Vigenère cipher, 248249

encryption key, 4

end keyword argument, for print(), 306

endswith() string method, 135

English, detecting programmatically, 141143, 156159

equal to operator (==), 4345

Eratosthenes, sieve of, 328331

errors, 15. See also debugging

finding with diff tool, 3132

ImportError, 55

IndexError, 25

ModuleNotFoundError, 226

NameError, 33

SyntaxError, 15, 28, 29

escape characters, 2830

ETAOIN, 260

Euclid’s algorithm, 176177

extended, 181

execution, program

defined, 34

terminating with sys.exit(), 124

exists() function (os.path module), 133

exit() function (sys module), 124

exiting programs, 3536, 124

exponent operator (**), 201

expressions, 1215

defined, 13

evaluating, 1415

extend() list method, 301

F

factors

of composite numbers, 323

greatest common divisors, 173175, 176177

of prime numbers, 322, 357

of spacings, in Kasiski examination, 283284, 297

False (Boolean value), 43

file editor (IDLE), 30

file objects

close() file object method, 132

open() function, 131

read() file object method, 132

write() file object method, 131

files, plain text, 128, 130134

find() string method, 6263

float, 13

float() function, 154

floating-point numbers, 13

floor() function (math module), 103104

for statement, 5859

freqAnalysis.py, 265266

frequency

defined, 260

of English letters, 261

frequency analysis, 259, 285287

frequency match score, 262264, 286

functions, 28

calling from modules, 65

passing as values, 272273

G

Gadsby (Wright), 266

gcd() function, 176177

GCD (greatest common divisor), 173175, 176177. See also factors

generating keys

affine cipher, 195196

public key cipher, 340

simple substitution cipher, 218

global scope, 84

googol, 322

greater than operator (>), 4344

greater than or equal to (>=) operator, 4344

greatest common divisor (GCD), 173175, 176177. See also factors

H

hacker, 2

hacking

affine cipher, 201204

Caesar cipher, 69

public key cipher, why we can’t, 355357

simple substitution cipher, 222225, 241245

transposition cipher, 166168

Vigenère cipher, 280282

hash mark (#), 34

hello.py, 31

hybrid cryptosystems, 347

I

IDLE, xxvii, 12, 30

debugging with, 375379

opening programs, 34

running programs, 33

saving programs, 32

if statement, 5960

ImportError, 55

import statement, 55, 56

IndexError, 25

indexing, 2427

infinite loop, 195

in operator, 6162

in place modification, of lists, 122

input() function, 35, 50

insert() list method, 367

installing Python, xxv–xxvi

integer division operator (//), 181

integer (int) data type, 13

interactive shell, xxvii, 12

intersected mapping, 225, 234235

int() function, 154

islower() string method, 216217

isupper() string method, 216217

itertools module, 307308

J

join() string method, 9394, 253254

K

Kasiski examination, 282284

Kasiski, Friedrich, 282

Kerckhoffs, Auguste, 70

Kerckhoffs’s principle, 70

key, encryption, 4

key keyword argument, for sort(), 273

L

len() function, 4142

using with dictionaries, 148

using with lists, 89

less than operator (<), 4344

less than or equal to operator (<=), 4344

list-append-join process, 253254

list() function, 123, 213, 253254, 274275

lists, 8687

append() method, 121, 155156, 253254

as arguments, 121122, 301

concatenating, 8990

dictionaries, differences from, 147, 149

duplicating, 122

extend() method, 301

in operator, 89

insert() method, 367

len() method, 89

in place modification, 122

references, 119121

replicating, 8990

sort() method, 212213, 271275

local scope, 84

loops

for, 5859

with the range() function, 7273

while, 4243

Lovelace, Ada, 163

lower() string method, 134

M

machine-in-the-middle (MITM) attack, 339

'__main__' string value, 95

main() function

in affineCipher.py, 188189, 196

in affineHacker.py, 200, 204

in makePublicPrivateKeys.py, 343

and __name__ variable, 95

in publicKeyCipher.py, 373

in simpleSubCipher.py, 211, 219

in simpleSubHacker.py, 231, 245

in transpositionDecrypt.py, 102103, 110

in transpositionEncrypt.py, 85, 95

in transpositionFileCipher.py, 132133, 138

in transpositionHacker.py, 164165, 169

in transpositionTest.py, 118, 124

in vigenereCipher.py, 252253, 257

in vigenereHacker.py, 294, 313

makePublicPrivateKeys.py, 340341

makeWordPatterns.py, 225

man-in-the-middle (MITM) attack, 339

mappings. See cipherletter mappings

math.ceil() function, 103104

math.floor() function, 103104

math operators

+ (addition operator), 12

/ (division operator), 12

** (exponent operator), 201

% (modulo operator), 173

* (multiplication operator), 12

- (subtraction operator), 12

math.sqrt() function, 327, 356

max() function, 364

methods

defined, 62

passing as values, 272273

min() function, 364

MITM (man-in-the-middle or machine-in-the-middle) attack, 339

modular arithmetic, 172173

modular inverse, 180181

in affine cipher, 179180, 181

in public key cipher, 344

ModuleNotFoundError, 226

modules

calling functions from, 65

importing, 56

modulo operator (%), 173

Morse code, 3

Morse, Samuel, 3

multiline strings, 164165

multiple assignment, 175176

multiplication operator (*), 12

multiplicative cipher, 177181

N

__name__ variable, 9596

NameError, 33

negative indexes, 2526

newline

escape character (\n), 2829

removing with the strip() method, 167

not equal to (!=) operator, 4345

not in operator, 6162

not operator, 107

O

one-time pad cipher, 316

open() function, 131

opening programs, 34

operators, 12

assignment, 16

augmented assignment, 9192

comparison, 4345

in, 6162

math, 12

not in, 6162

order of operations, 14

or operator, 107, 108

os.path.exists() function, 133134

P

parameters. See also arguments

defining default arguments, 157

defining functions with, 8384

optional, 131

passing references as, 121, 234

scope of, 84

parentheses (()), 14, 28, 35

passingReference.py, 121

path (of files), 131

pattern object, regular expression, 231

permutation, 78

PKI (public key infrastructure), 338

plaintext, 4

plain text files, 128, 130134

potential decryption letters, 223

pow() function, 353354

precedence, 14

primality test, 322

prime factorization, 323

prime numbers, 322323

finding

with sieve of Eratosthenes, 328330

with trial division algorithm, 326327

use in public key cryptography, 340, 356357

primeNum.py, 324326

print() function, 2728, 3435, 306

product() function (itertools module), 307308

pseudorandom numbers, 116117

public key cipher

decrypting with, 368

encrypting with, 367368

hacking, why we can’t, 355357

publicKeyCipher.py, 357360

public key cryptography, 336

example key files, 342

formula to generate keys, 340

public key infrastructure (PKI), 338

pyperclip.copy() function, 56

pyperclip.py, xxvi

Q

quotes

enclosing strings

multiline, 164165

single line, 2930

escaping in strings, 2829

R

Rabin-Miller primality test, 331333

random numbers, 116117

random.randint() function, 116117

random.seed() function, 116117

random.shuffle() function, 122

range() function, 7273

read() file object method, 132

references, to lists, 119121

regex objects, 231

regular expressions

calling sub() method on, 241

finding characters with, 230231

repeat keyword argument, for itertools.product(), 307

replace() string method, 243

replication, 24, 118

replication operator (*), 24, 8990

return statement, 9495

return value, 35, 9495

reverse cipher, 39

reverseCipher.py, 40

reverse keyword argument, for sort(), 273, 275

Rivest, Ron, 337

round() function, 103104

RSA cipher, 337

running programs, 33

Russell, Bertrand, 209

S

saving programs, 32

Schneier, Bruce, 337

scope, 84

secrets module, 318

seed() function (random module), 116117

set() function, 298

Shamir, Adi, 337

Shannon, Claude, 70

Shannon’s Maxim, 70, 190

shuffle() function (random module), 122

sieve of Eratosthenes, 328331

signatures, digital, 338

silent mode, 306

simpleSubCipher.py, 209210

simpleSubHacker.py, 226229

simple substitution cipher, 208

decrypting, 216

encrypting, 215

hacking, 222225, 241245

single quote ('), 22, 2830

slicing, 2627

sort() list method, 212213, 271275

source code, 31

spaces, removing with the strip() method, 167

split() string method, 150151

sqrt() function (math module), 327, 356

square brackets ([]), 26

square root, 327

startswith() string method, 135

statements, 16

assignment, 16

break, 311312

continue, 202204

def, 8284

elif, 61

else, 60

for, 5859

if, 5960

import, 55, 56

return, 9495

while, 4243

str() function, 154

strings, 2227

building with list-append-join process, 253254

concatenation, 23

endswith() method, 135

escape characters, 2830

find() method, 6263

formatting (%s), 75

indexing, 2427

interpolation, 75

islower() method, 216217

isupper() method, 216217

join() method, 9394, 253254

lower() method, 134

multiline, 164165

quotes, 2830

replace() method, 243

replication, 24, 118

slicing, 2627

split() method, 150151

startswith() method, 135

str() function, 154

strip() method, 167

title() method, 134135

upper() method, 134

stringTest.py, 254

strip() method, 167

sub() regular expression method, 241

subtraction operator (-), 12

symbol set, 57

locating symbols in, 63

wraparound in, 64, 177178, 193

SyntaxError, 15, 28, 29

sys.exit() function, 124

sys.path.append() function, 226

T

tab

escape character (\t), 2829

removing with the strip() method, 167

terminating programs, 3536

testing programs, 113114

textbook RSA, 337

time.time() function, 136137

title() string method, 134135

transposition cipher, 77, 7881

decrypting with, 100101

encrypting with, 7880

hacking, 166168

transpositionDecrypt.py, 101102

transpositionEncrypt.py, 8182

transpositionFileCipher.py, 130

transpositionHacker.py, 162163

transpositionTest.py, 114115

trial division algorithm, 326327, 328

triple quotes (''' or """), 164165

True (Boolean value), 43

tuples, 190, 268

Turing, Alan, 188, 268, 352

two-time pad cipher, 319320

U

upper() string method, 134

V

Vail, Alfred, 3

values, 12

variables, 1518

names, 18

overwriting, 17

Vigenère, Blaise de, 247

Vigenère cipher, 248251, 316, 319320

decrypting with, 248249

encrypting with, 248249

hacking, 280282

vigenereCipher.py, 251252

vigenereDictionaryHacker.py, 280281

vigenereHacker.py, 251252

W

while statements, 4243

whitespace characters, 167

word patterns, 222223

wordPatterns.py, 225

wraparound

in modular arithmetic, 172173

of symbol sets, 64, 177178, 193

wrapper functions, 213214

Wright, Ernest Vincent, 266

write() file object method, 131