Skip to content

Commit 70095a7

Browse files
committed
2023-04-10 15:21:32
1 parent 5685434 commit 70095a7

File tree

20 files changed

+1966
-1966
lines changed

20 files changed

+1966
-1966
lines changed

docs/cracking/05.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ translated = ''
4444

4545
i = len(message) - 1
4646
while i >= 0:
47-
 9.     translated = translated + message[i]
48-
10    i = i - 1
47+
    translated = translated + message[i]
48+
    i = i - 1
4949

5050
print(translated)
5151
```
@@ -300,8 +300,8 @@ True
300300

301301
```py
302302
while i >= 0:
303-
 9.     translated = translated + message[i]
304-
10    i = i - 1
303+
    translated = translated + message[i]
304+
    i = i - 1
305305

306306
print(translated)
307307
```
@@ -317,8 +317,8 @@ print(translated)
317317
```py
318318
i = len(message) - 1
319319
while i >= 0:
320-
 9.     translated = translated + message[i]
321-
10    i = i – 1
320+
    translated = translated + message[i]
321+
    i = i – 1
322322

323323
print(translated)
324324
```
@@ -343,10 +343,10 @@ print(translated)
343343

344344
```py
345345
while i >= 0:
346-
 9.     translated = translated + message[i]
347-
10    print('i is', i, ', message[i] is', message[i], ', translated is',
346+
    translated = translated + message[i]
347+
    print('i is', i, ', message[i] is', message[i], ', translated is',
348348
          translated)
349-
11    i = i - 1
349+
    i = i - 1
350350

351351
print(translated)
352352
```

docs/cracking/06.md

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -55,27 +55,27 @@ SYMBOLS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz12345
5555
translated = ''
5656

5757
for symbol in message:
58-
22    # Note: Only symbols in the SYMBOLS string can be
58+
    # Note: Only symbols in the SYMBOLS string can be
5959
          encrypted/decrypted.
60-
23    if symbol in SYMBOLS:
61-
24        symbolIndex = SYMBOLS.find(symbol)
62-
63-
26        # Perform encryption/decryption:
64-
27        if mode == 'encrypt':
65-
28            translatedIndex = symbolIndex + key
66-
29        elif mode == 'decrypt':
67-
30            translatedIndex = symbolIndex - key
68-
69-
32        # Handle wraparound, if needed:
70-
33        if translatedIndex >= len(SYMBOLS):
71-
34            translatedIndex = translatedIndex - len(SYMBOLS)
72-
35        elif translatedIndex < 0:
73-
36            translatedIndex = translatedIndex + len(SYMBOLS)
74-
75-
38        translated = translated + SYMBOLS[translatedIndex]
76-
39    else:
77-
40        # Append the symbol without encrypting/decrypting:
78-
41        translated = translated + symbol
60+
    if symbol in SYMBOLS:
61+
        symbolIndex = SYMBOLS.find(symbol)
62+
63+
        # Perform encryption/decryption:
64+
        if mode == 'encrypt':
65+
            translatedIndex = symbolIndex + key
66+
        elif mode == 'decrypt':
67+
            translatedIndex = symbolIndex - key
68+
69+
        # Handle wraparound, if needed:
70+
        if translatedIndex >= len(SYMBOLS):
71+
            translatedIndex = translatedIndex - len(SYMBOLS)
72+
        elif translatedIndex < 0:
73+
            translatedIndex = translatedIndex + len(SYMBOLS)
74+
75+
        translated = translated + SYMBOLS[translatedIndex]
76+
    else:
77+
        # Append the symbol without encrypting/decrypting:
78+
        translated = translated + symbol
7979

8080
# Output the translated string:
8181
print(translated)
@@ -237,7 +237,7 @@ The letter is y
237237
凯撒密码中的第 23 行有另一种 Python 指令——`if`语句:
238238

239239
```py
240-
23    if symbol in SYMBOLS:
240+
    if symbol in SYMBOLS:
241241
```
242242

243243
你可以把一个`if`语句理解为,“如果这个条件是`True`,执行下面块中的代码。否则,如果是`False`,跳过这个代码块。”一个`if`语句的格式是使用关键字`if`后跟一个条件,再跟一个冒号(`:`)。与循环一样,要执行的代码缩进在一个块中。
@@ -307,7 +307,7 @@ The letter is y
307307
`caesarCipher.py` 中的第 23 行也使用了`in`操作符:
308308

309309
```py
310-
23    if symbol in SYMBOLS:
310+
    if symbol in SYMBOLS:
311311
```
312312

313313
一个`in`操作符可以连接两个字符串,如果第一个字符串在第二个字符串内,它将计算为`True`,否则计算为`False``in` 操作符也可以与`not`配对,后者的作用正好相反。在交互式 shell 中输入以下内容:
@@ -336,7 +336,7 @@ The letter is y
336336
第 24 行找到了`SYMBOLS`字符串中的索引,其中`symbol`是:
337337

338338
```py
339-
24        symbolIndex = SYMBOLS.find(symbol)
339+
        symbolIndex = SYMBOLS.find(symbol)
340340
```
341341

342342
这段代码包含一个方法调用。*方法*就像函数一样,只不过它们附加了一个带句点的值(或者在第 24 行,一个包含值的变量)。这个方法的名字是`find()`,它被存储在`SYMBOLS`字符串值中用于调用。
@@ -386,8 +386,8 @@ The letter is y
386386
密码程序只能加密或解密符号集中的符号:
387387

388388
```py
389-
23    if symbol in SYMBOLS:
390-
24        symbolIndex = SYMBOLS.find(symbol)
389+
    if symbol in SYMBOLS:
390+
        symbolIndex = SYMBOLS.find(symbol)
391391
```
392392

393393
所以在运行第 24 行的代码之前,程序必须弄清楚`symbol`是否在符号集中。然后可以在`SYMBOLS`中找到`symbol`所在的索引。`find()`调用返回的索引存储在`symbolIndex`中。
@@ -397,11 +397,11 @@ The letter is y
397397
caesarCipher.py
398398

399399
```py
400-
26        # Perform encryption/decryption:
401-
27        if mode == 'encrypt':
402-
28            translatedIndex = symbolIndex + key
403-
29        elif mode == 'decrypt':
404-
30            translatedIndex = symbolIndex - key
400+
        # Perform encryption/decryption:
401+
        if mode == 'encrypt':
402+
            translatedIndex = symbolIndex + key
403+
        elif mode == 'decrypt':
404+
            translatedIndex = symbolIndex - key
405405
```
406406

407407
`mode`变量包含一个字符串,告诉程序应该加密还是解密。如果这个字符串是`'encrypt'`,那么第 27 行的`if`语句的条件将是`True`,执行第 28 行将`key`加上`symbolIndex`(跳过`elif`语句后的块)。否则,如果`mode``'decrypt'`,则执行第 30 行减去`key`
@@ -411,11 +411,11 @@ caesarCipher.py
411411
当我们在第一章中用纸和笔实现凯撒密码时,有时增加或减少密钥会导致一个大于或等于符号集大小或小于零的数。在这些情况下,我们必须增加或减少符号集的长度,以便它能够“回绕”,或者返回到符号集的开头或结尾。我们可以使用代码`len(SYMBOLS)`来做这件事,它返回`66`,即`SYMBOLS`字符串的长度。第 33 到 36 行在密码程序中处理这种回绕。
412412

413413
```py
414-
32        # Handle wraparound, if needed:
415-
33        if translatedIndex >= len(SYMBOLS):
416-
34            translatedIndex = translatedIndex - len(SYMBOLS)
417-
35        elif translatedIndex < 0:
418-
36            translatedIndex = translatedIndex + len(SYMBOLS)
414+
        # Handle wraparound, if needed:
415+
        if translatedIndex >= len(SYMBOLS):
416+
            translatedIndex = translatedIndex - len(SYMBOLS)
417+
        elif translatedIndex < 0:
418+
            translatedIndex = translatedIndex + len(SYMBOLS)
419419
```
420420

421421
如果`translatedIndex`大于等于`66`,则第 33 行的条件为`True`,执行第 34 行(跳过第 35 行的`elif`语句)。从`translatedIndex`中减去`SYMBOLS`的长度将变量的索引指向`SYMBOLS`字符串的开头。否则 Python 会检查`translatedIndex`是否小于`0`。如果条件是`True`,则执行第 36 行,并且`translatedIndex`绕到`SYMBOLS`字符串的末尾。
@@ -425,7 +425,7 @@ caesarCipher.py
425425
现在您已经在`translatedIndex`中有了translated变量中符号集的索引,`SYMBOLS[translatedIndex]`将对translated变量中符号集求值。第 38 行使用字符串连接将这个加密/解密的符号添加到`translated`字符串的末尾:
426426

427427
```py
428-
38        translated = translated + SYMBOLS[translatedIndex]
428+
        translated = translated + SYMBOLS[translatedIndex]
429429
```
430430

431431
最终,`translated`字符串将是整个编码或解码的消息。
@@ -435,9 +435,9 @@ caesarCipher.py
435435
`message`字符串可能包含不在`SYMBOLS`字符串中的字符。这些字符在密码程序的符号集之外,无法加密或解密。相反,它们将被直接追加到`translated`字符串中,这发生在第 39 到 41 行:
436436

437437
```py
438-
39    else:
439-
40        # Append the symbol without encrypting/decrypting:
440-
41        translated = translated + symbol
438+
    else:
439+
        # Append the symbol without encrypting/decrypting:
440+
        translated = translated + symbol
441441
```
442442

443443
第 39 行的`else`语句有四个缩进空间。如果您查看上面行的缩进,您会看到它与第 23 行的`if`语句成对出现。尽管在这个`if``else`语句之间有很多代码,但它们都属于同一个代码块。

docs/cracking/07.md

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -39,31 +39,31 @@ SYMBOLS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz12345
3939

4040
# Loop through every possible key:
4141
for key in range(len(SYMBOLS)):
42-
 9.     # It is important to set translated to the blank string so that the
43-
10    # previous iteration's value for translated is cleared:
44-
11    translated = ''
42+
    # It is important to set translated to the blank string so that the
43+
    # previous iteration's value for translated is cleared:
44+
    translated = ''
4545

46-
13    # The rest of the program is almost the same as the Caesar program:
46+
    # The rest of the program is almost the same as the Caesar program:
4747

48-
15    # Loop through each symbol in message:
49-
16    for symbol in message:
50-
17        if symbol in SYMBOLS:
51-
18            symbolIndex = SYMBOLS.find(symbol)
52-
19            translatedIndex = symbolIndex - key
48+
    # Loop through each symbol in message:
49+
    for symbol in message:
50+
        if symbol in SYMBOLS:
51+
            symbolIndex = SYMBOLS.find(symbol)
52+
            translatedIndex = symbolIndex - key
5353

54-
21            # Handle the wraparound:
55-
22            if translatedIndex < 0:
56-
23                translatedIndex = translatedIndex + len(SYMBOLS)
54+
            # Handle the wraparound:
55+
            if translatedIndex < 0:
56+
                translatedIndex = translatedIndex + len(SYMBOLS)
5757

58-
25            # Append the decrypted symbol:
59-
26            translated = translated + SYMBOLS[translatedIndex]
58+
            # Append the decrypted symbol:
59+
            translated = translated + SYMBOLS[translatedIndex]
6060

61-
28        else:
62-
29            # Append the symbol without encrypting/decrypting:
63-
30            translated = translated + symbol
61+
        else:
62+
            # Append the symbol without encrypting/decrypting:
63+
            translated = translated + symbol
6464

65-
32    # Display every possible decryption:
66-
33    print('Key #%s: %s' % (key, translated))
65+
    # Display every possible decryption:
66+
    print('Key #%s: %s' % (key, translated))
6767
```
6868

6969
请注意,这段代码的大部分与最初的 Caesar 密码程序中的代码相同。这是因为凯撒密码破解程序使用相同的步骤来解密消息。
@@ -170,47 +170,47 @@ Hello
170170
```py
171171
# Loop through every possible key:
172172
for key in range(len(SYMBOLS)):
173-
 9.     # It is important to set translated to the blank string so that the
174-
10    # previous iteration's value for translated is cleared:
175-
11    translated = ''
173+
    # It is important to set translated to the blank string so that the
174+
    # previous iteration's value for translated is cleared:
175+
    translated = ''
176176
```
177177

178178
在这个`for`循环的开始,我们将`translated`重置为空字符串,这一点很重要;否则,用当前密钥解密的文本将被添加到循环中最后一次迭代的`translated`解密文本中。
179179

180180
第 16 行到第 30 行几乎与第 5 章中的凯撒密码程序中的代码相同,但是稍微简单一些,因为这段代码只需要解密:
181181

182182
```py
183-
13    # The rest of the program is almost the same as the Caesar program:
183+
    # The rest of the program is almost the same as the Caesar program:
184184

185-
15    # Loop through each symbol in message:
186-
16    for symbol in message:
187-
17        if symbol in SYMBOLS:
188-
18            symbolIndex = SYMBOLS.find(symbol)
185+
    # Loop through each symbol in message:
186+
    for symbol in message:
187+
        if symbol in SYMBOLS:
188+
            symbolIndex = SYMBOLS.find(symbol)
189189
```
190190

191191
在第 16 行,我们遍历存储在`message`中的密文字符串中的每个符号。在这个循环的每次迭代中,第 17 行检查`symbol`是否存在于`SYMBOLS`常量变量中,如果存在,就解密它。第 18 行的`find()`方法调用定位`SYMBOLS``symbol`所在的索引,并将其存储在一个名为`symbolIndex`的变量中。
192192

193193
然后我们从第 19 行的`symbolIndex`中减去`key`来解密:
194194

195195
```py
196-
19            translatedIndex = symbolIndex - key
196+
            translatedIndex = symbolIndex - key
197197

198-
21            # Handle the wraparound:
199-
22            if translatedIndex < 0:
200-
23                translatedIndex = translatedIndex + len(SYMBOLS)
198+
            # Handle the wraparound:
199+
            if translatedIndex < 0:
200+
                translatedIndex = translatedIndex + len(SYMBOLS)
201201
```
202202

203203
这个减法操作可能会导致`translatedIndex`变得小于零,并且当我们在`SYMBOLS`中找到要解密的字符的位置时,需要我们“环绕”常量`SYMBOLS`。第 22 行检查这种情况,如果`translatedIndex`小于`0`,第 23 行增加`66`(这是`len(SYMBOLS)`返回的值)。
204204

205205
现在`translatedIndex`已经被修改,`SYMBOLS[translatedIndex]`将计算出解密后的符号。第 26 行将这个符号添加到存储在`translated`中的字符串的末尾:
206206

207207
```py
208-
25            # Append the decrypted symbol:
209-
26            translated = translated + SYMBOLS[translatedIndex]
208+
            # Append the decrypted symbol:
209+
            translated = translated + SYMBOLS[translatedIndex]
210210

211-
28        else:
212-
29            # Append the symbol without encrypting/decrypting:
213-
30            translated = translated + symbol
211+
        else:
212+
            # Append the symbol without encrypting/decrypting:
213+
            translated = translated + symbol
214214
```
215215

216216
如果在`SYMBOL`集合中没有找到值,第 30 行只是将未修改的`symbol`添加到`translated`的末尾。
@@ -220,8 +220,8 @@ for key in range(len(SYMBOLS)):
220220
尽管第 33 行是我们的 Caesar cipher hacker 程序中唯一的`print()`函数调用,但它将执行多次,因为它在第 8 行的`for`循环的每次迭代中被调用一次:
221221

222222
```py
223-
32    # Display every possible decryption:
224-
33    print('Key #%s: %s' % (key, translated))
223+
    # Display every possible decryption:
224+
    print('Key #%s: %s' % (key, translated))
225225
```
226226

227227
`print()`函数调用的参数是一个使用*字符串格式*(也称为*字符串插值*)的字符串值。使用`%s`文本的字符串格式将一个字符串放在另一个字符串中。字符串中的第一个`%s`被字符串末尾括号中的第一个值替换。

0 commit comments

Comments
 (0)