Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

Cryptography General Write a Java program to encrypt and decrypt a phrase using two similar approaches, each insecure by modern standards. The first approach is

Cryptography

General

Write a Java program to encrypt and decrypt a phrase using two similar approaches, each insecure by modern standards. The first approach is called the Caesar Cipher, and is a simple substitution cipher where characters in a message are replaced by a substitute character. The substitution is done according to an integer key which specifies the offset of the substituting characters. For example, the string ABC with a key of 3 would be replaced by DEF.

If the key is greater than the range of characters we want to consider, we wrap around by subtracting the range from the key until the key is within the desired range. For example, if we have a range from to _ (i.e., ASCII 32 to ASCII 95), and the key is 120, we note that 120 is outside the range. So we subtract 95-32=63 from 120, giving 57, which in ASCII is the character 9. If the key is even higher, we can subtract the range from the key over and over until the key is within the desired range.

Since our specified range does not include lower-case letters, the GUI (provided) will change strings to upper case. You can find the ASCII table at http://www.asciitable.com/, or may other places on the Internet.

The second approach, due to Giovan Battista Bellaso (b 1505, d 1581), uses a key word, where each character in the word specifies the offset for the corresponding character in the message, with the key word wrapping around as needed.

So for the string ABCDEFG and the key word CMSC, the key word is first extended to the length of the string, i.e., CMSCCMS. Then A is replaced by A offset by C, i.e., ASCII 65+67=132. The range of the characters is also specified, and again well say to _ (i.e., ASCII 32 to ASCII 95 + 1). The range is then 95-32+1=64. In our case, the offset is wrapped by reducing 132 by the range until it is the allowable range. 132 is adjusted to 132-64=68, or character D in the encrypted phase. Then the same logic is applied to the second letter of the plain text B shifted by the second letter of the key word M. This results in the character O as the second letter in the encrypted phase, and so on. In each approach, if the resulting integer is greater than 95 (the top of our range), the integer is wrapped around so that it stays within the specified range. The result is DOVGHSZ.

Your program will implement several methods that are specified in the file CryptoManager.java. A Graphical User Interface is provided, as well as a test file, that you should use to make sure your methods work correctly. Be sure to follow the naming exactly, as the tests will not work otherwise.

There are several features of Java that we have not yet covered in class. Just follow the syntax specified in this document and in the file CryptoManager.java. First, the required methods are static, which just means that they are available from the class even if an instance has not been created. To call a static method, for example, public static void myMethod(); the syntax is CryptoManager.myMethod();. Another feature that may be useful in this project is the method charAt(i) on a string, which returns a character at position i of a string (zero-based). So thisString.charAt(3); would return the char s.

It is strongly suggested that you implement the project in the order stated below, so that one set of work will build on another.

Specifications

.

Design

{C} Turn in pseudo-code for each of the methods specified in CryptoManager.java. Your pseudo-code should be part-way between English and java. There is no need to spell out all the details of variable declaration, etc., but by the same token, the pseudo-code needs to have enough detail that a competent Java programmer could implement it.

{C} Turn in a test table with at least two tests for the Caesar Cipher and two for the Bellaso Cipher. Also, include at least one string that will fail because it has characters outside the acceptable ones.

{C} The design documents are due in one week.

Data Manager CryptoManager.java

{C} Implement each of the methods specified in this file. The version provided will print error messages in the console, because they are just the skeletons.

{C} Each of the methods are static, so there is no need to create an instance of the Data Manager.

{C} The methods are described below.

{C} public static boolean stringInBounds (String plainText);

This method determines if a string is within the allowable bounds of ASCII codes according to the LOWER_BOUND and UPPER_BOUND characters. The parameter plainText is the string to be encrypted. The method returns true if all characters are within the allowable bounds, false if any character is outside.

{C} public static String encryptCaesar(String plainText, int key); E

This method encrypts a string according to the Caesar Cipher. The integer key specifies an offset and each character in plainText is replaced by the character the specified distance away from it. The parameter plainText is an uppercase string to be encrypted. The parameter key is an integer that specifies the offset of each character. The method returns the encrypted string.

{C} public static String decryptCaesar(String encryptedText, int key);

This method decrypts a string according to the Caesar Cipher. The integer key specifies an offset and each character in encryptedText is replaced by the character "offset" characters before it. This is the inverse of the encryptCaesar method. The parameter encryptedText is the encrypted string to be decrypted, and key is the integer used to encrypt the original text. The method returns the original plain text string.

{C} public static String encryptBellaso(String plainText, String bellasoStr);

This method encrypts a string according to the Bellaso Cipher. Each character in plainText is offset according to the ASCII value of the corresponding character in bellasoStr, which is repeated to correspond to the length of plaintext. The method returns the encrypted string.

{C} public static String decryptBellaso(String encryptedText, String bellasoStr);

This method decrypts a string according to the Bellaso Cipher. Each character in encryptedText is replaced by the character corresponding to the character in bellasoStr, which is repeated to correspond to the length of plainText. This is the inverse of the encryptBellaso method. The parameter encryptedText is the encrypted string to be decrypted, and bellasoStr is the string used to encrypt the original text. The method returns the original plain text string.

{C} Add additional methods if you wish to make your logic easier to follow.

GUI Driver

{C} A Graphical User Interface (GUI) is provided. Be sure that the GUI will compile and run with your methods. The GUI will not compile if your methods in CryptoManager.java are not exactly in the format specified. When you first run the application, your methods will all throw exceptions, which will be caught by the GUI and printed out in the console.

{C} Do not modify the GUI.

{C} The GUI takes care of capitalizing your input strings.

Testing

{C} Once your methods are implemented, run the test driver. Ensure that the driver file results in the following output:

"THIS TEST SHOULD SUCCEED" Is it in bounds? true

"THIS TEST THAT SHOULD FAIL BECAUSE { IS OUTSIDE THE RANGE" Is it in bounds? false

"This test should fail because of lowercase letters" Is it in bounds? false

Caesar cipher of "THIS IS ANOTHER TEST" should return "WKLV#LV#DQRWKHU#WHVW": WKLV#LV#DQRWKHU#WHVW

Bellaso cipher of "THIS IS ANOTHER TEST" should return "WU\VR9F#N!RF88U-'HED": WU\VR9F#N!RF88U-'HED

Caesar decryption of "WKLV#LV#DQRWKHU#WHVW" should return "THIS IS ANOTHER TEST": THIS IS ANOTHER TEST

Bellaso decryption of "WU\VR9F#N!RF88U-'HED" should return "THIS IS ANOTHER TEST": THIS IS ANOTHER TEST

{C} Include CryptoTest.java with the rest of your submission.

Code list::

public class CryptoManager {

private static final char LOWER_BOUND = ' ';

private static final char UPPER_BOUND = '_';

private static final int RANGE = UPPER_BOUND - LOWER_BOUND + 1;

/**

* This method determines if a string is within the allowable bounds of ASCII codes

* according to the LOWER_BOUND and UPPER_BOUND characters

* @param plainText a string to be encrypted, if it is within the allowable bounds

* @return true if all characters are within the allowable bounds, false if any character is outside

*/

public static boolean stringInBounds (String plainText) {

throw new RuntimeException("method not implemented");

}

/**

* Encrypts a string according to the Caesar Cipher. The integer key specifies an offset

* and each character in plainText is replaced by the character \"offset\" away from it

* @param plainText an uppercase string to be encrypted.

* @param key an integer that specifies the offset of each character

* @return the encrypted string

*/

public static String encryptCaesar(String plainText, int key) {

throw new RuntimeException("method not implemented");

}

/**

* Encrypts a string according the Bellaso Cipher. Each character in plainText is offset

* according to the ASCII value of the corresponding character in bellasoStr, which is repeated

* to correspond to the length of plainText

* @param plainText an uppercase string to be encrypted.

* @param bellasoStr an uppercase string that specifies the offsets, character by character.

* @return the encrypted string

*/

public static String encryptBellaso(String plainText, String bellasoStr) {

throw new RuntimeException("method not implemented");

}

/**

* Decrypts a string according to the Caesar Cipher. The integer key specifies an offset

* and each character in encryptedText is replaced by the character \"offset\" characters before it.

* This is the inverse of the encryptCaesar method.

* @param encryptedText an encrypted string to be decrypted.

* @param key an integer that specifies the offset of each character

* @return the plain text string

*/

public static String decryptCaesar(String encryptedText, int key) {

throw new RuntimeException("method not implemented");

}

/**

* Decrypts a string according the Bellaso Cipher. Each character in encryptedText is replaced by

* the character corresponding to the character in bellasoStr, which is repeated

* to correspond to the length of plainText. This is the inverse of the encryptBellaso method.

* @param encryptedText an uppercase string to be encrypted.

* @param bellasoStr an uppercase string that specifies the offsets, character by character.

* @return the decrypted string

*/

public static String decryptBellaso(String encryptedText, String bellasoStr) {

throw new RuntimeException("method not implemented");

}

}

public class CryptoTest {

public static void main(String[] args) {

String str1 = "\"THIS TEST SHOULD SUCCEED\"";

String str2 = "\"THIS TEST THAT SHOULD FAIL BECAUSE { IS OUTSIDE THE RANGE\"";

String str3 = "\"This test should fail because of lowercase letters\"";

String str4 = "THIS IS ANOTHER TEST";

String bellasoStr = "CMSC203";

String str5 = "WKLV#LV#DQRWKHU#WHVW";

String str6 = "WU\\VR9F#N!RF88U-'HED";

boolean good = CryptoManager.stringInBounds(str1);

boolean bad = CryptoManager.stringInBounds(str2);

System.out.println(str1+" Is it in bounds? "+good);

System.out.println(str2+" Is it in bounds? "+bad);

bad = CryptoManager.stringInBounds(str3);

System.out.println(str3+" Is it in bounds? "+bad);

System.out.println("Caesar cipher of \""+str4+"\" should return \"WKLV#LV#DQRWKHU#WHVW\": "+ CryptoManager.encryptCaesar(str4, 3));

System.out.println("Bellaso cipher of \""+str4+"\" should return \"WU\\VR9F#N!RF88U-'HED\": "+ CryptoManager.encryptBellaso(str4, bellasoStr));

System.out.println("Caesar decryption of \""+str5+"\" should return \"THIS IS ANOTHER TEST\": "+ CryptoManager.decryptCaesar(str5, 3));

System.out.println("Bellaso decryption of \""+str6+"\" should return \"THIS IS ANOTHER TEST\": "+ CryptoManager.decryptBellaso(str6, bellasoStr));

}

}

import java.io.IOException;

import javafx.application.Application;

//import javafx.application.Application;

import javafx.application.Platform;

import javafx.event.ActionEvent;

import javafx.event.EventHandler;

import javafx.scene.Scene;

import javafx.scene.control.Button;

import javafx.scene.control.Tooltip;

import javafx.scene.layout.BorderPane;

import javafx.scene.layout.Pane;

import javafx.scene.layout.StackPane;

import javafx.stage.Stage;

public class FXDriver extends Application {

/**

* The main method for the GUI example program JavaFX version

* @param args not used

* @throws IOException

*/

public static void main(String[] args) {

launch(args);

}

@Override

public void start(Stage stage) throws Exception {

//call the main scene which is a BorderPane

FXMainPane root = new FXMainPane();

stage.setScene(new Scene(root, 600, 350));

// Set stage title and show the stage.

stage.setTitle("Cybersecurity Encryption and Decryption");

stage.show();

}

}

import javafx.application.Platform;

import javafx.event.ActionEvent;

import javafx.event.EventHandler;

import javafx.geometry.Insets;

import javafx.geometry.Pos;

import javafx.scene.control.Button;

import javafx.scene.control.Label;

import javafx.scene.control.RadioButton;

import javafx.scene.control.TextField;

import javafx.scene.control.ToggleGroup;

import javafx.scene.control.Tooltip;

import javafx.scene.layout.BorderPane;

import javafx.scene.layout.HBox;

import javafx.scene.layout.VBox;

public class FXMainPane extends BorderPane {

private Button decryption, exitButton, encryption, test, clearButton;

private TextField plainTextTextField, inputForEncryptionTextField, encryptedStringTextField3, decryptedTextField4;

private Label plainTextLabel, descriptionForInputLabel, encryptedLabel3, decriptedLabel4, blankLabel1, blankLabel2, blankLabel3, blankLabel4;

private RadioButton radioButton1, radioButton2;

private int shiftInt = 0;

FXMainPane() {

Insets inset = new Insets(10); //for setting margins

plainTextTextField = new TextField();

plainTextLabel = new Label("Enter plain-text string to encrypt");

inputForEncryptionTextField = new TextField();

descriptionForInputLabel = new Label("Cyber Key - enter an integer for Caesar Cipher");

encryptedStringTextField3 = new TextField();

encryptedLabel3 = new Label("Encrypted string");

decryptedTextField4 = new TextField();

decriptedLabel4 = new Label("Decrypted string");

blankLabel1 = new Label(" ");

blankLabel2 = new Label(" ");

blankLabel3 = new Label(" ");

blankLabel4 = new Label(" ");

//create three radio button instances

radioButton1 = new RadioButton("Use Caesar cipher");

radioButton2 = new RadioButton("Use Bellaso cipher");

//create a group to make the radio buttons mutually exclusive

ToggleGroup radioButtonGroup = new ToggleGroup();

radioButton1.setToggleGroup(radioButtonGroup);

radioButton2.setToggleGroup(radioButtonGroup);

radioButton1.setSelected(true);

RadioButtonListener radioButtonListener = new RadioButtonListener();

radioButton1.setOnAction(radioButtonListener);

radioButton2.setOnAction(radioButtonListener);

radioButton1.setAlignment(Pos.CENTER);

radioButton2.setAlignment(Pos.CENTER);

HBox topBox = new HBox();

HBox.setMargin(radioButton1, inset);

topBox.setAlignment(Pos.CENTER);

topBox.getChildren().addAll(radioButton1, radioButton2);

topBox.setStyle("-fx-border-color: gray;");

//create the leftPanel

VBox centerBox = new VBox(10);

centerBox.getChildren().addAll(plainTextLabel, plainTextTextField, encryptedLabel3, encryptedStringTextField3,

decriptedLabel4, decryptedTextField4, descriptionForInputLabel, inputForEncryptionTextField);

setCenter(centerBox);

setRight(blankLabel1);

setLeft(blankLabel2);

setTop(topBox);

//create the exit Button

exitButton = new Button("E_xit");

//_ in label specifies that the next character is the mnemonic, ie, type Alt-m as a shortcut

//this activates the mnemonic on exitButton when the Alt key is pressed

exitButton.setMnemonicParsing(true);

exitButton.setTooltip(new Tooltip("Select to close the application"));

//use a lambda expression for the EventHandler class for exitButton

exitButton.setOnAction(

event -> {

Platform.exit();

System.exit(0);

}

);

//create the clear Button

clearButton = new Button("_Clear");

clearButton.setMnemonicParsing(true);

clearButton.setTooltip(new Tooltip("Select this to clear the text fields"));

//create a listener for the other button using a lambda expression

clearButton.setOnAction(event -> {

System.out.println("...clearing text fields...");

plainTextTextField.setText("");

inputForEncryptionTextField.setText("");

encryptedStringTextField3.setText("");

decryptedTextField4.setText("");

});

//create the decryption Button

decryption = new Button("_Decrypt a string");

decryption.setMnemonicParsing(true);

decryption.setTooltip(new Tooltip("Select this to decrypt a string"));

//create a listener for the other button using a lambda expression

decryption.setOnAction(event -> {

String encryptedText = "";

String bellasoStr = "";

String decryptedText;

System.out.println("...decrypting...");

encryptedText = encryptedStringTextField3.getText();

if (radioButton1.isSelected()) {

shiftInt = Integer.parseInt(inputForEncryptionTextField.getText());

decryptedText = CryptoManager.decryptCaesar(encryptedText, shiftInt);

}

else {

bellasoStr = inputForEncryptionTextField.getText().toUpperCase();

inputForEncryptionTextField.setText(bellasoStr);

decryptedText = CryptoManager.decryptBellaso(encryptedText, bellasoStr);

}

decryptedTextField4.setText(decryptedText);

});

//create the encryption Button

encryption = new Button("Encrypt a string");

encryption.setMnemonicParsing(true);

encryption.setTooltip(new Tooltip("Encrypt the string in the upper textfield"));

encryption.setVisible(true);

//create a listener for the exit button using a lambda expression

encryption.setOnAction(event -> {

try {

System.out.println("...encrypting...");

String bellasoStr = "";

String encryptedStr = "";

if (radioButton1.isSelected()) {

shiftInt = Integer.parseInt(inputForEncryptionTextField.getText());

encryptedStr = CryptoManager.encryptCaesar(plainTextTextField.getText().toUpperCase(), shiftInt);

}

else {

bellasoStr = inputForEncryptionTextField.getText().toUpperCase();

inputForEncryptionTextField.setText(bellasoStr);

encryptedStr = CryptoManager.encryptBellaso(plainTextTextField.getText().toUpperCase(), bellasoStr);

}

plainTextTextField.setText(plainTextTextField.getText().toUpperCase());

if (encryptedStr.equals(""))

encryptedStringTextField3.setText("encryption failed");

else

encryptedStringTextField3.setText(encryptedStr);

} catch (NumberFormatException e) {

System.out.println(inputForEncryptionTextField.getText()+" should be an integer");

}

});

//create the encryption Button

test = new Button("Test toStudent File");

test.setMnemonicParsing(true);

test.setTooltip(new Tooltip("Test the toStudent java file"));

test.setVisible(true);

//create a listener for the exit button using a lambda expression

test.setOnAction(event -> {

System.out.println("...testing...");

try {

CryptoManager.stringInBounds("TEST");

} catch (RuntimeException e) {

System.out.println("stringInBounds "+e.getMessage());

}try {

CryptoManager.encryptCaesar("TEST", 3);

} catch (RuntimeException e) {

System.out.println("encryptCaesar "+e.getMessage());

}

try {

CryptoManager.encryptBellaso("TEST", "CMSC");

} catch (RuntimeException e) {

System.out.println("encryptBellaso "+e.getMessage());

}

try {

CryptoManager.decryptCaesar("TEST", 3);

} catch (RuntimeException e) {

System.out.println("decryptCaesar "+e.getMessage());

}

try {

CryptoManager.decryptBellaso("TEST", "CMSC");

} catch (RuntimeException e) {

System.out.println("decryptBellaso "+e.getMessage());

}

});

HBox bottomBox = new HBox();

//HBox.setMargin(breakEncryption, inset);

HBox.setMargin(decryption, inset);

HBox.setMargin(encryption, inset);

HBox.setMargin(clearButton, inset);

HBox.setMargin(exitButton, inset);

//bottomBox.getChildren().addAll(encryption, decryption, clearButton, test, exitButton); //

bottomBox.getChildren().addAll(encryption, decryption, clearButton, exitButton);

setBottom(bottomBox);

bottomBox.setAlignment(Pos.CENTER);

}

class RadioButtonListener implements EventHandler

{

@Override

public void handle(ActionEvent event) {

Object source = event.getTarget();

if (source == radioButton1)

{

descriptionForInputLabel.setText("Cyber Key - enter an integer for Caesar Cipher");

}

else if(source == radioButton2)

{

descriptionForInputLabel.setText("Cyber Key - enter a string for Bellaso Cipher");

}

}

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions