forked from BotsheloRamela/Python-Text-Encoder-and-Decoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_encode_decode.py
More file actions
72 lines (50 loc) · 2.24 KB
/
Copy pathpython_encode_decode.py
File metadata and controls
72 lines (50 loc) · 2.24 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import base64
def encode(key, message):
"""Encode message"""
encoded = []
"""The remainder of division between i and len(key) uses the remainder
as index of key, value of key at index is stored in key_chars.
The function ord() is used to get integer unicode value of unicode character (message),
then convert the value at index i into integer value. The remainder of division
of addition of ord(message[i]) and ord(key_chars) with 256 is converted then stored in encoded.
encode() returns a utf-8 encoded message, decode() will decode the string"""
for i in range(len(message)):
key_chars = key[i % len(key)]
encoded.append(chr((ord(message[i]) + ord(key_chars)) % 256))
return base64.urlsafe_b64encode("".join(encoded).encode()).decode()
def decode(key, message):
"""Decode message"""
decoded = []
message = base64.urlsafe_b64decode(message).decode()
"""The remainder of addition of 256 with the subtraction and the division with 256
will give a remainder that is then passed in the chr function to convert the integer
value to string and store to decoded"""
for i in range(len(message)):
key_chars = key[i % len(key)]
decoded.append(chr((256 + ord(message[i]) - ord(key_chars)) % 256))
return "".join(decoded)
def mode(choice):
"""Set mode"""
if choice.lower() == 'e' or choice.lower() == 'encode':
text,private_key = get_message_and_pk()
result = encode(private_key, text)
print(f'\nEncoded message: {result}')
elif choice.lower() == 'd' or choice.lower() == 'decode':
private_key,encryption_message=get_encrypted_message()
result = decode(private_key, encryption_message)
print(f'\nDecoded message: {result}')
else:
print('\nInvalid Mode!\n')
def get_message_and_pk():
text = input('Enter a message: ')
private_key = input('Enter a private key to use: ')
return text,private_key
def get_encrypted_message():
private_key = input('Enter a private key: ')
encryption_message = input("Input encyption message")
return private_key,encryption_message
def main():
choice = input('Mode (Encode or Decode): ')
mode(choice)
if __name__ == '__main__':
main()