-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #92 from UBC-MDS/message_to_password
Switching the wording in encypt and decrypt functions to password
- Loading branch information
Showing
7 changed files
with
59 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,61 @@ | ||
import random | ||
from ._internals import original, getKeyMap | ||
|
||
def decrypt_password(encrypted_message, random_seed = 123): | ||
def decrypt_password(encrypted_password, random_seed = 123): | ||
""" | ||
Decrypt an encrypted password or message using a simple substitution cipher. | ||
Decrypt an encrypted password using a simple substitution cipher. | ||
The function uses a substitution cipher to decrypt an encrypted string. The | ||
original set is replaced with a randomly shuffled character from the same set. | ||
The random seed is utilized to make sure that the encryption and the decryption | ||
matches. | ||
Parameters: | ||
- encrypted_message (str): The encrypted message to be decrypted. | ||
- encrypted_password (str): The encrypted password to be decrypted. | ||
- random_seed (int): Seed for the random number generator to ensure that | ||
encryption and decryption match. Default is 123. | ||
Returns: | ||
- str: The decrypted message. | ||
- str: The decrypted password. | ||
The function uses a substitution cipher where each character in the original | ||
set is replaced with a randomly shuffled character from the same set. The random | ||
seed is utilized to maintain consistent decryption results when needed. | ||
Example: | ||
>>> original_message = 'Monty Python' | ||
>>> encrypted_message = encrypt_password(original_message, random_seed = 123) | ||
>>> decrypted_message = decrypt_password(encrypted_message, random_seed = 123) | ||
>>> original_password = 'Monty Python' | ||
>>> encrypted_password = encrypt_password(original_password, random_seed = 123) | ||
>>> decrypted_password = decrypt_password(encrypted_password, random_seed = 123) | ||
Output: 'Monty Python' | ||
""" | ||
if not isinstance(encrypted_message, str): | ||
if not isinstance(encrypted_password, str): | ||
raise TypeError( | ||
f"string expected as encrypted message, got '{type(encrypted_message)}'" | ||
f"string expected as encrypted password, got '{type(encrypted_password)}'" | ||
) | ||
|
||
if not isinstance(random_seed, int): | ||
raise TypeError( | ||
f"integer expected as random_seed, got '{type(random_seed)}'" | ||
) | ||
|
||
if encrypted_message == '': | ||
if encrypted_password == '': | ||
raise ValueError( | ||
'encrypted_message cannot be empty string') | ||
'encrypted_password cannot be empty string') | ||
|
||
random.seed(random_seed) | ||
|
||
decryption = original.copy() | ||
random.shuffle(decryption) | ||
|
||
keyMap = getKeyMap(decryption, isDecryption=True) | ||
decrypted_msg = [] | ||
decrypted_pass = [] | ||
|
||
for character in encrypted_message: | ||
for character in encrypted_password: | ||
if character in keyMap: | ||
decrypted_msg.append(keyMap[character]) | ||
decrypted_pass.append(keyMap[character]) | ||
else: | ||
decrypted_msg.append(character) | ||
decrypted_pass.append(character) | ||
|
||
decrypted_msg = ''.join(decrypted_msg) | ||
decrypted_pass = ''.join(decrypted_pass) | ||
|
||
return decrypted_msg | ||
return decrypted_pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters