Demonstrating Transposition Cipher in Java

A transposition cipher is a method of encryption where the positions of characters are shifted according to a certain system, without changing the actual characters themselves. It rearranges the characters in a message to create ciphertext, and the same method in reverse restores the original message.

This post walks you through how a transposition cipher works using a Java program. This particular example arranges characters into a matrix and reads them column-wise (for encryption) or row-wise (for decryption).


How the Transposition Cipher Works

  1. Encryption:
    • Fill a matrix column-wise with characters of the message.
    • Read the characters row-wise to get the encrypted message.
    • The operation is repeated twice for added confusion.
  2. Decryption:
    • Reverse the above process by filling the matrix row-wise.
    • Read it column-wise to retrieve the original message.

Java Implementation of Transposition Cipher

package an_tans;
import java.io.*;

public class An_tans {

    // Method for encryption
    public static String enc(String ip, int m_row, int m_col) {
        char[][] op = new char[100][100];
        int len = ip.length();
        String op2 = "";
        int i1, i2, i;

        // Fill matrix column-wise with input characters
        for (i = 0, i1 = 0, i2 = 0; i < len; i++) {
            op[i2][i1] = ip.charAt(i);
            i2++;
            if (i2 == m_row) {
                i2 = 0;
                i1++;
            }
        }

        // Display the matrix
        System.out.println("Cipher matrix:");
        for (i1 = 0; i1 < m_row; i1++) {
            for (i2 = 0; i2 < m_col; i2++) {
                System.out.print(op[i1][i2] + " ");
            }
            System.out.println();
        }

        // Read the matrix row-wise to form the encrypted message
        for (i1 = 0; i1 < m_row; i1++) {
            for (i2 = 0; i2 < m_col; i2++) {
                op2 = op2 + op[i1][i2];
            }
        }
        return (op2);
    }

    // Method for decryption
    public static String dec(String ip, int m_row, int m_col) {
        char[][] op = new char[100][100];
        int len = ip.length();
        String op2 = "";
        int i1, i2, i;

        // Fill matrix row-wise with input characters
        for (i = 0, i1 = 0, i2 = 0; i < len; i++) {
            op[i1][i2] = ip.charAt(i);
            i2++;
            if (i2 == m_col) {
                i2 = 0;
                i1++;
            }
        }

        // Display the matrix
        System.out.println("Cipher matrix");
        for (i1 = 0; i1 < m_row; i1++) {
            for (i2 = 0; i2 < m_col; i2++) {
                System.out.print(op[i1][i2] + " ");
            }
            System.out.println();
        }

        // Read the matrix column-wise to recover the original message
        for (i1 = 0; i1 < m_col; i1++) {
            for (i2 = 0; i2 < m_row; i2++) {
                op2 = op2 + op[i2][i1];
            }
        }
        return (op2);
    }

    public static void main(String[] args) throws IOException {
        BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));

        int m_col;
        int m_row;
        String ip;
        int len;
        String op2;

        System.out.println("Enter input:");
        ip = obj.readLine();
        len = ip.length();

        System.out.println("Enter number of rows:");
        m_row = Integer.parseInt(obj.readLine());

        // Calculate number of columns based on input length
        m_col = (int) Math.ceil((float) len / m_row);

        System.out.println("What do you want to perform:\n1.Encryption\n2.Decryption");
        int ch;
        ch = Integer.parseInt(obj.readLine());

        if (ch == 1) {
            op2 = enc(ip, m_row, m_col);
            op2 = enc(op2, m_row, m_col);
            System.out.println("Cipher text:" + op2);
        } else if (ch == 2) {
            op2 = dec(ip, m_row, m_col);
            op2 = dec(op2, m_row, m_col);
            System.out.println("Plain text:" + op2);
        } else {
            System.out.println("Invalid Choice");
        }
    }
}

Sample Output and Explanation

Enter input:
MEETMEATPARK
Enter number of rows:
2
What do you want to perform:
1.Encryption
2.Decryption
1
Cipher matrix:
M E M A P R 
E T E T A K 
Cipher matrix:
M M P E E A 
E A R T T K 
Cipher text:MMPEEAEARTTK

Enter input:
MMPEEAEARTTK
Enter number of rows:
2
What do you want to perform:
1.Encryption
2.Decryption
2
Cipher matrix
M M P E E A 
E A R T T K 
Cipher matrix
M E M A P R 
E T E T A K 
Plain text:MEETMEATPARK

Step-by-Step Explanation of Output

  • Input Message: MEETMEATPARK
  • Number of Rows: 2
  • Length: 12 → so columns = 12 / 2 = 6

First Encryption:

  • Fill the matrix column-wise:
    • Column 1: M E
    • Column 2: E T
    • Column 3: M E
    • Column 4: A T
    • Column 5: P A
    • Column 6: R K
  • Read the matrix row-wise:
    • Row 1: M E M A P R
    • Row 2: E T E T A K → Encrypted once: MEMAPRETETAK

Second Encryption:

  • Fill matrix again column-wise with the above result
    • Columns: M E, M A, P R, E T, E T, A K
  • Read row-wise:
    • Row 1: M M P E E A
    • Row 2: E A R T T K → Final Cipher Text: MMPEEAEARTTK

Decryption:

  • Fill the matrix row-wise with the cipher text (MMPEEAEARTTK):
    • Row 1: M M P E E A
    • Row 2: E A R T T K
  • Read column-wise:
    • M E M A P R
    • E T E T A K → After one decryption: MEMAPRETETAK
  • Repeat decryption to recover original message:
    • Rows: M E M A P R and E T E T A K
    • Read column-wise again: MEETMEATPARK

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.