CrackingCodes.Ch14 package

Submodules

CrackingCodes.Ch14.PracticeQuestions module

Chapter 14 Practice Questions

Answers Chapter 14 Practice Questions via Python code.

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

CrackingCodes.Ch14.affineCipher module

Affine Cipher

Provides functions that implement affine cipher encryption and decryption.

CrackingCodes.Ch14.affineCipher.SYMBOLS

String containing all symbols that can be encrypted/decrypted.

Type:str

Example

>>> import pythontutorials.books.CrackingCodes.Ch14.affineCipher as affineCipher
>>> someString = 'Enthusiasm is contagious. Not having enthusiasm is also contagious.'
>>> key = affineCipher.getRandomKey()  # key = 921, in this example
>>> affineCipher.encryptMessage(key, someString)
'xq3eBprFpdLrpLf4q3FRr4BpyLi43LeFOrqRL6q3eBprFpdLrpLFQp4Lf4q3FRr4Bpy'

Note

CrackingCodes.Ch14.affineCipher.checkKeys(keyA: int, keyB: int, mode: str) → None[source]

Checks keys for validity.

Prevents keyA from being 1 and keyB from being 0 (if encrypting). Makes sure keyA is relatively prime with the length of SYMBOLS. Ensures keyA is greater than 0 and that keyB is between 0 and length of SYMBOLS.

Parameters:
  • keyA – Integer integral of the original key after floor division by length of SYMBOLS.
  • keyB – Integer remainder of the original key after modulus by length of SYMBOLS.
  • mode – String specifying whether to ‘encrypt’ or ‘decrypt’.
Returns:

None if successful, exits program with error message otherwise.

CrackingCodes.Ch14.affineCipher.decryptMessage(key: int, message: str) → str[source]

Affine cipher decryption

Decrypts given affine cipher encrypted message with given key.

Parameters:
  • key – Integer decryption key to decrypt affine cipher.
  • message – Message string to decrypt.
Returns:

Decrypted message string.

CrackingCodes.Ch14.affineCipher.encryptMessage(key: int, message: str) → str[source]

Affine cipher encryption

Encrypts given message with given key using the affine cipher.

Parameters:
  • key – Integer encryption key to encrypt with affine cipher.
  • message – Message string to encrypt.
Returns:

Encrypted message string.

CrackingCodes.Ch14.affineCipher.getKeyParts(key: int) -> (<class 'int'>, <class 'int'>)[source]

Split key into parts

Splits key into keyA and keyB via floor division and modulus by length of SYMBOLS.

Parameters:key – Integer key used to encrypt message.
Returns:Tuple containing the integral and remainder.
CrackingCodes.Ch14.affineCipher.getRandomKey() → int[source]

Affine cipher key generator

Generates a random key that can be used with the affine cipher.

Returns:Random, valid integer key
CrackingCodes.Ch14.affineCipher.main()[source]

CrackingCodes.Ch14.affineKeyTest module

Test affine cipher keyspace

This program proves that the keyspace of the affine cipher is limited to less than len(SYMBOLS) ^ 2.

Note

Tests every key from 2 through 80 and prints it with the encrypted message if the key and length of SYMBOLS have a gcd.

CrackingCodes.Ch14.affineKeyTest.main()[source]

Module contents