Java program to Implement Rail Fence Cipher

Ram Pothuraju
// File Name: RailFence.java

import java.util.*;

// Class containing Encryption and Decryption methods
class RailFenceBasic {

    // Method for Encryption
    String Encryption(String plainText, int depth) throws Exception {

        // r = number of rows
        int r = depth;

        // Length of plain text
        int len = plainText.length();

        // Number of columns
        int c = len / depth;

        // Matrix creation
        char mat[][] = new char[r][c];

        int k = 0;

        // String to store encrypted text
        String cipherText = "";

        // Filling characters column-wise into matrix
        for (int i = 0; i < c; i++) {
            for (int j = 0; j < r; j++) {

                // Insert characters into matrix
                if (k != len)
                    mat[j][i] = plainText.charAt(k++);

                // Fill extra spaces with X
                else
                    mat[j][i] = 'X';
            }
        }

        // Reading matrix row-wise to generate cipher text
        for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++) {
                cipherText += mat[i][j];
            }
        }

        return cipherText;
    }

    // Method for Decryption
    String Decryption(String cipherText, int depth) throws Exception {

        int r = depth;

        // Length of cipher text
        int len = cipherText.length();

        // Number of columns
        int c = len / depth;

        // Matrix creation
        char mat[][] = new char[r][c];

        int k = 0;

        // String to store decrypted text
        String plainText = "";

        // Filling cipher text row-wise into matrix
        for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++) {
                mat[i][j] = cipherText.charAt(k++);
            }
        }

        // Reading matrix column-wise to get original text
        for (int i = 0; i < c; i++) {
            for (int j = 0; j < r; j++) {
                plainText += mat[j][i];
            }
        }

        return plainText;
    }
}

// Main Class
class RailFence {

    public static void main(String args[]) throws Exception {

        // Object creation
        RailFenceBasic rf = new RailFenceBasic();

        Scanner scn = new Scanner(System.in);

        int depth;

        String plainText, cipherText, decryptedText;

        // Accepting plain text
        System.out.println("Enter plain text:");
        plainText = scn.nextLine();

        // Accepting depth value
        System.out.println("Enter depth for Encryption:");
        depth = scn.nextInt();

        // Encryption
        cipherText = rf.Encryption(plainText, depth);

        System.out.println("Encrypted text is:");
        System.out.println(cipherText);

        // Decryption
        decryptedText = rf.Decryption(cipherText, depth);

        System.out.println("Decrypted text is:");
        System.out.println(decryptedText);
    }
}



OutPut 
:

Enter plain text:
railfencecipher

Enter depth for Encryption:
3

Encrypted text is:
rlnchafcieieepr

Decrypted text is:
railfencecipher

Post a Comment

7Comments

  1. Thanks Dude it works Successfully in java as well as in android thank you for sharing

    ReplyDelete
  2. hi, how would add an offset to this so that the user can input the starting row for the plain text to be added into the array?

    ReplyDelete
  3. Hai, Rail Fence cipher was suppose to be "zig zag" right ? why does it arrange it descending order only??

    ReplyDelete
Post a Comment