CrackingCodes.Ch24 package

Submodules

CrackingCodes.Ch24.publicKeyCipher module

Public Key Cipher

Implements a series of functions capable of encrypting and decrypting with textbook RSA public/private keypairs.

CrackingCodes.Ch24.publicKeyCipher.SYMBOLS

String with all characters to be encrypted/decrypted.

Type:str
CrackingCodes.Ch24.publicKeyCipher.PUBLIC_KEY_PATH

String with absolute location of public key file.

Type:str
CrackingCodes.Ch24.publicKeyCipher.PRIVATE_KEY_PATH

String with absolute location of private key file.

Type:str

Note

CrackingCodes.Ch24.publicKeyCipher.decryptMessage(encryptedBlocks: list, messageLength: int, key: tuple, blockSize: int) → str[source]

Decrypt Message

Decrypts a list of encrypted block integers back to the original message string.

Parameters:
  • encryptedBlocks – List containing block integers encrypted with PUBLIC key.
  • messageLength – Length of the original message.
  • key – Tuple with PRIVATE key used to decryption.
  • blockSize – Bit size of block integers (usually specified in PRIVATE key file).
Returns:

Original message before block integer conversion and PUBLIC key encryption.

Notes

  • The original message length is required to properly decrypt the last block.
  • Ensure to pass the PRIVATE key to decrypt.
CrackingCodes.Ch24.publicKeyCipher.encryptAndWriteToFile(messageFilename: str, keyFilename: str, message: str, blockSize: int = None) → str[source]

Encrypt and write to file

Using a key from a keyfile, encrypt the message and save it to a file.

Parameters:
  • messageFilename – String containing name of file to save encrypted message to.
  • keyFilename – String containing absolute file path of PUBLIC key file.
  • message – String containing message to encrypt and save.
  • blockSize – Bit size of blocks of integers used to convert and encrypt message (usually specified in PUBLIC key file).
Returns:

Encrypted message string.

CrackingCodes.Ch24.publicKeyCipher.encryptMessage(message: str, key: tuple, blockSize: int) → list[source]

Encrypt message

Converts the message string into a list of block integers, and then encrypts each block integer.

Parameters:
  • message – String containing message to encrypt with PUBLIC key.
  • key – Tuple with PUBLIC key used for encryption.
  • blockSize – Bit size of block integers (usually specified in the PUBLIC key file).
Returns:

List of block integers encrypted with PUBLIC key.

Note

  • Ensure to pass the PUBLIC key to encrypt.
CrackingCodes.Ch24.publicKeyCipher.getBlocksFromText(message: str, blockSize: int) → list[source]

Get blocks from text

Converts a string message to a list of block integers.

Parameters:
  • message – String containing message to convert into blocks of integers.
  • blockSize – Size of each block of integers.
Returns:

List with blocks of integers of the given size.

Note

  • If a character in the message is not in SYMBOLS, program exits with an error.
CrackingCodes.Ch24.publicKeyCipher.getTextFromBlocks(blockInts: list, messageLength: int, blockSize: int) → str[source]

Get text from blocks

Converts a list of block integers to the original message string.

Parameters:
  • blockInts – List of block integers of specified size.
  • messageLength – Length of the original message.
  • blockSize – Bit size of each block of integers.
Returns:

Original message string before block integer conversion.

Note

  • The original message length is needed to properly convert the last block integer.
CrackingCodes.Ch24.publicKeyCipher.main()[source]
CrackingCodes.Ch24.publicKeyCipher.readFromFileAndDecrypt(messageFilename: str, keyFilename: str) → str[source]

Read from file and decrypt

Using a key from a key file, read an encrypted message from a file and then decrypt it.

Parameters:
  • messageFilename – String containing name of file with encrypted message saved to it.
  • keyFilename – String containing absolute file path of PRIVATE key file.
Returns:

Decrypted message string.

Note

  • Checks block size in key file and exits with error if too large.
CrackingCodes.Ch24.publicKeyCipher.readKeyFile(keyFilename: str) → tuple[source]

Read key from key file

Reads the given public/private key file and returns the key.

Parameters:keyFilename – String containing absolute path to public/private key file.
Returns:The key as a (n,e) or (n,d) tuple value.

Module contents