CrackingCodes.Ch20 package

Submodules

CrackingCodes.Ch20.PracticeQuestions module

Chapter 20 Practice Questions

Answers Chapter 20 Practice Questions via Python code.

CrackingCodes.Ch20.PracticeQuestions.main()[source]

CrackingCodes.Ch20.vigenereDictionaryHacker module

Vigenère Cipher Dictionary Hacker

Implements a function that can hack a Vigenère cipher encrypted message using a dictionary.

CrackingCodes.Ch20.vigenereDictionaryHacker.DICTIONARY_FILE

String with absolute location of dictionary.txt file.

Type:str
CrackingCodes.Ch20.vigenereDictionaryHacker.hackVigenereDictionary(ciphertext: str)[source]

Hack Vigenère Dictionary

Brute-forces ciphertext by using every word in the dictionary file as a key. Checks if decrypted message is English with the isEnglish() module, and prompts user for confirmation by displaying first 100 characters.

Parameters:ciphertext – String containing Vigenère cipher encrypted message.
Returns:Decrypted message, if confirmed, None otherwise.
CrackingCodes.Ch20.vigenereDictionaryHacker.main()[source]

CrackingCodes.Ch20.vigenereHacker module

Vigenère Cipher Hacker

Implements a series of functions that can hack a Vigenère cipher encrypted message by brute-forcing key lengths.

CrackingCodes.Ch20.vigenereHacker.LETTERS

String with uppercase latin letters.

Type:str
CrackingCodes.Ch20.vigenereHacker.MAX_KEY_LENGTH

Will not attempt keys longer than this.

Type:int
CrackingCodes.Ch20.vigenereHacker.NUM_MOST_FREQ_LETTERS

Attempt this many letters per subkey.

Type:int
CrackingCodes.Ch20.vigenereHacker.SILENT_MODE

If set to True, program doesn’t print anything.

Type:bool
CrackingCodes.Ch20.vigenereHacker.NONLETTERS_PATTERN

Regular expression object representing all non-letter characters.

Type:re._sre.SRE_Pattern
CrackingCodes.Ch20.vigenereHacker.attemptHackWithKeyLength(ciphertext: str, mostLikelyKeyLength: int)[source]

Attempt hack with key length

Brute-forces ciphertext using every key of a given length, checks if decrypted message is English with the isEnglish() module, and prompts user for confirmation by displaying first 200 characters.

Parameters:
  • ciphertext – String with encrypted message.
  • mostLikelyKeyLength – Integer representing the length of the key used to encrypt message.
Returns:

Decrypted message, if confirmed, None otherwise.

Note

CrackingCodes.Ch20.vigenereHacker.findRepeatSequencesSpacings(message: str) → dict[source]

Find spacing between repeat sequences

Goes through the message and finds any 3- to 5-letter sequences that are repeated. Then counts the number of letters between the repeated sequences.

Parameters:message – String with message to find repeat sequence spacing.
Returns:Dictionary with the keys of the sequence and values of a list of spacings (num of letters between the repeats).
CrackingCodes.Ch20.vigenereHacker.getItemAtIndexOne(x: tuple) → int[source]

Get item at index one

Helper function that returns the second element of given tuple.

Parameters:x – Tuple with integers as values.
Returns:Second element of x.
CrackingCodes.Ch20.vigenereHacker.getMostCommonFactors(seqFactors: dict) → list[source]

Get most common factors

Counts how often each factor in the seqFactors dictionary occurs and returns a list of tuples with each factor and its count.

Parameters:seqFactors – Dictionary with 3- to 5- letter sequences as keys and the factors of the spacings between them as values.
Returns:A list of tuples of each factor and its count.
CrackingCodes.Ch20.vigenereHacker.getNthSubkeysLetters(nth: int, keyLength: int, message: str) → str[source]

Get nth subkeys letters

Gets every nth letter for each set of letters of a given length in a given text.

Parameters:
  • nth – Integer representing desired letter in message (similar to an index number).
  • keyLength – Integer representing length of key to use (spacing between nth letters).
  • message – String containing text to extract subkey letters from.
Returns:

String with every nth letter for each specified key length.

Examples

>>> getNthSubkeysLetters(1, 3, 'ABCABCABC')
'AAA'
>>> getNthSubkeysLetters(2, 3, 'ABCABCABC')
'BBB'
>>> getNthSubkeysLetters(3, 3, 'ABCABCABC')
'CCC'
>>> getNthSubkeysLetters(1, 5, 'ABCDEFGHI')
'AF'
CrackingCodes.Ch20.vigenereHacker.getUsefulFactors(num: int) → list[source]

Get useful factors

Returns a list of useful factors of num. By “useful” we mean factors less than MAX_KEY_LENGTH + 1 and not 1.

Parameters:num – Integer to get useful factors of.
Returns:List of useful factors, if found, empty list otherwise.

Example

>>> getUsefulFactors(144)
[2, 3, 4, 6, 8, 9, 12, 16]
CrackingCodes.Ch20.vigenereHacker.hackVigenere(ciphertext: str)[source]

Hack vigenere

Hacks Vigenère cipher encrypted message using likely key lengths, otherwise all possible key lengths.

Parameters:ciphertext – String containing Vigenère cipher encrypted message.
Returns:Decrypted message, if confirmed, None otherwise.
CrackingCodes.Ch20.vigenereHacker.kasiskiExamination(ciphertext: str) → list[source]

Kasiski Examination

Uses Kasiski Examination to determine the likely length of the key used to encrypt the given ciphertext.

Parameters:ciphertext – String containing encrypted message.
Returns:List of likely key lengths used to encrypt message.
CrackingCodes.Ch20.vigenereHacker.main()[source]

Module contents