Question

Please write in Java in Jgrasp check code before posting Please write in Java in Jgrasp check code before posting

FOP- 2. Write a program that ciphers a plain text message using double transposition scheme and vice versa. [50 Marks) The pr

Please write in Java in Jgrasp check code before posting, will give thumbs up

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi, here is the solution. Try it and let me know if you need any assistance. This is working in jGRASP as well.

Source & Screens: DoubleTranspositionCipher.java
public class DoubleTranspositionCipher
{
    // private constructor.
    // prevents creating instances of class
    private DoubleTranspositionCipher()
    {
    }
    private static char[][] buildMatrix(String message, int rows, int cols)
    {
        char[][] matrix = new char[rows][cols];
        int pos;
        char c;
        for(int i = 0; i < rows; i++)
        {
            for(int j = 0; j < cols; j++)
            {
                pos = i * cols + j;
                // if matrix cell is beyond the message length
                if(pos >= message.length())
                {
                    // set as blanks
                    c = ' ';
                }
                else
                {
                    c = message.charAt(pos);
                }
                matrix[i][j] = c;
            }
        }
        return matrix;
    }
    private static String getColumnTranspositionMessage(char[][] matrix, int rows, int[] colOrder)
    {
        StringBuilder codedMessage = new StringBuilder();
        for(int i =0; i <rows; i++)
        {
            for(int j : colOrder)
            {
                codedMessage.append(matrix[i][j-1]);
            }
        }
        return codedMessage.toString();
    }
    private static String getRowTranspositionMessage(char[][] matrix, int cols, int[] rowOrder)
    {
        StringBuilder codedMessage = new StringBuilder();
        for(int i : rowOrder)
        {
            for(int j = 0 ; j < cols; j++)
            {
                codedMessage.append(matrix[i-1][j]);
            }
        }
        return codedMessage.toString();
    }
    public static String encrypt(String message, int rows, int cols, int[] rowOrder, int[] colOrder)
    {
        // build code matrix
        char [][] codeMatrix = buildMatrix(message,rows,cols);
        // perform columnar transposition and get intermediate message
        String intermediateMessage = getColumnTranspositionMessage(codeMatrix,rows,colOrder);
        // build code matrix from intermediate message
        codeMatrix = buildMatrix(intermediateMessage,rows,cols);
        // perform row transposition
        String encryptedMessage = getRowTranspositionMessage(codeMatrix,cols,rowOrder);
        // replace all blanks with x
        return encryptedMessage.replaceAll(" ","x");
    }
    public static String decrypt(String message, int rows, int cols, int[] rowOrder, int[] colOrder)
    {
        // build code matrix
        char [][] codeMatrix = buildMatrix(message,rows,cols);
        // perform row transposition
        String intermediateMessage = getRowTranspositionMessage(codeMatrix,cols,rowOrder);
        // build code matrix from intermediate message
        codeMatrix = buildMatrix(intermediateMessage,rows,cols);
        // perform columnar transposition and get intermediate message
        String encryptedMessage = getColumnTranspositionMessage(codeMatrix,rows,colOrder);
        // replace all x with blanks
        return encryptedMessage.replaceAll("x"," ");
    }
}
public class DoubleTranspositionCipher 4 5 @ // private constructor. // prevents creating instances of class private Double T34 private static String getColumn TranspositionMessage(char[][] matrix, int rows, int[] colorder), { StringBuilder codedMess57 @ 58 public static String encrypt(String message, int rows, int cols, int[] rowOrder, int[] colorder) { // build code matr
Source & Screens : CipherDriver.java
import java.util.Scanner;

public class CipherDriver
{
    public static void main(String [] args)
    {
        String message;
        int rows,cols;
        int [] rowOrder, colOrder;
        Scanner in = new Scanner(System.in);
        // encrypt
        System.out.println("ENCRYPTION");
        System.out.println("----------");
        System.out.print("Plain Text: ");
        message = in.nextLine();
        System.out.print("Matrix row size: ");
        rows = Integer.parseInt(in.nextLine());
        System.out.print("Matrix column size: ");
        cols = Integer.parseInt(in.nextLine());
        rowOrder = new int[rows];
        colOrder = new int[cols];
        System.out.print("Row permutation order: ");
        for(int i = 0 ; i < rows; i++)
        {
            rowOrder[i] = in.nextInt();
        }
        in.nextLine();
        System.out.print("Column permutation order: ");
        for(int i = 0 ; i < cols; i++)
        {
            colOrder[i] = in.nextInt();
        }
        in.nextLine();
        System.out.println("Encrypted text: "+
                DoubleTranspositionCipher.encrypt(message,rows,cols,rowOrder,colOrder));
        // decrypt
        System.out.println("\n\nDECRYPTION");
        System.out.println("----------");
        System.out.print("Encrypted text: ");
        message = in.nextLine();
        System.out.print("Matrix row size: ");
        rows = Integer.parseInt(in.nextLine());
        System.out.print("Matrix column size: ");
        cols = Integer.parseInt(in.nextLine());
        rowOrder = new int[rows];
        colOrder = new int[cols];
        System.out.print("Row permutation order: ");
        for(int i = 0 ; i < rows; i++)
        {
            rowOrder[i] = in.nextInt();
        }
        in.nextLine();
        System.out.print("Column permutation order: ");
        for(int i = 0 ; i < cols; i++)
        {
            colOrder[i] = in.nextInt();
        }
        in.nextLine();
        System.out.println("Plain text: "+
                DoubleTranspositionCipher.decrypt(message,rows,cols,rowOrder,colOrder));
    }
}
import java.util.Scanner; public class CipherDriver public static void main(String [] args) String message; int rows, cols; iSystem.out.println(Encrypted text: + DoubleTranspositionCipher.encrypt(message, rows, cols, rowOrder, colorder)); // decryp
ENCRYPTION Plain Text: attack at dawn Matrix row size: 5 Matrix column size: 3 Row permutation order: 35 1 4 2 Column permuta

Screenshots from jGRASP

Threads Call Stack 1] CipherDriver.main (CipherDriver.java: 10) pc = 0 Scanner in = new Scanner(System.in); 77 encrypt System

Add a comment
Know the answer?
Add Answer to:
Please write in Java in Jgrasp check code before posting Please write in Java in Jgrasp...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • write a python program that ciphers a plain text message using double transposition scheme and vise...

    write a python program that ciphers a plain text message using double transposition scheme and vise versa. The program should accept five tuples of input: plain text, row, column, permutation of row and columns. Pain Text: attack at dawn Matrix Row Size: 5 Matrix Column Size: 3 Row Permutation Order: 3,5,1,4,2 Column permutation order: 1,3,2 Encrypted Text:

  • PLEASE GIVE C++ CODE OF THE 2nd and 3rd Question. PLEASE CHECK YOUR CODE BEFORE POSTING...

    PLEASE GIVE C++ CODE OF THE 2nd and 3rd Question. PLEASE CHECK YOUR CODE BEFORE POSTING IT I WILL UPVOTE IF THE ANSWER IS CORRECT! 2. Consider the statement double ans = 20.0/multiply(2+3) For each of the below mentioned four versions of the function macro multiply Obelow. write the corresponding value of ans variable. a. #define multiply (x) x*x b. #define multiply (x) (x*x) c. #define multiply (x) (x)*(x) d. #define multiply (x) ((x)*(x)) 3. Considerint val=0xFACE; Write expressions using...

  • Please help me write this Java program. I had posted this question before, but got an...

    Please help me write this Java program. I had posted this question before, but got an answer that was totally wrong. We are using the latest version of Java8. Thank You! -------------------------------------------------------------------------------------- Write a Java program that can successfully DECRYPT a string inputted by the user which has been encrypted using a Caesar Cipher with a unknown shift value(key). You can use brute force to do a for loop through all the 26 shift values, however, your program should only...

  • Please help! We use Java JGrasp. Write a Java program to take the input of 5...

    Please help! We use Java JGrasp. Write a Java program to take the input of 5 numbers and output the mean (average) and standard deviation of the numbers. If the numbers are x1, x2, x3, x4, and x5, the formula for mean is X = (x1 + x2 + x3 + x4 + x5)/5 And the formula for standard deviation is You can also break standard deviation down like this: 1. Work out the Mean (the simple average of the...

  • I want to change this C code so that instead of prompting the user to enter...

    I want to change this C code so that instead of prompting the user to enter the argument value, or requiring the argument be entered after by pressing the “enter” key, instead I want the code changed so that the program opens a file, (which include the input values) read in the elements and pass in the input file as a command line argument. (the input file includes the size of the matrix and the matrix values) Here's an example...

  • C++ Code

    "For two thousand years, codemakers have fought to preserve secrets while codebreakers have tried their best to reveal them." - taken from Code Book, The Evolution of Secrecy from Mary, Queen of Scots to Quantum Cryptography by Simon Singh.The idea for this machine problem came from this book.You will encrypt and decrypt some messages using a simplified version of a code in the book. The convention in cryptography is to write the plain text in lower case letters and the encrypted text in upper case...

  • MATLAB Write a code where the elements of a squared matrix size (order) n are equal...

    MATLAB Write a code where the elements of a squared matrix size (order) n are equal to the sum of its row and column position (I + j)

  • Write a Java program that calculates the sum of a variable sized matrix using a two...

    Write a Java program that calculates the sum of a variable sized matrix using a two dimensional array. (ArraySum.java) The user should provide the number of rows and columns Test multiple user inputs (3 times 2, 4 times 4, 6 times 2, etc) A sum should be created for each row, each column, and the total for the matrix Ex.: How big would you like your matrix? Rows - ? 3 Columns -? 2 Please enter your row 1? Column...

  • Java: makes sure program compiles before posting. Write a program that uses nested for loops to...

    Java: makes sure program compiles before posting. Write a program that uses nested for loops to create the pattern of Xs and Os, in which on every line each letter is displayed one additional space to the right. Use a toggle variable ( a boolean flag) to alternate between X and O to produce output as shown in the sample run below: Enter the number of lines: 7 X O X O X O X

  • In the following graph, write a Java program using Topological Sort. please write java code and...

    In the following graph, write a Java program using Topological Sort. please write java code and show me the output. Thank you :) b.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT