The permutation cipher, also called the transposition cipher, is a simple encryption technique that rearranges the characters in a message based on a secret key. It divides the message into blocks and applies a permutation to the characters within each block according to the key. The key is a sequence of unique integers that determine the order of character rearrangement.
For more info: https://www.nku.edu/~christensen/1402%20permutation%20ciphers.pdf
|
Decrypt an encrypted message using a permutation cipher with block rearrangement. |
|
Encrypt a message using a permutation cipher with block rearrangement using a key. |
|
Generate a random permutation key of a specified block size. |
|
Generate a valid block size that is a factor of the message length. |
|
Driver function to pass message to get encrypted, then decrypted. |
Decrypt an encrypted message using a permutation cipher with block rearrangement.
encrypted_message (str): The encrypted message. key (list[int]): The permutation key for decryption.
str: The decrypted plaintext message.
>>> encrypted_message, key = encrypt("HELLO WORLD")
>>> decrypted_message = decrypt(encrypted_message, key)
>>> decrypted_message
'HELLO WORLD'
Encrypt a message using a permutation cipher with block rearrangement using a key.
message (str): The plaintext message to be encrypted. key (list[int]): The permutation key for decryption. block_size (int): The size of each permutation block.
tuple: A tuple containing the encrypted message and the encryption key.
>>> encrypted_message, key = encrypt("HELLO WORLD")
>>> decrypted_message = decrypt(encrypted_message, key)
>>> decrypted_message
'HELLO WORLD'
Generate a random permutation key of a specified block size.
block_size (int): The size of each permutation block.
list[int]: A list containing a random permutation of digits.
>>> random.seed(0)
>>> generate_permutation_key(4)
[2, 0, 1, 3]
Generate a valid block size that is a factor of the message length.
message_length (int): The length of the message.
int: A valid block size.
>>> random.seed(1)
>>> generate_valid_block_size(12)
3
Driver function to pass message to get encrypted, then decrypted.
Example: >>> main() Decrypted message: HELLO WORLD