Skip to content

Commit

Permalink
Merge pull request #92 from UBC-MDS/message_to_password
Browse files Browse the repository at this point in the history
Switching the wording in encypt and decrypt functions to password
  • Loading branch information
mishelly-h authored Feb 2, 2024
2 parents 3f7d7d3 + 2b8e0a5 commit a8c64ad
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 59 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ This package provides password management tools in Python. The package consists
- `encrypt_password`:
- Encrypts a password using a simple substitution cipher. This function applies a character mapping based on a shuffled character set, providing basic encryption.
- `decrypt_password`:
- Decrypts a message that was encrypted using the `encrypt_password` function. It reverses the encryption process by mapping each character of the encrypted message back to its original character.
- Decrypts a password that was encrypted using the `encrypt_password` function. It reverses the encryption process by mapping each character of the encrypted message back to its original character.

This Python package is useful for users seeking an integrated solution for password management, offering a user-friendly experience. With key functionalities consolidated in one package, users can effortlessly generate strong passwords, evaluate their strength, and grasp encryption and decryption methods through our straightforward substitution cipher.

Expand Down
4 changes: 2 additions & 2 deletions docs/example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@
"source": [
"### Encrypt a Password\n",
"\n",
"The `encrypt_password` function encrypts a message using a simple substitution cipher, by substituting each character with a corresponding character from a shuffled set. It uses the same set of characters as the decryption function, so they can work in tandem. Its first argument, `message`, requires a string input, while the second optional argument, `random_seed`, accepts an integer value."
"The `encrypt_password` function encrypts a password using a simple substitution cipher, by substituting each character with a corresponding character from a shuffled set. It uses the same set of characters as the decryption function, so they can work in tandem. Its first argument, `password`, requires a string input, while the second optional argument, `random_seed`, accepts an integer value."
]
},
{
Expand Down Expand Up @@ -317,7 +317,7 @@
"metadata": {},
"source": [
"### Decrypt a Password\n",
"The `decrypt_password` function decrypts a password that has previously been encrypted with the `encrypt_password` function. It requires the same seed that was used in the `encrypt_password`. It takes two arguments, the first `encrypted_message` is the encrypted password passed in as a string. The `random_seed` argument is the seed that was used to encrypt the password, passed in as an integer value.\n",
"The `decrypt_password` function decrypts a password that has previously been encrypted with the `encrypt_password` function. It requires the same seed that was used in the `encrypt_password`. It takes two arguments, the first `encrypted_password` is the encrypted password passed in as a string. The `random_seed` argument is the seed that was used to encrypt the password, passed in as an integer value.\n",
"\n",
"#### Decryption Defaults\n",
"The next time Bob tries to log in to Facebook, he can't remember his password. Bob knows where he saved his encrypted password on his computer, but he cannot find the seed, so he tries to decrypt his encrypted password with the default settings."
Expand Down
34 changes: 17 additions & 17 deletions src/passwordler/decrypt_password.py
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
40 changes: 20 additions & 20 deletions src/passwordler/encrypt_password.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,58 +3,58 @@
import random
from ._internals import original, getKeyMap

def encrypt_password(message, random_seed=123):
def encrypt_password(password, random_seed=123):
"""
Encrypt a message using a simple substitution cipher.
Encrypt a password using a simple substitution cipher.
This function encrypts a message by substituting each character with a corresponding character
This function encrypts a password by substituting each character with a corresponding character
from a shuffled set, using the same set of characters as the decryption function. The shuffle
is controlled by a random seed to ensure reproducible results, allowing encrypted messages
is controlled by a random seed to ensure reproducible results, allowing encrypted passwords
to be consistently decrypted using the matching seed. Characters not included in the
substitution set remain unchanged in the encrypted message.
substitution set remain unchanged in the encrypted password.
Parameters:
- message (str): The message to be encrypted.
- password (str): The password to be encrypted.
- random_seed (int): Seed for the random number generator to ensure consistent encryption
results. Default value is 123.
Returns:
- str: The encrypted message.
- str: The encrypted password.
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(message, str):
if not isinstance(password, str):
raise TypeError(
f"string expected as encrypted message, got '{type(message)}'"
f"string expected as encrypted password, got '{type(password)}'"
)

if not isinstance(random_seed, int):
raise TypeError(
f"integer expected as random_seed, got '{type(random_seed)}'"
)

if message == '':
if password == '':
raise ValueError(
'encrypted_message cannot be empty string')
'encrypted_password cannot be empty string')

random.seed(random_seed)

encryption = original.copy()
random.shuffle(encryption)

keyMap = getKeyMap(encryption)
encrypted_msg = []
encrypted_pass = []

for character in message:
for character in password:
if character in original:
encrypted_msg.append(keyMap[character])
encrypted_pass.append(keyMap[character])
else:
encrypted_msg.append(character)
encrypted_pass.append(character)

encrypted_msg = ''.join(encrypted_msg)
encrypted_pass = ''.join(encrypted_pass)

return encrypted_msg
return encrypted_pass
10 changes: 5 additions & 5 deletions tests/test_decrypt_password.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def test_decrypt_password_type_error():
"""
Test if the function returns a TypeError if the type of the input is not correct.
"""
with pytest.raises(TypeError, match="string expected as encrypted message"):
with pytest.raises(TypeError, match="string expected as encrypted password"):
decrypt_password(123)

with pytest.raises(TypeError, match="integer expected as random_seed"):
Expand All @@ -16,9 +16,9 @@ def test_decrypt_password_type_error():

def test_decrypt_password_value_error():
"""
Test if the function returns a ValueError if the input for the encrypted_message is an emptry string.
Test if the function returns a ValueError if the input for the encrypted_password is an emptry string.
"""
with pytest.raises(ValueError, match="encrypted_message cannot be empty string"):
with pytest.raises(ValueError, match="encrypted_password cannot be empty string"):
decrypt_password("")


Expand Down Expand Up @@ -47,9 +47,9 @@ def test_decrypt_password_special_characters_input():
assert isinstance(result, str)


def test_decrypt_password_long_input_message():
def test_decrypt_password_long_input_password():
"""
Test if the function handles longer messages correctly.
Test if the function handles longer passwords correctly.
"""
result = decrypt_password(
"I say you are, and I should know. I followed a few!", random_seed=42
Expand Down
6 changes: 3 additions & 3 deletions tests/test_encrypt_decrypt_password.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ def test_encryption_decryption():
"""
Test if the encryption and decryption function match each other.
"""
encrypted_message = encrypt_password("Monty Python", random_seed=123)
encrypted_password = encrypt_password("Monty Python", random_seed=123)

assert (
decrypt_password(encrypted_message, random_seed=123) == "Monty Python"
), "The encrypted message is not the same as the initial message"
decrypt_password(encrypted_password, random_seed=123) == "Monty Python"
), "The encrypted password is not the same as the initial message"
22 changes: 11 additions & 11 deletions tests/test_encrypt_password.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def test_encrypt_password_type_error():
"""
Test if the function returns a TypeError if the type of the input is not correct.
"""
with pytest.raises(TypeError, match="string expected as encrypted message"):
with pytest.raises(TypeError, match="string expected as encrypted password"):
encrypt_password(123)

with pytest.raises(TypeError, match="integer expected as random_seed"):
Expand All @@ -16,9 +16,9 @@ def test_encrypt_password_type_error():

def test_encrypt_password_value_error():
"""
Test if the function returns a ValueError if the input for the encrypted_message is an emptry string.
Test if the function returns a ValueError if the input for the encrypted_password is an emptry string.
"""
with pytest.raises(ValueError, match="encrypted_message cannot be empty string"):
with pytest.raises(ValueError, match="encrypted_password cannot be empty string"):
encrypt_password('')


Expand All @@ -32,22 +32,22 @@ def test_encrypt_password_no_seed():

def test_encrypt_normal():
"""
Test that a standard message is correctly encrypted.
The encrypted message should be different from the original message.
Test that a standard password is correctly encrypted.
The encrypted password should be different from the original password.
"""
message = "testmessage"
encrypted = encrypt_password(message, 123)
assert encrypted != message
password = "testmessage"
encrypted = encrypt_password(password, 123)
assert encrypted != password
assert isinstance(encrypted, str)


def test_encrypt_non_standard_characters():
"""
Test that non-standard characters (not in the original character set)
are unchanged in the encrypted message.
are unchanged in the encrypted password.
"""
message = "test \t\n\r\x0b\x0c"
encrypted = encrypt_password(message, 123)
password = "test \t\n\r\x0b\x0c"
encrypted = encrypt_password(password, 123)
print(encrypted)

assert all(char in encrypted for char in " \t\n\r\x0b\x0c")

0 comments on commit a8c64ad

Please sign in to comment.