-
Notifications
You must be signed in to change notification settings - Fork 0
/
superTopSecretEncryption.py
141 lines (94 loc) · 3.08 KB
/
superTopSecretEncryption.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
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
#Example at the bottom
#FUNCTIONS
#IMPORTANT Use these functions to encrypt or decrypt a string (of lowercase letters or numbers or space) with a specific key (which is a number)
#turn plainText into cipherText. input (string, number)
def encrypt(plainText, key):
cipherText = getCharCodes(plainText);
cipherText = toLowerNumbers(cipherText);
cipherText = encryptionAction(cipherText, key);
cipherText = toHigherNumbers(cipherText);
cipherText = arrayToString(cipherText);
return cipherText
#turn cipherText into plainText. input (string, number)
def decrypt(cipherText, key) :
plainText = getCharCodes(cipherText);
plainText = toLowerNumbers(plainText);
plainText = decryptionAction(plainText, key);
plainText = toHigherNumbers(plainText);
plainText = arrayToString(plainText);
return plainText
#The rest of these functions are subset functions of encrypt and decrypt
#modulus that gives positives. input(number, number)
def mod(n, m) :
return ((n % m) + m) % m
#turn plainText into array of Character codes. input(string)
def getCharCodes(plainText) :
charCodes = []
i = 0
while i < len(plainText):
charCodes.append(ord(plainText[i]))
i += 1
return charCodes
#turn array of character codes into 0-26. input(array)
def toLowerNumbers(array) :
newArray = []
i = 0
while i < len(array):
newArray.append(array[i] - 32);
i += 1
return newArray
#turn 0-26 back to character codes. input(array)
def toHigherNumbers(array) :
newArray = [];
i = 0
while i < len(array):
newArray.append(array[i] + 32);
i += 1
return newArray
#turn an array of character codes into string. input(array)
def arrayToString(array):
# converting character array to string
return "".join(chr(i) for i in array)
#turn plainText array into cipherText array. input(array, number)
def encryptionAction(array, key) :
keyString = str(key)
keyNumbers = getCharCodes(keyString)
keyLength = len(keyNumbers)
encryptedArray = []
i = 0
while i < len(array):
j = 0
while j < keyLength:
if i%keyLength==j :
newEntry = ( (array[i] + (10*keyNumbers[j])) % 95 )
encryptedArray.append(newEntry)
j += 1
i += 1
return encryptedArray
#turn cipherText array into plainText array. input(array, number)
def decryptionAction(array, key) :
keyString = str(key)
keyNumbers = getCharCodes(keyString)
keyLength = len(keyNumbers)
decryptedArray = []
i = 0
while i < len(array):
j = 0
while j < keyLength:
if i % keyLength == j :
newEntry = mod(array[i] - (10*keyNumbers[j]), 95 )
decryptedArray.append(newEntry)
j += 1
i += 1
return decryptedArray
#EXAMPLE
examplePlainText = "0123456789 ABC abcdefghijklmnopqrstuvwxyz"
print(examplePlainText)
exampleKey = 3095
print(exampleKey)
#encrypt the example
exampleCipherText = encrypt(examplePlainText, exampleKey)
print(exampleCipherText)
#decrypt the example
decryptedExampleCipherText = decrypt(exampleCipherText, exampleKey)
print(decryptedExampleCipherText)