Question

This is Crypto Manager blank public class CryptoManager { private static final char LOWER_BOUND = '...

This is Crypto Manager blank


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");
        }
}

This is my version of Crypto Manager

//CryptoManager.java

public class CryptoManager {

  

static int LOWER_BOUND=32;

static int UPPER_BOUND=95;

static 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. 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 boolean stringInBounds (String plainText)

{

for(int i=0;i<plainText.length();i++)

{

if(plainText.charAt(i)<LOWER_BOUND || plainText.charAt(i)>UPPER_BOUND)

{

return false;

}

}

return true;

}

/*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 encryptCaesar(String plainText, int key)

{

key=keyWrap(key);

String caesar="";

for(int i=0;i<plainText.length();i++) {

caesar+= Character.toString((char)((int)plainText.charAt(i)+key));

}

return caesar;

}

/* 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 decryptCaesar(String encryptedText, int key){

String deCaesar="";

key=keyWrap(key);

for(int i=0;i<encryptedText.length();i++) {

deCaesar+= Character.toString((char)((int)encryptedText.charAt(i)-key));

}

return deCaesar;

}

  

/* 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 encryptBellaso(String plainText, String bellasoStr)

{

String res="";

int bellasoLength=bellasoStr.length();

for(int i=0;i<plainText.length();i++) {

char choiceChar=plainText.charAt(i);

int encryptedChar=((int)choiceChar+(int)bellasoStr.charAt(i%bellasoLength));

while(encryptedChar>(int)UPPER_BOUND) {

encryptedChar-=RANGE;

}

res+=(char)encryptedChar;

}

return res;

}

/*

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

public static String decryptBellaso(String encryptedText, String bellasoStr)

{

String keyInput="";

String decryptedBellaso="";

int counter=0;

while(keyInput.length()!=encryptedText.length()) {

keyInput=keyInput+bellasoStr.charAt(counter);

counter++;

if(counter==bellasoStr.length())

counter=0;

}

for(int i=0;i<encryptedText.length();i++) {

int disp=encryptedText.charAt(i)-keyInput.charAt(i);

if(disp<LOWER_BOUND) {

while(disp<LOWER_BOUND)

disp=disp+RANGE;

}

decryptedBellaso=decryptedBellaso+(char)disp;

}

return decryptedBellaso;

}

public static int keyWrap(int key) {

while (key>UPPER_BOUND)

{

key=key-(UPPER_BOUND-LOWER_BOUND);

}

return key;

}

}

This is the Junit Test it must pass. But it has two failures that I can't figure out as to why?

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class CryptoManagerTest {
        
        @Before
        public void setUp() throws Exception {
        }

        @After
        public void tearDown() throws Exception {
        }

        @Test
        public void testStringInBounds() {
                assertTrue(CryptoManager.stringInBounds("THIS TEST SHOULD SUCCEED"));
                assertFalse(CryptoManager.stringInBounds("TEST SHOULD FAIL BECAUSE ~ IS OUTSIDE THE RANGE"));
                assertFalse(CryptoManager.stringInBounds("This test should fail because of lowercase letters"));
        }

        @Test
        public void testEncryptCaesar() {
                assertEquals("WKLV#LV#DQRWKHU#WHVW",CryptoManager.encryptCaesar("THIS IS ANOTHER TEST", 3));
                assertEquals("RFGQ^RCQR^QFMSJB^QSAACCB", CryptoManager.encryptCaesar("THIS TEST SHOULD SUCCEED", 190));
                assertEquals("DAHHK\\SKNH@", CryptoManager.encryptCaesar("HELLO WORLD", 444));
        }
        
        @Test
        public void testDecryptCaesar() {
                assertEquals("THIS IS ANOTHER TEST", CryptoManager.decryptCaesar("WKLV#LV#DQRWKHU#WHVW", 3));
                assertEquals("THIS TEST SHOULD SUCCEED", CryptoManager.decryptCaesar("RFGQ^RCQR^QFMSJB^QSAACCB", 190));
                assertEquals("HELLO WORLD", CryptoManager.decryptCaesar("DAHHK\\SKNH@", 444));
        }

        @Test
        public void testEncryptBellaso() {
                assertEquals("WU\\VR9F#N!RF88U-'HED", CryptoManager.encryptBellaso("THIS IS ANOTHER TEST", "CMSC203"));
                assertEquals("PR%UKP6K_\\VF=4V", CryptoManager.encryptBellaso("MERRY CHRISTMAS", "CMSC203"));
                assertEquals("ZF&ZJX4US][EE2 ", CryptoManager.encryptBellaso("MERRY CHRISTMAS", "MATH181")); 
                                                                                                      
        }

        @Test
        public void testDecryptBellaso() {
                assertEquals("THIS IS ANOTHER TEST", CryptoManager.decryptBellaso("WU\\VR9F#N!RF88U-'HED", "CMSC203"));
                assertEquals("MERRY CHRISTMAS", CryptoManager.decryptBellaso("PR%UKP6K_\\VF=4V", "CMSC203"));
                assertEquals("MERRY CHRISTMAS", CryptoManager.decryptBellaso("ZF&ZJX4US][EE2 ", "MATH181")); 

        }

}

Programming Language: JAVA

Can someone help me fix my methods?

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

It is successfully passing all the test cases, after the below modifications.

public class CryptoManager {

   static int LOWER_BOUND = ' ';

   static int UPPER_BOUND = '_';

   static 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. 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 boolean stringInBounds(String plainText)

   {

       for (int i = 0; i < plainText.length(); i++)

       {

           if (plainText.charAt(i) < LOWER_BOUND || plainText.charAt(i) > UPPER_BOUND)

           {

               return false;

           }

       }

       return true;

   }

   /*
   * 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 encryptCaesar(String plainText, int key)

   {

       String caesar = "";

       for (int i = 0; i < plainText.length(); i++) {
          
           //shift the character by the given key

           int encryptedChar = (int) plainText.charAt(i) + key;
          
           // Make sure that the encryptedChar is with in the bounds.
          
           while (encryptedChar > (int) UPPER_BOUND) {

               encryptedChar -= RANGE;

           }

           caesar += (char) encryptedChar;

       }

       return caesar;

   }

   /*
   * 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 decryptCaesar(String encryptedText, int key) {

       String deCaesar = "";

       for (int i = 0; i < encryptedText.length(); i++) {
          
           //shift the character by the given key
          
           int decryptedChar = (int) encryptedText.charAt(i) - key;
          
           // Make sure that the encryptedChar is with in the bounds.
          
           while (decryptedChar < (int) LOWER_BOUND) {

               decryptedChar += RANGE;

           }

           deCaesar += (char) decryptedChar;

       }

       return deCaesar;

   }

   /*
   * 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 encryptBellaso(String plainText, String bellasoStr)

   {

       String res = "";

       int bellasoLength = bellasoStr.length();

       for (int i = 0; i < plainText.length(); i++) {

           char choiceChar = plainText.charAt(i);

           int encryptedChar = ((int) choiceChar + (int) bellasoStr.charAt(i % bellasoLength));

           while (encryptedChar > (int) UPPER_BOUND) {

               encryptedChar -= RANGE;

           }

           res += (char) encryptedChar;

       }

       return res;

   }

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

   public static String decryptBellaso(String encryptedText, String bellasoStr)

   {

       String keyInput = "";

       String decryptedBellaso = "";

       int counter = 0;

       while (keyInput.length() != encryptedText.length()) {

           keyInput = keyInput + bellasoStr.charAt(counter);

           counter++;

           if (counter == bellasoStr.length())

               counter = 0;

       }

       for (int i = 0; i < encryptedText.length(); i++) {

           int disp = encryptedText.charAt(i) - keyInput.charAt(i);

           if (disp < LOWER_BOUND) {

               while (disp < LOWER_BOUND)

                   disp = disp + RANGE;

           }

           decryptedBellaso = decryptedBellaso + (char) disp;

       }

       return decryptedBellaso;

   }

}

=====================================================

PLEASE UPVOTE, IF THE ANSWER IS HELPFUL.

Thank You.

Add a comment
Know the answer?
Add Answer to:
This is Crypto Manager blank public class CryptoManager { private static final char LOWER_BOUND = '...
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
  • Given the following classes: StringTools.java: public class StringTools { public static String reverse(String s){ char[] original=s.toCharArray();...

    Given the following classes: StringTools.java: public class StringTools { public static String reverse(String s){ char[] original=s.toCharArray(); char[] reverse = new char[original.length]; for(int i =0; i<s.length(); i++){ reverse[i] = original[original.length-1-i]; } return new String(reverse); } /**  * Takes in a string containing a first, middle and last name in that order.  * For example Amith Mamidi Reddy to A. M. Reddy.  * If there are not three words in the string then the method will return null  * @param name in...

  • Here is the indexOf method that I wrote: public static int indexOf(char[] arr, char ch) {...

    Here is the indexOf method that I wrote: public static int indexOf(char[] arr, char ch) {            if(arr == null || arr.length == 0) {                return -1;            }            for (int i = 0; i < arr.length; i++) {                if(arr[i] == ch) {                    return i;                }            }        return -1;       ...

  • Caesar Cipher v3 Decription Description A Caesar cipher is one of the first and most simple...

    Caesar Cipher v3 Decription Description A Caesar cipher is one of the first and most simple encryption methods. It works by shifting all letters in the original message (plaintext) by a certain fixed amount (the amounts represents the encryption key). The resulting encoded text is called ciphertext. Example Key (Shift): 3 Plaintext: Abc Ciphertext: Def Task Your goal is to implement a Caesar cipher program that receives the key and an encrypted paragraph (with uppercase and lowercase letters, punctuations, and...

  • Using Python; Caesar part of the homework: Write c_encrypt() and c_decrypt(), both of which take two...

    Using Python; Caesar part of the homework: Write c_encrypt() and c_decrypt(), both of which take two arguments, the first one a string and the second one an integer key. Both should return a string. Vigenère part of the homework: Write vig_encrypt() and vig_decrypt() functions. Each takes two strings as inputs, with the first being the plaintext/ciphertext, and the second being the key. Both should be calling functions you wrote earlier to help make the work easier. The key will be...

  • ​​​​​​public static int countCharacter(String str, char c) { // This recursive method takes a String and...

    ​​​​​​public static int countCharacter(String str, char c) { // This recursive method takes a String and a char as parameters and // returns the number of times the char appears in the String. You may // use the function charAt(int i) described below to test if a single // character of the input String matches the input char. // For example, countCharacter(“bobbie”, ‘b’) would return back 3, while // countCharacter(“xyzzy”, ‘y’) would return back 2. // Must be a RECURSIVE...

  • Solver.java package hw7; import java.util.Iterator; import edu.princeton.cs.algs4.Graph; import edu.princeton.cs.algs4.BreadthFirstPaths; public class Solver {    public static...

    Solver.java package hw7; import java.util.Iterator; import edu.princeton.cs.algs4.Graph; import edu.princeton.cs.algs4.BreadthFirstPaths; public class Solver {    public static String solve(char[][] grid) {        // TODO        /*        * 1. Construct a graph using grid        * 2. Use BFS to find shortest path from start to finish        * 3. Return the sequence of moves to get from start to finish        */               // Hardcoded solution to toyTest        return...

  • JAVA public static String generatePassword(String gp)        {            String password="";       ...

    JAVA public static String generatePassword(String gp)        {            String password="";            boolean b= false;            for (int i =0;i            {                char ch = gp.charAt(i);                if (i == 0)                {                    password += Character.toUpperCase(ch);                }                if (ch == 'S' || ch == 's')           ...

  • Change the following Shift Cipher program so that it uses OOP(constructor, ect.) import java.util...

    Change the following Shift Cipher program so that it uses OOP(constructor, ect.) import java.util.*; import java.lang.*; /** * * @author STEP */ public class ShiftCipher { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here Scanner input = new Scanner(System.in); String plainText; System.out.print("Please enter your string: "); // get message plainText = input.nextLine(); System.out.print("Please enter your shift cipher key: "); // get s int s = input.nextInt(); int...

  • Write the programming C please, not C++. The main function should be to find the offset...

    Write the programming C please, not C++. The main function should be to find the offset value of the ciper text "wPL2KLK9PWWZ7K3ST24KZYKfPMKJ4SKLYOKRP4KFKP842LK0ZTY43 " and decrypt it. In cryptography, a Caesar Cipher is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a left shift of 3, D would be...

  • Computer Science C++ Help, here's the question that needs to be answered (TASK D): Task D....

    Computer Science C++ Help, here's the question that needs to be answered (TASK D): Task D. Decryption Implement two decryption functions corresponding to the above ciphers. When decrypting ciphertext, ensure that the produced decrypted string is equal to the original plaintext: decryptCaesar(ciphertext, rshift) == plaintext decryptVigenere(ciphertext, keyword) == plaintext Write a program decryption.cpp that uses the above functions to demonstrate encryption and decryption for both ciphers. It should first ask the user to input plaintext, then ask for a right...

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