python columnar cipher

Solutions on MaxInterview for python columnar cipher by the best coders in the world

showing results for - "python columnar cipher"
Melyne
25 Nov 2017
1def columnar_encrypt(text, key):
2    m = { i : [] for i in key }
3    cols = [list(text[j:j+len(key)]) for j in range(0, len(text), len(key))]
4    if len(cols[-1]) < len(key):
5        while len(cols[-1]) != len(key):
6            cols[-1].append(' ')
7    i = 0
8    for k in m.keys():
9        if i < len(key):
10            for j in cols:
11                m[k] += j[i]
12            i += 1
13    s = {k : m[k] for k in sorted(m)}
14    cipher = ''
15    for i in s.keys():
16        for x in s[i]:
17            cipher += x
18    print(m)
19    return cipher
20
21def columnar_decrypt(cipher, key):
22    if len(cipher) < len(key):
23        key = key[:len(cipher)]
24    n = len(cipher) // len(key)
25    s = { k : [] for k in sorted(key) }
26    cols = [cipher[j:j+n] for j in range(0, len(cipher), n)]    
27    i = 0
28    for k in s.keys():
29        if i < len(key):
30            s[k] = list(cols[i])
31            i += 1
32    m = {}  
33    for k in key:
34        m[k] = s[k]
35    o = m
36    plain = ''
37    import pandas as pd
38    m = pd.DataFrame(m)
39    for i in m.itertuples():
40        for j in i[1:]:
41            plain += j
42    print(s, '\n')
43    return plain.strip()
Maelia
13 Oct 2020
1def columnar_encrypt():
2    plain = input("Enter the plain text: ").replace(' ', '')
3    key = list(input("Enter the plain text: ").lower())
4    rowSize = len((key))
5    m = [(list(plain[i: i + rowSize])) for i in range(0, len(plain), rowSize)]
6    for i in m:
7        if len(i) != rowSize:
8            while len(i) != rowSize:
9                i.append('')
10        print(i)
11    key_sort = []
12    for i in sorted(key):
13        key_sort.append(key.index(i))
14        key[key.index(i)] = ''
15    print(key_sort)
16    cipher = []
17    for i in key_sort:
18        for j in range(len(m)):
19            if m[j][i] is not '':   
20                cipher.append(m[j][i])
21    return 'Cipher Text: Read if you can: {0}'.format(''.join(cipher))