+ (addition and concatenation operator), 12, 23–24, 89–90
+= (augmented addition assignment operator), 91–92
/= (augmented division assignment operator), 92
*= (augmented multiplication assignment operator), 92
-= (augmented subtraction assignment operator), 92
: (colon), 26
/ (division operator), 12
__ (double underscore), 95
== (equal to comparison operator), 43–45
** (exponent operator), 201
> (greater than comparison operator), 43–44
>= (greater than or equal to comparison operator), 43–44
# (hash mark), 34
// (integer division operator), 181
< (less than comparison operator), 43–44
<= (less than or equal to comparison operator), 43–44
% (modulo operator), 173
* (multiplication and replication operator), 12, 24, 89–90
\n (newline escape character), 28–29
!= (not equal to comparison operator), 43–45
[] (square brackets), 26
%s (string formatting), 75
- (subtraction operator), 12
\t (tab escape character), 28–29
''' or """ (triple quotes), 164–165
absolute path, 131
addition operator (+), 12
Adleman, Leonard, 337
decrypting with, 179–180, 194–195
affineKeyTest.py, 192
al_sweigart_privkey.txt, 342, 343
al_sweigart_pubkey.txt, 342, 343, 346, 363
append() list method, 121, 155–156, 253–254
arguments, 28
default, 157
defining functions with parameters, 83–84
for file permissions, 131, 132
keyword
end, for print(), 306
key, for sort(), 273
repeat, for itertools.product(), 307
optional, 131
passing references as, 121, 234
assignment statements, 16
augmented assignment operators, 91–92
Bellaso, Giovan Battista, 247
Bernstein, Daniel J., xxi–xxii
blocks
in cryptography, 350
converting block to string, 354–355
converting string to block, 350–352
Boolean values, 43
breakpoints, for debugging, 379–380
brute-force attack, 69
built-in functions, 56
Caesar cipher, 4
decrypting with, 7
encrypting with, 7
hacking, 69
candidates (possible cipherwords), 223
case (of letters)
converting with string methods, 134, 216–217
sensitivity, in Python, 18
ceil() function (math module), 103–104
chiffre indéchiffrable, 247
cipher disk, 4
cipherletters, 222
cipherletter mappings, 224, 232–241
ciphertext, 4
cipher wheel, 4
cipherwords, 222
close() file object method, 132
code breaker, 2
colon (:), 26
comments, 34
composite numbers, 323, 326–327
concatenation operator (+), 23–24, 89–90
confidentiality, 338
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
Vigenère, Blaise de, 247
cryptography, 2
cryptomath.py, 182
data types, 13
Boolean, 43
floating-point, 13
integer, 13
string, 22
tuple, 190
debugging
decoding, 3
decrypting, 2
with the affine cipher, 179–180, 194–195
with the Caesar cipher, 7
with the public key cipher, 368
with the reverse cipher, 39
with the simple substitution cipher, 216
with the transposition cipher, 100–101
with the Vigenère cipher, 248–249
deepcopy() function (copy module), 122
adding or changing items in, 147
in operator and, 148
len() function and, 148
lists, differences from, 147
dictionary attacks, 250–251, 280
dictionary.txt, 280
diff tool, checking source code with, 31–32
digital signatures, 338
division operator (/), 12
integer (//), 181
divisors. See factors
double encryption, 8
dunder (__, double underscore), 95
dunder name dunder (__name__) variable, 95–96
duplicating lists, 122
elif statement, 61
else statement, 60
encoding, 3
encrypting
with the affine cipher, 179, 193–194
with the Caesar cipher, 7
with the public key cipher, 367–368
with the reverse cipher, 39
with the simple substitution cipher, 215
with the tranposition cipher, 78–80
with the Vigenère cipher, 248–249
encryption key, 4
end keyword argument, for print(), 306
endswith() string method, 135
English, detecting programmatically, 141–143, 156–159
Eratosthenes, sieve of, 328–331
errors, 15. See also debugging
ImportError, 55
IndexError, 25
ModuleNotFoundError, 226
NameError, 33
ETAOIN, 260
extended, 181
execution, program
defined, 34
terminating with sys.exit(), 124
exists() function (os.path module), 133
exit() function (sys module), 124
exponent operator (**), 201
defined, 13
extend() list method, 301
factors
of composite numbers, 323
greatest common divisors, 173–175, 176–177
of spacings, in Kasiski examination, 283–284, 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, 130–134
float, 13
float() function, 154
floating-point numbers, 13
floor() function (math module), 103–104
frequency
defined, 260
of English letters, 261
frequency analysis, 259, 285–287
frequency match score, 262–264, 286
functions, 28
calling from modules, 65
Gadsby (Wright), 266
GCD (greatest common divisor), 173–175, 176–177. See also factors
generating keys
public key cipher, 340
simple substitution cipher, 218
global scope, 84
googol, 322
greater than operator (>), 43–44
greater than or equal to (>=) operator, 43–44
greatest common divisor (GCD), 173–175, 176–177. See also factors
hacker, 2
hacking
Caesar cipher, 69
public key cipher, why we can’t, 355–357
simple substitution cipher, 222–225, 241–245
hash mark (#), 34
hello.py, 31
hybrid cryptosystems, 347
opening programs, 34
running programs, 33
saving programs, 32
ImportError, 55
IndexError, 25
infinite loop, 195
in place modification, of lists, 122
insert() list method, 367
installing Python, xxv–xxvi
integer division operator (//), 181
integer (int) data type, 13
interactive shell, xxvii, 12
intersected mapping, 225, 234–235
int() function, 154
islower() string method, 216–217
isupper() string method, 216–217
join() string method, 93–94, 253–254
Kasiski, Friedrich, 282
Kerckhoffs, Auguste, 70
Kerckhoffs’s principle, 70
key, encryption, 4
key keyword argument, for sort(), 273
using with dictionaries, 148
using with lists, 89
less than or equal to operator (<=), 43–44
list-append-join process, 253–254
list() function, 123, 213, 253–254, 274–275
append() method, 121, 155–156, 253–254
dictionaries, differences from, 147, 149
duplicating, 122
extend() method, 301
in operator, 89
insert() method, 367
len() method, 89
in place modification, 122
sort() method, 212–213, 271–275
local scope, 84
loops
with the range() function, 72–73
Lovelace, Ada, 163
lower() string method, 134
machine-in-the-middle (MITM) attack, 339
'__main__' string value, 95
main() function
in affineCipher.py, 188–189, 196
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, 102–103, 110
in transpositionEncrypt.py, 85, 95
in transpositionFileCipher.py, 132–133, 138
in transpositionHacker.py, 164–165, 169
in transpositionTest.py, 118, 124
in vigenereCipher.py, 252–253, 257
in vigenereHacker.py, 294, 313
makePublicPrivateKeys.py, 340–341
makeWordPatterns.py, 225
man-in-the-middle (MITM) attack, 339
mappings. See cipherletter mappings
math.floor() function, 103–104
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
min() function, 364
MITM (man-in-the-middle or machine-in-the-middle) attack, 339
in affine cipher, 179–180, 181
in public key cipher, 344
ModuleNotFoundError, 226
modules
calling functions from, 65
importing, 56
modulo operator (%), 173
Morse code, 3
Morse, Samuel, 3
multiplication operator (*), 12
multiplicative cipher, 177–181
NameError, 33
newline
removing with the strip() method, 167
not equal to (!=) operator, 43–45
not operator, 107
one-time pad cipher, 316
open() function, 131
opening programs, 34
operators, 12
assignment, 16
math, 12
order of operations, 14
os.path.exists() function, 133–134
parameters. See also arguments
defining default arguments, 157
defining functions with, 83–84
optional, 131
passing references as, 121, 234
scope of, 84
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, 130–134
potential decryption letters, 223
precedence, 14
primality test, 322
prime factorization, 323
finding
with sieve of Eratosthenes, 328–330
with trial division algorithm, 326–327
use in public key cryptography, 340, 356–357
print() function, 27–28, 34–35, 306
product() function (itertools module), 307–308
public key cipher
decrypting with, 368
hacking, why we can’t, 355–357
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
quotes
enclosing strings
Rabin-Miller primality test, 331–333
random.randint() function, 116–117
random.seed() function, 116–117
random.shuffle() function, 122
read() file object method, 132
regex objects, 231
regular expressions
calling sub() method on, 241
finding characters with, 230–231
repeat keyword argument, for itertools.product(), 307
replace() string method, 243
replication operator (*), 24, 89–90
reverse cipher, 39
reverseCipher.py, 40
reverse keyword argument, for sort(), 273, 275
Rivest, Ron, 337
RSA cipher, 337
running programs, 33
Russell, Bertrand, 209
saving programs, 32
Schneier, Bruce, 337
scope, 84
secrets module, 318
seed() function (random module), 116–117
set() function, 298
Shamir, Adi, 337
Shannon, Claude, 70
shuffle() function (random module), 122
sieve of Eratosthenes, 328–331
signatures, digital, 338
silent mode, 306
simple substitution cipher, 208
decrypting, 216
encrypting, 215
sort() list method, 212–213, 271–275
source code, 31
spaces, removing with the strip() method, 167
split() string method, 150–151
sqrt() function (math module), 327, 356
square brackets ([]), 26
square root, 327
startswith() string method, 135
statements, 16
assignment, 16
elif, 61
else, 60
str() function, 154
building with list-append-join process, 253–254
concatenation, 23
endswith() method, 135
formatting (%s), 75
interpolation, 75
lower() method, 134
replace() method, 243
startswith() method, 135
str() function, 154
strip() method, 167
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, 177–178, 193
sys.exit() function, 124
sys.path.append() function, 226
tab
removing with the strip() method, 167
textbook RSA, 337
title() string method, 134–135
transposition cipher, 77, 78–81
transpositionDecrypt.py, 101–102
transpositionEncrypt.py, 81–82
transpositionFileCipher.py, 130
transpositionHacker.py, 162–163
trial division algorithm, 326–327, 328
triple quotes (''' or """), 164–165
True (Boolean value), 43
upper() string method, 134
Vail, Alfred, 3
values, 12
names, 18
overwriting, 17
Vigenère, Blaise de, 247
Vigenère cipher, 248–251, 316, 319–320
vigenereDictionaryHacker.py, 280–281
whitespace characters, 167
wordPatterns.py, 225
wraparound
in modular arithmetic, 172–173
of symbol sets, 64, 177–178, 193
Wright, Ernest Vincent, 266
write() file object method, 131