Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 key = sc.getKey(); assertEquals("Key should contain 52 values, lower case and capitals", 52, key.size()); int THRESHOLD = 3; // The number that is OK to be the same since it is randomly selected int same = 0; for (Map.Entry e : key.entrySet()) { if (e.getKey() == e.getValue()) { same = same + 1; } } assertFalse("Your map should have fewer than " + THRESHOLD + " unciphered letters but you had " + same, same > THRESHOLD); }

@Test @GradedTest(name="Test generateDecriptKey()", max_score=5) public void testGenerateDecryptKey() { SimpleCipher sc = new SimpleCipher(); Map key = sc.getKey(); Map decryptKey = sc.getDecryptKey(); assertEquals("Key should contain 52 values, lower case and capitals", 52, key.size()); assertEquals("The size of the key and decrypt key must match", key.size(), decryptKey.size()); for (Character c : decryptKey.keySet()) { assertEquals("The encryption keys do not match!", key.get(decryptKey.get(c)), c); } } @Test @GradedTest(name="Test encrypt() and decrypt()", max_score=10) public void testEncrypt() { String[] phrases = {"Hello Maps", "Map=Dictionary=Symbol Table", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"};

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

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

Oracle Database Foundations Technology Fundamentals For IT Success

Authors: Bob Bryla

1st Edition

0782143725, 9780782143720

More Books

Students also viewed these Databases questions