-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencryption5.py
More file actions
144 lines (112 loc) · 4.1 KB
/
encryption5.py
File metadata and controls
144 lines (112 loc) · 4.1 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
"""
Encryption 5 is the same as encryption 4 but uses a dictionary instead of a list
"""
import random
import string
def notIn(val, listDict):
"""
checks if key is in any of the
:param key:
:param listDict: keys are letters values are lists of strings
:return: Boolean true if key is already in twodarray, even if only a substring
"""
for keyInner in listDict:
for keyVal in listDict[keyInner]:
if val in keyVal:
return False
return True
def noDivider(dividers, strVal):
"""
:param dividers: List of dividers that cannot be present in the strings
:param strVal: The string that could contain the dividers
:return: boolean, False if dividers are present otherwise True
"""
for divider in dividers:
if divider in strVal:
return False
else:
# Need to check that if the divider is added to the str at the end
# The divider doesn't appear earlier
temp = strVal + divider
# Get the index of where the divider appears
index = temp.find(divider)
# If it is any earlier than the len(string) then return false
if index < len(strVal):
return False
return True
def createEncryptionKey(chars, not_allowed):
"""
:param chars: the characters to generate values for
:param not_allowed: list of values that cannot be used, such as the delimiter
:return: A dictionary
"""
result = {}
# All printable characters can be used
accepted_chars = list(string.printable)
# Apart from ' as used for string and others used for newline or weird spaces the space is used as the middle delimiter during decryption
for banned in ["'", '\t', '\n', '\r', " ", '\x0b', '\x0c']:
accepted_chars.remove(banned)
for char in chars:
valList = []
# Have 4 options for every value
for i in range(4):
val = []
# Get a random amount of letters from 4 to 9
for j in range(random.randint(4, 10)):
# Get a random character
val.append(random.choice(accepted_chars))
#Make list into string
val = "".join(val)
# Before adding to the valList make sure it is unique and doesn't have dividers
if notIn(val,result) and noDivider(not_allowed, val):
valList.append(val)
result[char] = valList
return result
def createDecryptionKey(encryptionKey):
"""
It reverses the encryption key
:param encryptionKey:
:return:
"""
decryptionKey = {}
for originalLetter, valueList in encryptionKey.items():
for value in valueList:
decryptionKey[value] = originalLetter
return decryptionKey
def encrypt():
"""
:return:
"""
encrypted_list = []
for letter in plainText:
# Take one random value from the list associated with the value key
encrypted_list.append(random.choice(encryptionKey[letter]))
encrypted_text = encrypted_list[0]
for i in range(1, len(encrypted_list)):
encrypted_text += random.choice(dividers) + encrypted_list[i]
return encrypted_text
def decrypt():
global plainText
decryptedText = []
for divider in dividers:
plainText = plainText.replace(divider, " ")
words = plainText.split()
for word in words:
decryptedText += decryptionKey.get(word, "")
return "".join(decryptedText)
accepted_chars = list(string.ascii_letters + string.digits + string.punctuation + string.whitespace)
dividers = ["sk$v", "$$", "#]2f", ";lkjw"]
print("The accepted characters are: ", accepted_chars)
encryptionKey = createEncryptionKey(accepted_chars, dividers)
decryptionKey = createDecryptionKey(encryptionKey)
while True:
plainText = input("Please enter text: ")
choice = input("e or d to encrypt or decrypt (anything else to exit): ")
if choice == "e":
output = encrypt()
elif choice == "d":
output = decrypt()
else:
break
print(output)
# Hello World! ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz