Hacking Secret Ciphers with Python

(Ann) #1

184 http://inventwithpython.com/hacking


Email questions to the author: [email protected]


When isEnglish() is called with no second and third argument, the function will require that
20% of the words in message are English words that exist in the dictionary text file and 85% of
the characters in message are letters. These percentages work for detecting English in most
cases. But sometimes a program calling isEnglish() will want looser or more restrictive
thresholds. If so, a program can just pass arguments for wordPercentage and
letterPercentage instead of using the default arguments.


Calculating Percentage


A percentage is a number between 0 and 100 that shows how much of something there is
proportional to the total number of those things. In the string value 'Hello cat MOOSE
fsdkl ewpin' there are five “words” but only three of them are English words. To calculate
the percentage of English words, you divide the number of English words by the total
number of words and multiply by 100. The percentage of English words in 'Hello cat
MOOSE fsdkl ewpin' is 3 / 5 * 100, which is 60.


Table 12-2 shows some percentage calculations:


Table 12- 2. Some percentage calculations.
Number of
English Words

Total Number
of Words

English
Words / Total

* 100 = Percentage

3 5 0.6 * 100 = 60
6 10 0.6 *100 = 60
300 500 0.6 * 100 = 60
32 87 0.3678 * 100 = 36.78
87 87 1.0 * 100 = 100
0 10 0 * 100 = 0

The percentage will always be between 0% (meaning none of the words) and 100% (meaning all
of the words). Our isEnglish() function will consider a string to be English if at least 20% of
the words are English words that exist in the dictionary file and 85% of the characters in the
string are letters (or spaces).


detectEnglish.py
51. wordsMatch = getEnglishCount(message) * 100 >= wordPercentage


Line 51 calculates the percentage of recognized English words in message by passing
message to getEnglishCount(), which does the division for us and returns a float
between 0.0 and 1.0. To get a percentage from this float, we just have to multiply it by 100. If
this number is greater than or equal to the wordPercentage parameter, then True is stored in

Free download pdf