Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using Java programming language. Description: The purposes of this assignment ar

ID: 3668999 • Letter: U

Question

Using Java programming language.

Description: The purposes of this assignment are:

Write methods that use parameters

Work with Strings

Work with user input

In this assignment you will write a program that performs encryption on a message from a user using a columnar transposition cipher. Then the program will decrypt a message from the user using a columnar transposition cipher.

Your program must reproduce this output exactly given the same input.

The program performs the following steps:

Prints an introduction

Asks the user for a clear text message.

Shows the encrypted text based on the clear text message. The program performs the encryption with various numbers of rows starting at 2 and going up to some maximum based on the class constant MAX_ROWS. The MAX_ROWS class constant must be set to 10 in the version of the program you submit. The program shows the encrypted message both with and without padding.

Next the program asks the user for an encrypted message. The message is then decoded with various numbers of rows. The decryption is done assuming the message was encrypted with padding characters if necessary. If the length of the message is not a multiple of the number of rows padding characters (X's) are added to the end. Note, adding padding characters will throw off the decryption in some cases.

As usual develop the program in steps. Design one method at a time, implement it, and test it before moving on to the next portion of the program.

Use parameters to send information from one method to another as necessary. Do not use class variables as a form of "global variables". Doing so is extremely poor design and will cause you to lose points.

When encrypting the message you will not actually create the table shown in the example above. Instead you will use nested loops to generate the necessary indices in the String. I think it is easiest to generate the indices of the first row, then the second row, and so forth. Work this out on paper with various messages and various numbers of rows to find the necessary pattern for the for loops.

The methods you will need from the String class are the length() and charAt() methods. You will also use String concatenation and an accumulator pattern.

If you program does not work a good debugging technique is to print out the current loop control variables and the current version of the encrypted or decrypted String.

Explanation / Answer

public class TranspositionCipher

{

    public static String selectedKey;

    public static char   sortedKey[];

    public static int    sortedKeyPos[];



    // default constructor define the default key

    public TranspositionCipher()

    {

        selectedKey = "megabuck";

        sortedKeyPos = new int[selectedKey.length()];

        sortedKey = selectedKey.toCharArray();

    }



    // Parameterized constructor define the custom key

    public TranspositionCipher(String myKey)

    {

        selectedKey = myKey;

        sortedKeyPos = new int[selectedKey.length()];

        sortedKey = selectedKey.toCharArray();

    }



    // To reorder data do the sorting on selected key

    public static void doProcessOnKey()

    {

        // Find position of each character in selected key and arrange it on

        // alphabetical order

        int min, i, j;

        char orginalKey[] = selectedKey.toCharArray();

        char temp;

        // First Sort the array of selected key

        for (i = 0; i < selectedKey.length(); i++)

        {

            min = i;

            for (j = i; j < selectedKey.length(); j++)

            {

                if (sortedKey[min] > sortedKey[j])

                {

                    min = j;

                }

            }

            if (min != i)

            {

                temp = sortedKey[i];

                sortedKey[i] = sortedKey[min];

                sortedKey[min] = temp;

            }

        }

        // Fill the position of array according to alphabetical order

        for (i = 0; i < selectedKey.length(); i++)

        {

            for (j = 0; j < selectedKey.length(); j++)

            {

                if (orginalKey[i] == sortedKey[j])

                    sortedKeyPos[i] = j;

            }

        }

    }



    // to encrypt the targeted string

    public static String doEncryption(String plainText)

    {

        int min, i, j;

        char orginalKey[] = selectedKey.toCharArray();

        char temp;

        doProcessOnKey();

        // Generate encrypted message by doing encryption using Transpotion

        // Cipher

        int row = plainText.length() / selectedKey.length();

        int extrabit = plainText.length() % selectedKey.length();

        int exrow = (extrabit == 0) ? 0 : 1;

        int rowtemp = -1, coltemp = -1;

        int totallen = (row + exrow) * selectedKey.length();

        char pmat[][] = new char[(row + exrow)][(selectedKey.length())];

        char encry[] = new char[totallen];

        int tempcnt = -1;

        row = 0;

        for (i = 0; i < totallen; i++)

        {

            coltemp++;

            if (i < plainText.length())

            {

                if (coltemp == (selectedKey.length()))

                {

                    row++;

                    coltemp = 0;

                }

                pmat[row][coltemp] = plainText.charAt(i);

            }

            else

            { // do the padding ...

                pmat[row][coltemp] = '*';

            }

        }

        int len = -1, k;

        for (i = 0; i < selectedKey.length(); i++)

        {

            for (k = 0; k < selectedKey.length(); k++)

            {

                if (i == sortedKeyPos[k])

                {

                    break;

                }

            }

            for (j = 0; j <= row; j++)

            {

                len++;

                encry[len] = pmat[j][k];

            }

        }

        String p1 = new String(encry);

        return (new String(p1));

    }



    // to decrypt the targeted string

    public static String doDecryption(String s)

    {

        int min, i, j, k;

        char key[] = selectedKey.toCharArray();

        char encry[] = s.toCharArray();

        char temp;

        doProcessOnKey();

        // Now generating plain message

        int row = s.length() / selectedKey.length();

        char pmat[][] = new char[row][(selectedKey.length())];

        int tempcnt = -1;

        for (i = 0; i < selectedKey.length(); i++)

        {

            for (k = 0; k < selectedKey.length(); k++)

            {

                if (i == sortedKeyPos[k])

                {

                    break;

                }

            }

            for (j = 0; j < row; j++)

            {

                tempcnt++;

                pmat[j][k] = encry[tempcnt];

            }

        }

        // store matrix character in to a single string

        char p1[] = new char[row * selectedKey.length()];

        k = 0;

        for (i = 0; i < row; i++)

        {

            for (j = 0; j < selectedKey.length(); j++)

            {

                if (pmat[i][j] != '*')

                {

                    p1[k++] = pmat[i][j];

                }

            }

        }

        p1[k++] = '';

        return (new String(p1));

    }



    @SuppressWarnings("static-access")

    public static void main(String[] args)

    {

        TranspositionCipher tc = new TranspositionCipher();

        System.out.println("Encrypted Message is: "

                + tc.doEncryption("Sanfoundry"));

        System.out.println("Decrypted Message is: "

                + tc.doDecryption(tc.doEncryption("Sanfoundry")));

    }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote