-
Notifications
You must be signed in to change notification settings - Fork 0
/
columnar.py
166 lines (130 loc) · 6.1 KB
/
columnar.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# Author: Andrew Thompson
# Description: Application for encrypting and decrypting Strings using a columnar transposition.
#
#
# Currently working on the functionality of the GUI
#
import random
from appJar import gui
def generateKey(raw_key): #return permutation with no duplicates
acc=''
acc1=''
acc2=''
key=''
for i in range(1,len(raw_key)+1): #reverse the raw_key in order to eliminate all duplicates
acc=acc+raw_key[-i]
for ch in acc: #eliminate duplicates
if ch not in acc1:
acc1=acc1+ch
for j in range(1,len(acc1)+1): #reverse back to original order
acc2=acc2+acc1[-j]
for ch1 in acc2: #create permutation
count=0
for newch1 in acc2:
if newch1<ch1:
count=count+1
key=key+str(count+1)
return(key)
def toPlaintext(s,keyLength): #eliminate anything but letters and make uppercase, add random letters to fill last row
s = s.upper()
upper = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
for ch in s: #eliminate anything that is not a letter
if ch not in upper:
s = s.replace(ch,'')
intermediate = s
if len(intermediate)%keyLength != 0: #add random letters to fill last row
numLetters = keyLength - len(intermediate)%keyLength
for i in range(numLetters):
index = random.randint(0,25)
intermediate = intermediate + upper[index]
return(intermediate)
def columnar(plaintext,key): #construct ciphertext using columnar transposition
acc = ''
listInt = []
finalList = []
order = []
keyLength = len(key)
for i in range(len(key)): #create a list from key
listInt.append(int(key[i]))
finalList.append(int(key[i]))
for i in range(len(listInt)): #order the list from key
order.append(min(listInt))
listInt.remove(min(listInt))
for item in order: #add columns in order to accumulator
for i in range(len(plaintext)//keyLength):
acc = acc + plaintext[finalList.index(item)+(keyLength*i)]
return(acc)
def encryptColumnar(s,raw_key): #combine all other functions to complete encryption
key = generateKey(raw_key)
keyLength = len(key)
plaintext = toPlaintext(s,keyLength)
ciphertext = columnar(plaintext,key)
return(ciphertext)
def decryptColumnar(ciphertext,raw_key):
acc = ''
listInt = []
finalList = []
order = []
key = generateKey(raw_key)
keyLength = len(key)
copyCipher = ''
for i in range(keyLength):
copyCipher = copyCipher + ciphertext[(len(ciphertext)//keyLength)*i:(len(ciphertext)//keyLength)*(i+1)] + ' ' #separate original columns into a list
listCipher = copyCipher.split(' ')
for i in range(len(key)): #create a list from key
listInt.append(int(key[i]))
finalList.append(int(key[i]))
for i in range(len(listInt)): #order the list from key
order.append(min(listInt))
listInt.remove(min(listInt))
for i in range(len(ciphertext)//keyLength): #create a string with the letters put back into order
for item in finalList:
acc = acc + listCipher[order.index(item)][i]
return(acc) #print plaintext
app = gui('main window', '800x400') # create main user window
app.setFont(20)
app.addLabel("title", "Welcome to columnar cryptographer!")
app.setLabelBg("title","white")
app.addLabel('question',"Would you like to encrypt or decrypt?")
app.setLabelBg('question', 'white')
def press(button): # define user event
if button == "Encrypt":
app.stop()
encryptApp = gui('encrypt window', '800x400') # create new window for encryption
encryptApp.addLabelEntry('Enter text for encryption: (letters only, no numbers allowed)')
encryptApp.addLabelEntry('Enter key for encryption: (can be mix of letters and numbers)')
encryptApp.setFocus('Enter text for encryption: (letters only, no numbers allowed)') # put cursor in label by default
encryptApp.setFont(20)
def submitEncrypt(button): # define user event
encryptText = encryptApp.getEntry('Enter text for encryption: (letters only, no numbers allowed)') # get entries that user inputs
encryptKey = encryptApp.getEntry('Enter key for encryption: (can be mix of letters and numbers)')
encryptApp.stop()
resultsEncrypt = gui('results encrypt', '800x400') # create new window for results
resultsEncrypt.addSelectableLabel('results', 'Encrypted result: ' + encryptColumnar(encryptText, encryptKey))
resultsEncrypt.setLabelBg('results', 'white')
resultsEncrypt.setFont(20)
resultsEncrypt.go()
encryptApp.addButton('Submit', submitEncrypt) # create button for submitting encryption
encryptApp.go()
else:
app.stop()
decryptApp = gui('decrypt window', '800x400') # create new window for decryption
decryptApp.addLabelEntry('Enter text for decryption:')
decryptApp.addLabelEntry('Enter key for decryption:')
decryptApp.setFocus('Enter text for decryption:')
decryptApp.setFont(20)
def submitDecrypt(button): # define user event
decryptText = decryptApp.getEntry('Enter text for decryption:') # get entries that user input
decryptKey = decryptApp.getEntry('Enter key for decryption:')
decryptApp.stop()
resultsDecrypt = gui('results decrypt', '800x400') # create new window for results
resultsDecrypt.addLabel('results', 'Decrypted result: ' + decryptColumnar(decryptText, decryptKey))
resultsDecrypt.setLabelBg('results', 'white')
resultsDecrypt.addLabel('disclaimer', 'Please be aware that all spaces have been removed')
resultsDecrypt.setLabelBg('disclaimer', 'white')
resultsDecrypt.setFont(20)
resultsDecrypt.go()
decryptApp.addButton('Submit', submitDecrypt) # create button for submitting decryption
decryptApp.go()
app.addButtons(["Encrypt", 'Decrypt'], press) # add encryption and decryption buttons to main window
app.go()