Question
Need help with my Java code below: I am failing the Test for testEncrypt which leads me to believe my encrypt and decrypt is incorrect.
Need help with my Java code below:
I am failing the Test for testEncrypt which leads me to believe my encrypt and decrypt is incorrect. I cant figure out how to fix. Any help would be awsome
package cipher; import java.util.Map; import java.util.Random; import java.util.TreeMap;
public class SimpleCipher { Map key; Map decryptKey;
public SimpleCipher() { key = generateKey(); decryptKey = generateDecryptKey(key); } /** * Build the encryption key as a map where the key is a letter and * the value is its ciphered value. The map needs to include A-Z and * a-z (no need to encrypt other characters) */ private Map generateKey() { Map key = new TreeMap(); char out = 0 ; for(char alphabet = 'a'; alphabet <='z'; alphabet++ ) { out += alphabet; key.put(alphabet, out);} for(char alphabet = 'A'; alphabet <='Z'; alphabet++ ) { out += alphabet; key.put(alphabet, out);} return key; // possibly build a loop //key.put('A', 'b'); //key.put('B', 'c'); //key.put('C', 'd'); //key.put('D', 'e'); //key.put('E', 'f'); //return key; }
/** * Generate a map that is the 'flip' of the key. * A call to generate key must happen first */ private Map generateDecryptKey(Map key) { Map decryptKey = new TreeMap(); //key.get(decryptKey); char out = 0 ; for(char alphabet = 'a'; alphabet <='z'; alphabet++ ) { out += alphabet; decryptKey.put(out, alphabet);} for(char alphabet = 'A'; alphabet <='Z'; alphabet++ ) { out += alphabet; decryptKey.put(out, alphabet);} return decryptKey; }
/** * Encrypt the given message * @param message - to encrypt * @return a ciphered message */ public String encrypt(String message) { String chipencryp = message ; chipencryp = chipencryp + key; return chipencryp;}
/** * Decrypt the given message * @param ciphered - to decrypt * @return the original message */ public String decrypt(String ciphered) { return ciphered + decryptKey.toString(); } public Map getKey() { return key; }
public Map getDecryptKey() { return decryptKey; } }
TEST CODE:
package cipher.test;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotEquals;
import java.util.Map;
import org.junit.Test;
import com.gradescope.jh61b.grader.GradedTest;
import cipher.SimpleCipher;
public class CipherTest { @Test @GradedTest(name="Test generateKey()", max_score=5) public void testGenerateKey() { SimpleCipher sc = new SimpleCipher(); Map
@Test @GradedTest(name="Test generateDecriptKey()", max_score=5) public void testGenerateDecryptKey() { SimpleCipher sc = new SimpleCipher(); Map
SimpleCipher sc = new SimpleCipher(); for (String phrase : phrases) { String ciphered = sc.encrypt(phrase); assertNotEquals("Message did not change", phrase, ciphered); assertEquals("The decrypted does not match the original", phrase, sc.decrypt(ciphered)); } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started