row column transposition cipher code example
Example 1: python simple columnar cipher
def columnar_encrypt(text, key):
m = { i : [] for i in key }
cols = [list(text[j:j+len(key)]) for j in range(0, len(text), len(key))]
if len(cols[-1]) < len(key):
while len(cols[-1]) != len(key):
cols[-1].append(' ')
i = 0
for k in m.keys():
if i < len(key):
for j in cols:
m[k] += j[i]
i += 1
s = {k : m[k] for k in sorted(m)}
cipher = ''
for i in s.keys():
for x in s[i]:
cipher += x
print(m)
return cipher
def columnar_decrypt(cipher, key):
if len(cipher) < len(key):
key = key[:len(cipher)]
n = len(cipher) // len(key)
s = { k : [] for k in sorted(key) }
cols = [cipher[j:j+n] for j in range(0, len(cipher), n)]
i = 0
for k in s.keys():
if i < len(key):
s[k] = list(cols[i])
i += 1
m = {}
for k in key:
m[k] = s[k]
o = m
plain = ''
import pandas as pd
m = pd.DataFrame(m)
for i in m.itertuples():
for j in i[1:]:
plain += j
print(s, '\n')
return plain.strip()
Example 2: python columnar cipher
def columnar_encrypt():
plain = input("Enter the plain text: ").replace(' ', '')
key = list(input("Enter the plain text: ").lower())
rowSize = len((key))
m = [(list(plain[i: i + rowSize])) for i in range(0, len(plain), rowSize)]
for i in m:
if len(i) != rowSize:
while len(i) != rowSize:
i.append('')
print(i)
key_sort = []
for i in sorted(key):
key_sort.append(key.index(i))
key[key.index(i)] = ''
print(key_sort)
cipher = []
for i in key_sort:
for j in range(len(m)):
if m[j][i] is not '':
cipher.append(m[j][i])
return 'Cipher Text: Read if you can: {0}'.format(''.join(cipher))