-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolumnar.py
59 lines (45 loc) · 1.19 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
def columnarCipher(plainText, key):
cipherText = ['']*len(key)
cipherText = list(key)
for col in range(len(key)):
pointer = col
for i in range(pointer, len(plainText), len(key)):
cipherText[col]+=plainText[i]
#order the columns in order of key letters
cipherText.sort()
for i in range(len(cipherText)):
cipherText[i] = cipherText[i][1:] #remove the key first letter
return ' '.join(cipherText)
def columnardecode(text, key):
# cipherText = ['']*len(key)
key = list(key)
key.sort()
key = ''.join(key)
print(key)
# cipherText = list(key)
# print(key)
# for col in range(len(key)):
# pointer = col
# for i in range(pointer, len(plainText), len(key)):
# cipherText[col]+=plainText[i]
# #order the columns in order of key letters
# cipherText.sort(reverse=True)
# for i in range(len(cipherText)):
# cipherText[i] = cipherText[i][1:] #remove the key first letter
# return ' '.join(cipherText)
# print(columnarCipher("THISISACOLUMNAR", "HELLO"))
print(columnardecode("HAM TSU ICN SOA ILR", "HELLO"))
#THISISACOLUMNAR
#HAMTSUICNSOAILR
# Col = 0 Pos = 0
# T
# Col = 0 Pos = 0+5
# TS
#Col = 0 Pos = 5+5
# TSU
# Col = 0 Pos = 15
# break
# Col = 1 Pos = 1
# H
# Col = 1 Pos = 1+5
# HA