Skip to content

Commit c45d426

Browse files
committed
2023-04-10 15:17:03
1 parent 2895679 commit c45d426

File tree

7 files changed

+86
-86
lines changed

7 files changed

+86
-86
lines changed

docs/cracking/07.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@
3030
`caesarHacker.py`
3131

3232
```py
33-
 1\. # Caesar Cipher Hacker
34-
 2\. # https://www.nostarch.com/crackingcodes/ (BSD Licensed)
33+
# Caesar Cipher Hacker
34+
# https://www.nostarch.com/crackingcodes/ (BSD Licensed)
3535
 3.
36-
 4\. message = 'guv6Jv6Jz!J6rp5r7Jzr66ntrM'
37-
 5\. SYMBOLS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz12345
36+
message = 'guv6Jv6Jz!J6rp5r7Jzr66ntrM'
37+
SYMBOLS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz12345
3838
      67890 !?.'
3939
 6.
40-
 7\. # Loop through every possible key:
41-
 8\. for key in range(len(SYMBOLS)):
40+
# Loop through every possible key:
41+
for key in range(len(SYMBOLS)):
4242
 9.     # It is important to set translated to the blank string so that the
4343
10.     # previous iteration's value for translated is cleared:
4444
11.     translated = ''
@@ -99,11 +99,11 @@ Key #65: hvw7Kw7K1?K7sq6s8K1s77ousN
9999
破解程序将创建一个`message`变量,存储程序试图解密的密文字符串。`SYMBOLS`常量包含密码可以加密的每个字符:
100100

101101
```py
102-
 1\. # Caesar Cipher Hacker
103-
 2\. # https://www.nostarch.com/crackingcodes/ (BSD Licensed)
102+
# Caesar Cipher Hacker
103+
# https://www.nostarch.com/crackingcodes/ (BSD Licensed)
104104
 3.
105-
 4\. message = 'guv6Jv6Jz!J6rp5r7Jzr66ntrM'
106-
 5\. SYMBOLS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz12345
105+
message = 'guv6Jv6Jz!J6rp5r7Jzr66ntrM'
106+
SYMBOLS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz12345
107107
      67890 !?.'
108108
```
109109

@@ -114,8 +114,8 @@ Key #65: hvw7Kw7K1?K7sq6s8K1s77ousN
114114
第 8 行是一个`for`循环,它不遍历字符串值,而是遍历对`range()`函数调用的返回值:
115115

116116
```py
117-
 7\. # Loop through every possible key:
118-
 8\. for key in range(len(SYMBOLS)):
117+
# Loop through every possible key:
118+
for key in range(len(SYMBOLS)):
119119
```
120120

121121
`range()`函数接受一个整数参数并返回一个数据类型为`range`的值。范围值可以用在`for`循环中,根据你给函数的整数循环特定的次数。让我们试一个例子。在交互式 shell 中输入以下内容:
@@ -168,8 +168,8 @@ Hello
168168
接下来几行中的解密代码将解密后的文本添加到`translated`中的字符串末尾。在第 11 行,`translated`被设置为空字符串:
169169

170170
```py
171-
 7\. # Loop through every possible key:
172-
 8\. for key in range(len(SYMBOLS)):
171+
# Loop through every possible key:
172+
for key in range(len(SYMBOLS)):
173173
 9.     # It is important to set translated to the blank string so that the
174174
10.     # previous iteration's value for translated is cleared:
175175
11.     translated = ''

docs/cracking/08.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,12 @@
109109
Encrypt.py*
110110

111111
```py
112-
 1\. # Transposition Cipher Encryption
113-
 2\. # https://www.nostarch.com/crackingcodes/ (BSD Licensed)
112+
# Transposition Cipher Encryption
113+
# https://www.nostarch.com/crackingcodes/ (BSD Licensed)
114114
 3.
115-
 4\. import pyperclip
115+
import pyperclip
116116
 5.
117-
 6\. def main():
117+
def main():
118118
 7.     myMessage = 'Common sense is not so common.'
119119
 8.     myKey = 8
120120
 9.
@@ -171,11 +171,11 @@ Cenoonommstmme oo snnio. s s c|
171171
导入`pyperclip`模块后,您将在第 6 行使用一个`def`语句创建一个定制函数`main()`
172172

173173
```py
174-
 1\. # Transposition Cipher Encryption
175-
 2\. # https://www.nostarch.com/crackingcodes/ (BSD Licensed)
174+
# Transposition Cipher Encryption
175+
# https://www.nostarch.com/crackingcodes/ (BSD Licensed)
176176
 3.
177-
 4\. import pyperclip
178-
 6\. def main():
177+
import pyperclip
178+
def main():
179179
 7.     myMessage = 'Common sense is not so common.'
180180
 8.     myKey = 8
181181
```
@@ -250,7 +250,7 @@ Hello
250250
*transpositonecrypt . py*中的第 6 到第 8 行,你可以看到我们已经定义了一个`main()`函数,它将在被调用时为变量`myMessage``myKey`设置值:
251251

252252
```py
253-
 6\. def main():
253+
def main():
254254
 7.     myMessage = 'Common sense is not so common.'
255255
 8.     myKey = 8
256256
```

docs/cracking/09.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@
5959
解密. py*
6060

6161
```py
62-
 1\. # Transposition Cipher Decryption
63-
 2\. # https://www.nostarch.com/crackingcodes/ (BSD Licensed)
62+
# Transposition Cipher Decryption
63+
# https://www.nostarch.com/crackingcodes/ (BSD Licensed)
6464
 3.
65-
 4\. import math, pyperclip
65+
import math, pyperclip
6666
 5.
67-
 6\. def main():
67+
def main():
6868
 7.     myMessage = 'Cenoonommstmme oo snnio. s s c'
6969
 8.     myKey = 8
7070
 9.
@@ -132,12 +132,12 @@ Common sense is not so common.|
132132
*transpositonecrypt . py*程序的第一部分与*transpositonecrypt . py*的第一部分类似:
133133

134134
```py
135-
 1\. # Transposition Cipher Decryption
136-
 2\. # https://www.nostarch.com/crackingcodes/ (BSD Licensed)
135+
# Transposition Cipher Decryption
136+
# https://www.nostarch.com/crackingcodes/ (BSD Licensed)
137137
 3.
138-
 4\. import math, pyperclip
138+
import math, pyperclip
139139
 5.
140-
 6\. def main():
140+
def main():
141141
 7.     myMessage = 'Cenoonommstmme oo snnio. s s c'
142142
 8.     myKey = 8
143143
 9.

docs/cracking/10.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@
3737
Test.py*
3838

3939
```py
40-
 1\. # Transposition Cipher Test
41-
 2\. # https://www.nostarch.com/crackingcodes/ (BSD Licensed)
40+
# Transposition Cipher Test
41+
# https://www.nostarch.com/crackingcodes/ (BSD Licensed)
4242
 3.
43-
 4\. import random, sys, transpositionEncrypt, transpositionDecrypt
43+
import random, sys, transpositionEncrypt, transpositionDecrypt
4444
 5.
45-
 6\. def main():
45+
def main():
4646
 7.     random.seed(42) # Set the random "seed" to a static value.
4747
 8.
4848
 9.     for i in range(20): # Run 20 tests.
@@ -108,10 +108,10 @@ Transposition cipher test passed.
108108
程序从导入模块开始,包括您已经看到的 Python 自带的两个模块,`random``sys`:
109109

110110
```py
111-
 1\. # Transposition Cipher Test
112-
 2\. # https://www.nostarch.com/crackingcodes/ (BSD Licensed)
111+
# Transposition Cipher Test
112+
# https://www.nostarch.com/crackingcodes/ (BSD Licensed)
113113
 3.
114-
 4\. import random, sys, transpositionEncrypt, transpositionDecrypt
114+
import random, sys, transpositionEncrypt, transpositionDecrypt
115115
```
116116

117117
我们还需要导入转置密码程序(即*transpositonecrypt . py**transpositonecrypt . py*),只需输入它们的名称,而不需要输入*。py* 分机。
@@ -171,7 +171,7 @@ Transposition cipher test passed.
171171
首先,让我们设置`main()`函数,它包含测试密码程序的代码。它首先为伪随机字符串设置一个种子:
172172

173173
```py
174-
 6\. def main():
174+
def main():
175175
 7.     random.seed(42) # Set the random "seed" to a static value.
176176
```
177177

docs/cracking/11.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@
4141
FileCipher.py*
4242

4343
```py
44-
 1\. # Transposition Cipher Encrypt/Decrypt File
45-
 2\. # https://www.nostarch.com/crackingcodes/ (BSD Licensed)
44+
# Transposition Cipher Encrypt/Decrypt File
45+
# https://www.nostarch.com/crackingcodes/ (BSD Licensed)
4646
 3.
47-
 4\. import time, os, sys, transpositionEncrypt, transpositionDecrypt
47+
import time, os, sys, transpositionEncrypt, transpositionDecrypt
4848
 5.
49-
 6\. def main():
49+
def main():
5050
 7.     inputFilename = 'frankenstein.txt'
5151
 8.     # BE CAREFUL! If a file with the outputFilename name already exists,
5252
 9.     # this program will overwrite that file:
@@ -212,12 +212,12 @@ Hello world!
212212
*transpositonfilecipher . py*程序的第一部分应该看起来很熟悉。第 4 行是程序 `transpositionEncrypt.py`*transpositionencrypt . py*以及 Python 的`time``os``sys`模块的`import`语句。然后我们开始`main()`,设置一些在程序中使用的变量。
213213

214214
```py
215-
 1\. # Transposition Cipher Encrypt/Decrypt File
216-
 2\. # https://www.nostarch.com/crackingcodes/ (BSD Licensed)
215+
# Transposition Cipher Encrypt/Decrypt File
216+
# https://www.nostarch.com/crackingcodes/ (BSD Licensed)
217217
 3.
218-
 4\. import time, os, sys, transpositionEncrypt, transpositionDecrypt
218+
import time, os, sys, transpositionEncrypt, transpositionDecrypt
219219
 5.
220-
 6\. def main():
220+
def main():
221221
 7.     inputFilename = 'frankenstein.txt'
222222
 8.     # BE CAREFUL! If a file with the outputFilename name already exists,
223223
 9.     # this program will overwrite that file:
@@ -441,7 +441,7 @@ if __name__ == '__main__':
441441

442442
练习题的答案可以在本书的网站[`www.nostarch.com/crackingcodes`](https://www.nostarch.com/crackingcodes/)找到。
443443

444-
1. `os.exists()``os.path.exists()`哪个正确?
444+
`os.exists()``os.path.exists()`哪个正确?
445445

446446
2. Unix 时代是什么时候?
447447

docs/cracking/12.md

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -79,30 +79,30 @@ ABANDONS
7979
*检测英语. py*
8080

8181
```py
82-
 1\. # Detect English module
83-
 2\. # https://www.nostarch.com/crackingcodes/ (BSD Licensed)
82+
# Detect English module
83+
# https://www.nostarch.com/crackingcodes/ (BSD Licensed)
8484
 3.
85-
 4\. # To use, type this code:
86-
 5\. #   import detectEnglish
87-
 6\. #   detectEnglish.isEnglish(someString) # Returns True or False
88-
 7\. # (There must be a "dictionary.txt" file in this directory with all
89-
 8\. # English words in it, one word per line. You can download this from
90-
 9\. # https://www.nostarch.com/crackingcodes/.)
91-
10\. UPPERLETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
92-
11\. LETTERS_AND_SPACE = UPPERLETTERS + UPPERLETTERS.lower() + ' \t\n'
85+
# To use, type this code:
86+
#   import detectEnglish
87+
#   detectEnglish.isEnglish(someString) # Returns True or False
88+
# (There must be a "dictionary.txt" file in this directory with all
89+
# English words in it, one word per line. You can download this from
90+
# https://www.nostarch.com/crackingcodes/.)
91+
UPPERLETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
92+
LETTERS_AND_SPACE = UPPERLETTERS + UPPERLETTERS.lower() + ' \t\n'
9393
12.
94-
13\. def loadDictionary():
94+
def loadDictionary():
9595
14.     dictionaryFile = open('dictionary.txt')
9696
15.     englishWords = {}
9797
16.     for word in dictionaryFile.read().split('\n'):
9898
17.         englishWords[word] = None
9999
18.     dictionaryFile.close()
100100
19.     return englishWords
101101
20.
102-
21\. ENGLISH_WORDS = loadDictionary()
102+
ENGLISH_WORDS = loadDictionary()
103103
22.
104104
23.
105-
24\. def getEnglishCount(message):
105+
def getEnglishCount(message):
106106
25.     message = message.upper()
107107
26.     message = removeNonLetters(message)
108108
27.     possibleWords = message.split()
@@ -117,15 +117,15 @@ ABANDONS
117117
36.     return float(matches) / len(possibleWords)
118118
37.
119119
38.
120-
39\. def removeNonLetters(message):
120+
def removeNonLetters(message):
121121
40.     lettersOnly = []
122122
41.     for symbol in message:
123123
42.         if symbol in LETTERS_AND_SPACE:
124124
43.             lettersOnly.append(symbol)
125125
44.     return ''.join(lettersOnly)
126126
45.
127127
46.
128-
47\. def isEnglish(message, wordPercentage=20, letterPercentage=85):
128+
def isEnglish(message, wordPercentage=20, letterPercentage=85):
129129
48.     # By default, 20% of the words must exist in the dictionary file, and
130130
49.     # 85% of all the characters in the message must be letters or spaces
131131
50.     # (not punctuation or numbers).
@@ -155,17 +155,17 @@ True
155155
让我们看一下 `detectEnglish.py` 程序的第一部分。前九行代码是注释,给出了如何使用这个模块的说明。
156156

157157
```py
158-
 1\. # Detect English module
159-
 2\. # https://www.nostarch.com/crackingcodes/ (BSD Licensed)
158+
# Detect English module
159+
# https://www.nostarch.com/crackingcodes/ (BSD Licensed)
160160
 3.
161-
 4\. # To use, type this code:
162-
 5\. #   import detectEnglish
163-
 6\. #   detectEnglish.isEnglish(someString) # Returns True or False
164-
 7\. # (There must be a "dictionary.txt" file in this directory with all
165-
 8\. # English words in it, one word per line. You can download this from
166-
 9\. # https://www.nostarch.com/crackingcodes/.)
167-
10\. UPPERLETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
168-
11\. LETTERS_AND_SPACE = UPPERLETTERS + UPPERLETTERS.lower() + ' \t\n'
161+
# To use, type this code:
162+
#   import detectEnglish
163+
#   detectEnglish.isEnglish(someString) # Returns True or False
164+
# (There must be a "dictionary.txt" file in this directory with all
165+
# English words in it, one word per line. You can download this from
166+
# https://www.nostarch.com/crackingcodes/.)
167+
UPPERLETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
168+
LETTERS_AND_SPACE = UPPERLETTERS + UPPERLETTERS.lower() + ' \t\n'
169169
```
170170

171171
前九行代码是注释,给出了如何使用这个模块的说明。他们提醒用户,除非名为 `dictionary.txt` 的文件与 `detectEnglish.py` 在同一个目录下,否则这个模块不会工作。
@@ -315,7 +315,7 @@ name Al
315315
现在让我们返回到 `detectEnglish.py` 并设置字典文件。字典文件位于用户的硬盘上,但是除非我们将该文件中的文本作为字符串值加载,否则我们的 Python 代码无法使用它。我们将创建一个`loadDictionary()`助手函数来完成这项工作:
316316

317317
```py
318-
13\. def loadDictionary():
318+
def loadDictionary():
319319
14.     dictionaryFile = open('dictionary.txt')
320320
15.     englishWords = {}
321321
```
@@ -375,7 +375,7 @@ name Al
375375
然后我们调用`loadDictionary()`并将它返回的字典值存储在一个名为`ENGLISH_WORDS`的变量中:
376376

377377
```py
378-
21\. ENGLISH_WORDS = loadDictionary()
378+
ENGLISH_WORDS = loadDictionary()
379379
```
380380

381381
我们想在`detectEnglish`模块中的其余代码之前调用`loadDictionary()`,但是 Python 必须在我们调用函数之前执行`loadDictionary()``def`语句。这就是为什么`ENGLISH_WORDS`的赋值在`loadDictionary()`函数代码之后的原因。
@@ -385,7 +385,7 @@ name Al
385385
程序代码的第 24 行到第 27 行定义了`getEnglishCount()`函数,该函数接受一个字符串参数并返回一个浮点值,该值指示识别的英语单词与总单词的比率。我们将把比率表示为`0.0``1.0`之间的一个值。值`0.0`意味着`message`中没有单词是英语单词,而`1.0`意味着`message`中的所有单词都是英语单词。最有可能的是,`getEnglishCount()`将返回一个介于`0.0``1.0`之间的浮点值。`isEnglish()`函数使用该返回值来确定是评估为`True`还是`False`
386386

387387
```py
388-
24\. def getEnglishCount(message):
388+
def getEnglishCount(message):
389389
25.     message = message.upper()
390390
26.     message = removeNonLetters(message)
391391
27.     possibleWords = message.split()
@@ -479,7 +479,7 @@ ZeroDivisionError: division by zero
479479
前面解释的`getEnglishCount()`函数调用字符串上的函数`removeNonLetters()`来删除其中的任何数字和标点符号。
480480

481481
```py
482-
39\. def removeNonLetters(message):
482+
def removeNonLetters(message):
483483
40.     lettersOnly = []
484484
41.     for symbol in message:
485485
42.         if symbol in LETTERS_AND_SPACE:
@@ -519,7 +519,7 @@ ZeroDivisionError: division by zero
519519
当用错误的密钥解密消息时,它通常会产生比典型的英语消息中多得多的非字母和非空格字符。此外,它产生的单词通常是随机的,在英语词典中是找不到的。`isEnglish()`函数可以在给定的字符串中检查这两个问题。
520520

521521
```py
522-
47\. def isEnglish(message, wordPercentage=20, letterPercentage=85):
522+
def isEnglish(message, wordPercentage=20, letterPercentage=85):
523523
48.     # By default, 20% of the words must exist in the dictionary file, and
524524
49.     # 85% of all the characters in the message must be letters or spaces
525525
50.     # (not punctuation or numbers).

docs/cracking/13.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ RSA 的发明者之一 Ron Rivest 认为限制加密技术是鲁莽的:“仅仅
2323
黑客. py*
2424

2525
```py
26-
 1\. # Transposition Cipher Hacker
27-
 2\. # https://www.nostarch.com/crackingcodes/ (BSD Licensed)
26+
# Transposition Cipher Hacker
27+
# https://www.nostarch.com/crackingcodes/ (BSD Licensed)
2828
 3.
29-
 4\. import pyperclip, detectEnglish, transpositionDecrypt
29+
import pyperclip, detectEnglish, transpositionDecrypt
3030
 5.
31-
 6\. def main():
31+
def main():
3232
 7.     # You might want to copy & paste this text from the source code at
3333
 8.     # https://www.nostarch.com/crackingcodes/:
3434
 9.     myMessage = """AaKoosoeDe5 b5sn ma reno ora'lhlrrceey e  enlh
@@ -129,10 +129,10 @@ Failed to hack encryption.
129129
代码的前几行告诉用户这个程序将做什么。第 4 行导入了几个我们在前面章节中写过或见过的模块: `pyperclip.py``detectEnglish.py`*transpositondecrypt . py*
130130

131131
```py
132-
 1\. # Transposition Cipher Hacker
133-
 2\. # https://www.nostarch.com/crackingcodes/ (BSD Licensed)
132+
# Transposition Cipher Hacker
133+
# https://www.nostarch.com/crackingcodes/ (BSD Licensed)
134134
 3.
135-
 4\. import pyperclip, detectEnglish, transpositionDecrypt
135+
import pyperclip, detectEnglish, transpositionDecrypt
136136
```
137137

138138
transposition cipher hacker 程序包含大约 50 行代码,相当短,因为它的大部分存在于我们用作模块的其他程序中。
@@ -142,7 +142,7 @@ transposition cipher hacker 程序包含大约 50 行代码,相当短,因为
142142
变量存储我们试图破解的密文。第 9 行存储一个以三重引号开始和结束的字符串值。注意这是一个很长的字符串。
143143

144144
```py
145-
 6\. def main():
145+
def main():
146146
 7.     # You might want to copy & paste this text from the source code at
147147
 8.     # https://www.nostarch.com/crackingcodes/:
148148
 9.     myMessage = """AaKoosoeDe5 b5sn ma reno ora'lhlrrceey e  enlh

0 commit comments

Comments
 (0)