Question

In: Computer Science

Write a Java program to encrypt and decrypt a phrase using two similar approaches, each insecure...

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 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.


⦁   Using loops
⦁   String and character processing
⦁   ASCII codes


⦁   Data Manager class – CryptoManager.java
⦁   Implement each of the methods specified in this file. This version as provided will print error messages in the console, because they are just the skeletons.
⦁   Each of the methods are static, so there is no need to create an instance of the Data Manager.
⦁   Document each of your methods with a simple description and document the class with a simple description and your name using in-line comments (//…). (Just a short sentence fragment will suffice for each documentation string.)
⦁   The methods are described below.
⦁   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.
⦁   public static String encryptCaesar(String plainText, int key);
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.
⦁   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.
⦁   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.
⦁   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.
⦁   Add additional methods if you wish to make your logic easier to follow.
⦁  
⦁   GUI Driver class – (provided)
⦁   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 method headers 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.
⦁   Do not modify the GUI.
⦁   The GUI takes care of capitalizing your input strings.
⦁  
⦁   JUnit Test/Test Driver
⦁   Once your methods are implemented, run the test driver. Ensure that the driver file results in the following output, as shown in RED (of course the output will not be in red when you run the test driver):
⦁   "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
⦁   Run the JUnit test file (provided). Ensure that the JUnit tests all succeed.
⦁   Include CryptoGFATest.java and CryptoManagerTest.java with the rest of your submission.

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 space (‘ ‘) 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+1=64 from 120, giving 56, which in ASCII is the character ‘8’. 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 many 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 we’ll say ‘ ‘ to ‘_’ (i.e., ASCII 32 to ASCII 95). The range is then 95-32+1=64. In our example, 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’.
Notes:
⦁   Proper naming conventions: All constants, except 0 and 1, should be named. Constant names should be all upper-case, variable names should begin in lower case, but subsequent words should be in title case. Variable and method names should be descriptive of the role of the variable or method. Single letter names should be avoided.
Indentation: It must be consistent throughout the program and must reflect the control structure

Solutions

Expert Solution

PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR YOU

class CryptoManager {
  static int LOWER_BOUND = 32;

  static int UPPER_BOUND = 95;

  public static boolean stringInBounds(String plainText) {
    boolean flag = true;
    for (int i = 0; i < plainText.length(); i++) {
      if (
        !(
          (int) plainText.charAt(i) >= LOWER_BOUND &&
          (int) plainText.charAt(i) <= UPPER_BOUND
        )
      ) { //false if any character is outside the bounds
        flag = false;

        break;
      }
    }

    //returns true if all characters are within the allowable bounds

    return flag;
  }

  public static String encryptCaesar(String plainText, int key) {
    //Wrap around the key, if it is greater than the UPPER_BOUND

    key = Wrap_around(key);

    //encrypted text

    String res = "";

    //encryption

    for (int i = 0; i < plainText.length(); i++) {
      res += Character.toString((char) ((int) plainText.charAt(i) + key));
    }

    //return result

    return res;
  }

  public static String decryptCaesar(String encryptedText, int key) {
    //Wrap around the key, if it is greater than the UPPER_BOUND

    key = Wrap_around(key);

    //decrypted text

    String org = "";

    //encryption

    for (int i = 0; i < encryptedText.length(); i++) {
      org += Character.toString((char) ((int) encryptedText.charAt(i) - key));
    }

    //return result

    return org;
  }

  public static int Wrap_around(int key) {
    while (key > UPPER_BOUND) {
      key -= (UPPER_BOUND - LOWER_BOUND);
    }

    return key;
  }

  public static String encryptBellaso(String plainText, String bellasoStr) {
    //encrypted text

    String res = "";

    //Adjust length of bellasoStr to plainText

    while (bellasoStr.length() < plainText.length()) {
      bellasoStr +=
        bellasoStr.substring(0, (plainText.length() - bellasoStr.length()));
    }

    //encryption

    for (int i = 0; i < plainText.length(); i++) {
      char c = (char) Wrap_around(
        (int) plainText.charAt(i) + (int) bellasoStr.charAt(i)
      );

      res += Character.toString(c);
    }

    //return result

    return res;
  }

  public static String decryptBellaso(String encryptedText, String bellasoStr) {
    //decrypted text

    String res = "";

    //Adjust length of bellasoStr to plainText

    while (bellasoStr.length() < encryptedText.length()) {
      bellasoStr +=
        bellasoStr.substring(0, (encryptedText.length() - bellasoStr.length()));
    }

    //decryption

    for (int i = 0; i < encryptedText.length(); i++) {
      char c = (char) Wrap_around(
        (int) encryptedText.charAt(i) - (int) bellasoStr.charAt(i)
      );

      res += Character.toString(c);
    }

    //return result

    return res;
  }
}

Related Solutions

how to write program in java for encrypt and decrypt input text using DH algorithm
how to write program in java for encrypt and decrypt input text using DH algorithm
Write a small program to encrypt and decrypt a message using Python library.
Write a small program to encrypt and decrypt a message using Python library.
Write a program in javascript to encrypt and decrypt the user input using the caesar algorithm...
Write a program in javascript to encrypt and decrypt the user input using the caesar algorithm with the substitution algorithm. Specify the min and max of the message user can enter to encrypt. Specify the length of the key user can enter to encrypt and decrypt the message. document your design by words or diagrams.
Write a Java program to encrypt the following message using the RC4 cipher using key CODES:...
Write a Java program to encrypt the following message using the RC4 cipher using key CODES: Cryptography is a method of protecting information and communications through the use of codes so that only those for whom the information is intended can read and process it. Instead of using stream length 256, we will use length 26. When encrypting, let A = 0 to Z = 25 (hence CODES = [2 14 3 4 18]). Ignore spaces and punctuations and put...
Write a Java program that uses the RC4 cipher algorithm to encrypt the following message using...
Write a Java program that uses the RC4 cipher algorithm to encrypt the following message using the word CODES as the key:   Cryptography is a method of protecting information and communications through the use of codes so that only those for whom the information is intended can read and process it. Instead of using stream length 256, we will use length 26. When encrypting, let A = 0 to Z = 25. Ignore spaces and punctuations and put the message...
using Dr java Objective: Write a program that takes a phrase and then counts the number...
using Dr java Objective: Write a program that takes a phrase and then counts the number of vowels (case does not matter) in the phrase. It then should display all of the vowels in sorted ascending order according to their count. Only consider {AEIOU} as the vowels. Hint: It may be a good idea to keep track and sort two arrays: Has the vowels in alphabetic order Has the number of said vowels Whenever one would swap then it swaps...
write a java program to Translate or Encrypt the given string : (input char is all...
write a java program to Translate or Encrypt the given string : (input char is all in capital letters) { 15 } *) Each character replaced by new character based on its position value in english alphabet. As A is position is 1, and Z is position 26. *) New characters will be formed after skipping the N (position value MOD 10) char forward. A->A+1= B , B->B+2=D ,C->C+3=F, .... Y->Y+(25%10)->Y+5=D A B C D E F G H I...
Hello! I'm trying to write a code that will either encrypt/decrypt a message from an inputed...
Hello! I'm trying to write a code that will either encrypt/decrypt a message from an inputed text. I'm having the absolute hardest time getting stared and figuring out how to identify in the code if the input needs to be encrypted/decrypted and identifying the string to encrypt/decrypt with. These are the instructions: Objectives Command line input File input and output Rethrowing exceptions Program Description Gaius Julius Caesar encoded his battle messages so that the opponent could not read them should...
Note: It is possible to encrypt/decrypt problems 2-5 using online calculators. For this reason, it is...
Note: It is possible to encrypt/decrypt problems 2-5 using online calculators. For this reason, it is important that sufficient work is shown in order to demonstrate your understanding of the material. Answers which simply include the final encrypted/decrypted message but do not include all necessary steps will be awarded no credit. 1. Find the following Euler Totients using Euler’s Theorem, as explained on p.409 of the text (10 points each): a.ϕ(13) b.ϕ(81) c.ϕ(100) d.ϕ(102) 2. Use a Hill Cipher to...
for eclipse java Overview Write a program that translates an English phrase into a Pig Latin...
for eclipse java Overview Write a program that translates an English phrase into a Pig Latin phrase. Input a phrase from the user and translate it to pig latin. You can assume the phrase will be on a single line. Use at least the REQUIRED METHODS listed below, with the exact same SPELLING for method names and parameters. Input Specification The input will be an English phrase, with NO terminal punctuation. Read the phrase as a SINGLE STRING using the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT