MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / decrypt

Function decrypt

ciphers/permutation_cipher.py:94–121  ·  view source on GitHub ↗

Decrypt an encrypted message using a permutation cipher with block rearrangement. Args: encrypted_message (str): The encrypted message. key (list[int]): The permutation key for decryption. Returns: str: The decrypted plaintext message. Example: >>>

(encrypted_message: str, key: list[int])

Source from the content-addressed store, hash-verified

92
93
94def decrypt(encrypted_message: str, key: list[int]) -> str:
95 """
96 Decrypt an encrypted message using a permutation cipher with block rearrangement.
97
98 Args:
99 encrypted_message (str): The encrypted message.
100 key (list[int]): The permutation key for decryption.
101
102 Returns:
103 str: The decrypted plaintext message.
104
105 Example:
106 >>> encrypted_message, key = encrypt("HELLO WORLD")
107 >>> decrypted_message = decrypt(encrypted_message, key)
108 >>> decrypted_message
109 'HELLO WORLD'
110 """
111 key_length = len(key)
112 decrypted_message = ""
113
114 for i in range(0, len(encrypted_message), key_length):
115 block = encrypted_message[i : i + key_length]
116 original_block = [""] * key_length
117 for j, digit in enumerate(key):
118 original_block[digit] = block[j]
119 decrypted_message += "".join(original_block)
120
121 return decrypted_message
122
123
124def main() -> None:

Callers 1

mainFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected