-
Notifications
You must be signed in to change notification settings - Fork 0
/
encryption.py
39 lines (31 loc) · 1.02 KB
/
encryption.py
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
import base64
def encrypt(message, key):
cipherChars = []
for i in range(len(message)):
currentKey = key[i % len(key)]
currentChar = chr(ord(message[i]) + ord(currentKey) % 256)
cipherChars.append(currentChar)
ciphertext = "".join(cipherChars)
return ciphertext
def decrypt(ciphertext, key):
plainChars = []
for i in range(len(ciphertext)):
currentKey = key[i % len(key)]
currentChar = chr((256 + ord(ciphertext[i]) - ord(currentKey)) % 256)
plainChars.append(currentChar)
plaintext = "".join(plainChars)
return plaintext
def overload(text1, text2, flag):
if flag is True:
output = encrypt(text1, text2)
return output
else:
output = decrypt(text1, text2)
return output
message = raw_input("Enter a message: ")
key = raw_input("Enter a key: ")
ciphertext = overload(message, key, True)
gibberish = base64.urlsafe_b64encode(ciphertext)
print(gibberish)
plaintext = overload(ciphertext, key, False)
print(plaintext)