-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpassword.py
More file actions
40 lines (28 loc) · 997 Bytes
/
password.py
File metadata and controls
40 lines (28 loc) · 997 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
def encrypt(password: str, salt: str) -> str:
"""
Encrypt the password using a simple character shift with the salt.
"""
encrypted: str = ""
key_len: int = len(salt)
for index, char in enumerate(password):
encrypted += chr(ord(char) + ord(salt[index % key_len]))
return encrypted
def decrypt(encoded_pass: str, salt: str) -> str:
"""
Decrypt the encoded password back to the original using the salt.
"""
decrypted: str = ""
key_len: int = len(salt)
for index, char in enumerate(encoded_pass):
decrypted += chr(ord(char) - ord(salt[index % key_len]))
return decrypted
def main() -> None:
"""Run a simple demo."""
password: str = input("Enter password: ")
key: str = input("Enter key: ")
encrypted: str = encrypt(password, key)
decrypted: str = decrypt(encrypted, key)
print("Encrypted password:", encrypted)
print("Decrypted password:", decrypted)
if __name__ == "__main__":
main()