Question

If you cant answer this please dont waste my question. thank you. This cryptographic program run...

If you cant answer this please dont waste my question. thank you.

This cryptographic program run and produce text screen output. You are to create a GUI that uses the program. Your program (GUI) must allow a user to input of a message to be coded. It must also have an area to show the plaintext, the ciphertext, and the decrypted text. If required by your choice of cryptographic method, the user should have an area to input a key. You should also include instructions to the user on what and how to use your program. Remember user-friendly.

You should not have to rewrite any of the example programs just modify them to use a GUI.

Please add comments to code

import java.util.Scanner;

public class OneTimePadCipher

{

public static String encryptionMessage(String s)

{

int i, j;
int randomBitPattern[] = new int[8];

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

{

randomBitPattern[i] = (i % 2 == 0) ? 1 : 0;

}

char asc[] = new char[s.length()];

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

{

asc[i] = (char) ((int) s.charAt(i));

}

BasicOperation b1 = new BasicOperation();

String cipherText = new String("");

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

{

int temp = (int) (asc[i]);

int len = b1.decimalToBinary(temp);
int bintemp[] = new int[7];
int xorlen;

if (len == 7)

{

for (j = 1; j <= len; j++)

{

bintemp[j - 1] = b1.binaryArrayAtPosition(j);

}

// XOR Operation

xorlen = b1.xorop(bintemp, randomBitPattern, len);
}

else

{

// System.out.println("\n less than 7 :"+len);

bintemp[0] = 0;

for (j = 1; j <= len; j++)

{

bintemp[j] = b1.binaryArrayAtPosition(j);

}

// XOR Operation

xorlen = b1.xorop(bintemp, randomBitPattern, len + 1);

}

int xor[] = new int[xorlen];

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

{

xor[j] = b1.xorinArrayAt(j);

cipherText = cipherText + xor[j];

}

cipherText += " ";

}

return (cipherText);

}

public static String decryptionMessage(String s)

{

int i, j;

// char cipherChar[]=new char[(s.length()/2)];

char cipherChar[] = new char[(s.length())];

int cnt = -1;

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

{

// we receive only Ascii of it is allow 0 and 1, do not accept white

// space

// int ascii=(int)s.charAt(i);

if ((int) s.charAt(i) == 48 || (int) s.charAt(i) == 49

|| (int) s.charAt(i) == 32)

{

cnt++;

cipherChar[cnt] = s.charAt(i);

}

}

String s1 = new String(cipherChar);

String s2[] = s1.split(" ");

int data[] = new int[s2.length];

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

{

data[i] = Integer.parseInt(s2[i]);

}

char randomBitPattern[] = new char[7];

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

{

randomBitPattern[i] = (i % 2 == 0) ? '1' : '0';

}

BasicOperation b1 = new BasicOperation();

String plain = new String("");

// do the XOR Operation

for (i = 0; i < s2.length; i++)
{
int xorlen = b1.xorop(s2[i], randomBitPattern);
int xor[] = new int[xorlen];
for (j = 0; j < xorlen; j++)

{

xor[j] = b1.xorinArrayAt(j);

plain += xor[j];

}

plain += " ";
}

String p[] = plain.split(" ");

BasicOperation ob = new BasicOperation();

int decryptedChar[] = new int[p.length];

char plainTextChar[] = new char[p.length];

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

{

decryptedChar[i] = ob.binaryToDecimal(Integer.parseInt(p[i]));

plainTextChar[i] = (char) decryptedChar[i];

}

return (new String(plainTextChar));

}


public static void main(String[] args)

{

Scanner sc = new Scanner(System.in);

System.out.println("Enter the message: ");

String message = sc.next();

System.out.println("'" + message + "' in encrypted message : "

+ encryptionMessage(message));

System.out.println("'" + encryptionMessage(message)

+ "' in decrypted message : "

+ decryptionMessage(encryptionMessage(message)));

sc.close();

}

}

class BasicOperation

{

int bin[] = new int[100];

int xor[] = new int[100];

int temp1[] = new int[100];

int temp2[] = new int[100];

int len;

int xorlen;

// convert binary number to decimal number

public int binaryToDecimal(int myNum)

{

int dec = 0, no, i, n = 0;

no = myNum;

// Find total digit of no of inupted number

while (no > 0)

{

n++;

no = no / 10;

}

// Convert inputed number into decimal

no = myNum;

for (i = 0; i < n; i++)
{
int temp = no % 10;
dec = dec + temp * ((int) Math.pow(2, i));

no = no / 10;

}

return dec;

}

public int decimalToBinary(int myNum)

{

int j, i = -1, no, temp = 0;

no = myNum;

int t[] = new int[100];

while (no > 0)

{

i++;

temp = no % 2;

t[i] = temp;

no = no / 2;

}

len = (i + 1);

j = -1;

for (i = len; i >= 0; i--)

{

j++;

bin[j] = t[i];

}

return len;

}

// find the specific bit value of binary number at given position

public int binaryArrayAtPosition(int pos)

{
return bin[pos];

}

public int xorinArrayAt(int pos)

{

return xor[pos];

}

// perform the binary X-OR operation

public int xorop(int a[], int b[], int arrlen)

{

int i;

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

{

xor[i] = (a[i] == b[i]) ? 0 : 1;

}

xorlen = i;

return xorlen;

}

// perform the binary X-OR operation

public int xorop(String s, char c[])

{

int i = -1;
for (i = 0; i < s.length(); i++)

{

xor[i] = (s.charAt(i) == c[i]) ? 0 : 1;

}

xorlen = i;
return xorlen;

}

public int getLen()

{

return len + 1;

}

// display binary bit pattern or the array

public void displayBinaryArray()

{

for (int i = 0; i <= len; i++)

{

System.out.println("\n Binary Array :" + bin[i]);

}

}

}

Homework Answers

Answer #1

Output:

Program:

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class OneTimePadCipher
{
public static String encryptionMessage(String s)
{
int i, j;
int randomBitPattern[] = new int[8];
for (i = 0; i < 7; i++)
{
randomBitPattern[i] = (i % 2 == 0) ? 1 : 0;
}
char asc[] = new char[s.length()];
for (i = 0; i < s.length(); i++)
{
asc[i] = (char) ((int) s.charAt(i));
}
BasicOperation b1 = new BasicOperation();
String cipherText = new String("");
for (i = 0; i < asc.length; i++)
{
int temp = (int) (asc[i]);
int len = b1.decimalToBinary(temp);
int bintemp[] = new int[7];
int xorlen;
if (len == 7)
{
for (j = 1; j <= len; j++)
{
bintemp[j - 1] = b1.binaryArrayAtPosition(j);
}
// XOR Operation
xorlen = b1.xorop(bintemp, randomBitPattern, len);
}
else
{
// System.out.println("\n less than 7 :"+len);
bintemp[0] = 0;
for (j = 1; j <= len; j++)
{
bintemp[j] = b1.binaryArrayAtPosition(j);
}
// XOR Operation
xorlen = b1.xorop(bintemp, randomBitPattern, len + 1);
}
int xor[] = new int[xorlen];
for (j = 0; j < xorlen; j++)
{
xor[j] = b1.xorinArrayAt(j);
cipherText = cipherText + xor[j];
}
cipherText += " ";
}
return (cipherText);
}
public static String decryptionMessage(String s)
{
int i, j;
// char cipherChar[]=new char[(s.length()/2)];
char cipherChar[] = new char[(s.length())];
int cnt = -1;
for (i = 0; i < s.length(); i++)
{
// we receive only Ascii of it is allow 0 and 1, do not accept white
// space
// int ascii=(int)s.charAt(i);
if ((int) s.charAt(i) == 48 || (int) s.charAt(i) == 49 || (int) s.charAt(i) == 32)
{
cnt++;
cipherChar[cnt] = s.charAt(i);
}
}
String s1 = new String(cipherChar);
String s2[] = s1.split(" ");
int data[] = new int[s2.length];
for (i = 0; i < s2.length; i++)
{
data[i] = Integer.parseInt(s2[i]);
}
char randomBitPattern[] = new char[7];
for (i = 0; i < 7; i++)
{
randomBitPattern[i] = (i % 2 == 0) ? '1' : '0';
}
BasicOperation b1 = new BasicOperation();
String plain = new String("");
// do the XOR Operation
for (i = 0; i < s2.length; i++)
{
int xorlen = b1.xorop(s2[i], randomBitPattern);
int xor[] = new int[xorlen];
for (j = 0; j < xorlen; j++)
{
xor[j] = b1.xorinArrayAt(j);
plain += xor[j];
}
plain += " ";
}
String p[] = plain.split(" ");
BasicOperation ob = new BasicOperation();
int decryptedChar[] = new int[p.length];
char plainTextChar[] = new char[p.length];
for (i = 0; i < p.length; i++)
{
decryptedChar[i] = ob.binaryToDecimal(Integer.parseInt(p[i]));
plainTextChar[i] = (char) decryptedChar[i];
}
return (new String(plainTextChar));
}

public static void main(String[] args)
{
//create labels for show text
JFrame frame=new JFrame("cryptographic program ");
frame.setLayout(new BorderLayout());
frame.setSize(300, 400);
  
JLabel input=new JLabel("Enter the message: ");
JLabel enc=new JLabel("Encrypted message: ");
JLabel dec=new JLabel("Decrypted message: ");
//textfield to read input from user and show output
final JTextField inputMsg=new JTextField("");
final JTextField encMsg=new JTextField("");
final JTextField decMsg=new JTextField("");

//button for encryption and decryption
JButton ency=new JButton("Encrypt");
JButton decy=new JButton("Decrypt");
Panel panel=new Panel();
panel.setSize(200, 300);
panel.setLayout(new GridLayout(4,2));//set GridLayout to panel
//add elements on panel
panel.add(input);
panel.add(inputMsg);
encMsg.setEditable(false);
panel.add(enc);
panel.add(encMsg);
panel.add(dec);
panel.add(decMsg);
decMsg.setEditable(false);
//add buttons on panel
panel.add(ency);
panel.add(decy);
frame.add(panel,BorderLayout.CENTER);
frame.setResizable(false);
frame.setVisible(true);
ency.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
String message=inputMsg.getText().trim();//get text from input field
String messageEnc=encryptionMessage(message);//encrypt message
encMsg.setText(messageEnc);//set on encrypted Message field
}
});
decy.addActionListener(new ActionListener(){

@Override
public void actionPerformed(ActionEvent e) {
String message=encMsg.getText().trim();//get text from input field
String messageDec=decryptionMessage(message);//decrypt mssage
decMsg.setText(messageDec);//set on decrypted Message field
}

});
  
}
}
class BasicOperation
{
int bin[] = new int[100];
int xor[] = new int[100];
int temp1[] = new int[100];
int temp2[] = new int[100];
int len;
int xorlen;
// convert binary number to decimal number
public int binaryToDecimal(int myNum)
{
int dec = 0, no, i, n = 0;
no = myNum;
// Find total digit of no of inupted number
while (no > 0)
{
n++;
no = no / 10;
}
// Convert inputed number into decimal
no = myNum;
for (i = 0; i < n; i++)
{
int temp = no % 10;
dec = dec + temp * ((int) Math.pow(2, i));
no = no / 10;
}
return dec;
}
public int decimalToBinary(int myNum)
{
int j, i = -1, no, temp = 0;
no = myNum;
int t[] = new int[100];
while (no > 0)
{
i++;
temp = no % 2;
t[i] = temp;
no = no / 2;
}
len = (i + 1);
j = -1;
for (i = len; i >= 0; i--)
{
j++;
bin[j] = t[i];
}
return len;
}
// find the specific bit value of binary number at given position
public int binaryArrayAtPosition(int pos)
{
return bin[pos];
}
public int xorinArrayAt(int pos)
{
return xor[pos];
}
// perform the binary X-OR operation
public int xorop(int a[], int b[], int arrlen)
{
int i;
for (i = 0; i < arrlen; i++)
{
xor[i] = (a[i] == b[i]) ? 0 : 1;
}
xorlen = i;
return xorlen;
}
// perform the binary X-OR operation
public int xorop(String s, char c[])
{
int i = -1;
for (i = 0; i < s.length(); i++)
{
xor[i] = (s.charAt(i) == c[i]) ? 0 : 1;
}
xorlen = i;
return xorlen;
}
public int getLen()
{
return len + 1;
}
// display binary bit pattern or the array
public void displayBinaryArray()
{
for (int i = 0; i <= len; i++)
{
System.out.println("\n Binary Array :" + bin[i]);
}
}
}

Know the answer?
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
Similar Questions
Here is my java code, I keep getting this error and I do not know how...
Here is my java code, I keep getting this error and I do not know how to fix it: PigLatin.java:3: error: class Main is public, should be declared in a file named Main.java public class Main { ^ import java.io.*; public class Main { private static BufferedReader buf = new BufferedReader( new InputStreamReader(System.in)); public static void main(String[] args) throws IOException { String english = getString(); String translated = translate(english); System.out.println(translated); } private static String translate(String s) { String latin =...
question : Take the recursive Java program Levenshtein.java and convert it to the equivalent C program....
question : Take the recursive Java program Levenshtein.java and convert it to the equivalent C program. Tip: You have explicit permission to copy/paste the main method for this question, replacing instances of System.out.println with printf, where appropriate. You can get the assert function from assert.h. Try running the Java program on the CS macOS machines. You can compile and run the program following the instructions discussed in class. Assertions are disabled by default. You can enable assertions by running java...
Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as...
Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as possible: What is a checked exception, and what is an unchecked exception? What is NullPointerException? Which of the following statements (if any) will throw an exception? If no exception is thrown, what is the output? 1: System.out.println( 1 / 0 ); 2: System.out.println( 1.0 / 0 ); Point out the problem in the following code. Does the code throw any exceptions? 1: long value...
[Java] I'm not sure how to implement the code. Please check my code at the bottom....
[Java] I'm not sure how to implement the code. Please check my code at the bottom. In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester. public static int min(int[] array) gets the minimum value in the array public...
This assignment is an individual assignment. For Questions 1-3: consider the following code: public class A...
This assignment is an individual assignment. For Questions 1-3: consider the following code: public class A { private int number; protected String name; public double price; public A() { System.out.println(“A() called”); } private void foo1() { System.out.println(“A version of foo1() called”); } protected int foo2() { Sysem.out.println(“A version of foo2() called); return number; } public String foo3() { System.out.println(“A version of foo3() called”); Return “Hi”; } }//end class A public class B extends A { private char service; public B()...
Please provide answer in the format that I provided, thank you Write a program that prompts...
Please provide answer in the format that I provided, thank you Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must...
Question: I am using three different ways to execute this program. I am a bit confused...
Question: I am using three different ways to execute this program. I am a bit confused on the time complexity for each one. Can someone explain the time complexity for each function in relation to the nanoseconds. import java.util.HashMap; import java.util.Map; public class twosums {       public static void main(String args[]) {               int[] numbers = new int[] {2,3,9,4,8};;        int target = 10;               long nano_begin1 = System.nanoTime();        int[]...
can you please do this lab? use lunix or C program its a continuation of a...
can you please do this lab? use lunix or C program its a continuation of a previous lab. the previous lab: Unix lab 4: compile and link multiple c or c++ files Please do the following tasks step by step: create a new directory named by inlab4 enter directory inlab4 create a new file named by reverse.c with the following contents and then close the file: /*reverse.c */ #include <stdio.h> reverse(char *before, char *after); main() {       char str[100];    /*Buffer...
8.15 *zyLab: Method Library (Required & Human Graded) This code works but there are some problems...
8.15 *zyLab: Method Library (Required & Human Graded) This code works but there are some problems that need to be corrected. Your task is to complete it to course style and documentation standards CS 200 Style Guide. This project will be human graded. This class contains a set of methods. The main method contains some examples of using the methods. Figure out what each method does and style and document it appropriately. The display method is done for you and...
please use linux/unix command terminal to complete, step 1 1-create a new directory named by inlab4...
please use linux/unix command terminal to complete, step 1 1-create a new directory named by inlab4 enter directory inlab4 create a new file named by reverse.c with the following contents and then close the file: /*reverse.c */ #include <stdio.h> reverse(char *before, char *after); main() { char str[100]; /*Buffer to hold reversed string */ reverse("cat", str); /*Reverse the string "cat" */ printf("reverse(\"cat\")=%s\n", str); /*Display result */ reverse("noon", str); /*Reverse the string "noon" */ printf("reverse(\"noon\")=%s\n", str); /*Display result */ } reverse(char *before,...