0% found this document useful (0 votes)
2 views

NIS EXP9

The document describes the Simple Columnar Transposition technique, a manual encryption method that rearranges plaintext letters based on a keyword. It includes a Python program for both encrypting and decrypting messages using this technique, demonstrating the process with an example. The conclusion emphasizes that transposition ciphers reorder plaintext characters to create ciphertext, relying on mathematical functions for encryption and decryption.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

NIS EXP9

The document describes the Simple Columnar Transposition technique, a manual encryption method that rearranges plaintext letters based on a keyword. It includes a Python program for both encrypting and decrypting messages using this technique, demonstrating the process with an example. The conclusion emphasizes that transposition ciphers reorder plaintext characters to create ciphertext, relying on mathematical functions for encryption and decryption.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Practical No.

9
Aim: Write a program to implement Simple Columnar transposition
Technique
Introduction:

The Columnar Transposition rearranges the plaintext letters, based on a matrix


filled with letters in the order determined by the secret keyword.

The Columnar Transposition is a simple transposition cipher that can be performed


manually, without the need of using additional equipment. It was very popular
throughout centuries, and it was used in various situations by diplomats, soldiers,
and spies.
The encryption and decryption can be performed by hand, using a piece of paper and a
simple matrix, into which the user enters the letters of the message.

Example:

A simple example for a transposition cipher is columnar transposition cipher where


each character in the plain text is written horizontally with specified alphabet width.
The cipher is written vertically, which creates an entirely different cipher text.

Consider the plain text hello world, and let us apply the simple columnar
transposition technique as shown below

The plain text characters are placed horizontally and the cipher text is created with
vertical format as : holewdlo lr. Now, the receiver has to use the same table to
decrypt the cipher text to plain text.
Program:
# Python3 implementation of
# Columnar Transposition
import math
key = "HACK"

# Encryption
def encryptMessage(msg):
cipher = ""
# track key indices
k_indx = 0

msg_len = float(len(msg))
msg_lst = list(msg)
key_lst = sorted(list(key))
# calculate column of the matrix
col = len(key)

# calculate maximum row of the matrix


row = int(math.ceil(msg_len / col))

# add the padding character '_' in empty


# the empty cell of the matix
fill_null = int((row * col) - msg_len) msg_lst.extend('_' * fill_null)
# create Matrix and insert message and
# padding characters row-wise
matrix = [msg_lst[i: i + col]
for i in range(0, len(msg_lst), col)]
# read matrix column-wise using key
for _ in range(col):
curr_idx = key.index(key_lst[k_indx])
cipher += ''.join([row[curr_idx]
for row in matrix])
k_indx += 1 return cipher
# Decryption
def decryptMessage(cipher):
msg = ""

# track key indices


k_indx = 0

# track msg indices


msg_indx = 0
msg_len = float(len(cipher))
msg_lst = list(cipher)
# calculate column of the matrix
col = len(key)

# calculate maximum row of the matrix


row = int(math.ceil(msg_len / col))
# convert key into list and sort
# alphabetically so we can access
# each character by its alphabetical position.
key_lst = sorted(list(key))

# create an empty matrix to


# store deciphered message
dec_cipher = []
for _ in range(row):
dec_cipher += [[None] * col]

# Arrange the matrix column wise according


# to permutation order by adding into new matrix
for _ in range(col):
curr_idx = key.index(key_lst[k_indx])

for j in range(row):
dec_cipher[j][curr_idx] = msg_lst[msg_indx]
msg_indx += 1
k_indx += 1

# convert decrypted msg matrix into a string


try:
msg = ''.join(sum(dec_cipher, []))
except TypeError:
raise TypeError("This program cannot",
"handle repeating words.")

null_count = msg.count('_')

if null_count > 0:
return msg[: -null_count]
return msg

# Driver Code
msg = "Geeks for Geeks"

cipher = encryptMessage(msg)
print("Encrypted Message: {}".
format(cipher))
print("Decryped Message: {}".
format(decryptMessage(cipher)))
Output:

Conclusion:

Transposition Cipher is a cryptographic algorithm where the order of alphabets in the


plaintext is rearranged to form a cipher text. In this process, the actual plain text
alphabets are not included.In cryptography, a transposition cipher is a method of
encryption by which the positions held by units of plaintext (which are commonly
characters or groups of characters) are shifted according to a regular system, so that
the cipher text constitutes a permutation of the plaintext. That is, the order of the
units is changed (the plaintext is reordered).
Mathematically a bijective function is used on the characters' positions to
encrypt and an inverse function to decrypt.

You might also like