the code is doing two things wrong - first, it is not processing upper case letters at
Question:
the code is doing two things wrong - first, it is not processing upper case letters at all. Secondly, it is supposed to use key1 beginning with the first character and key2 with the second character and is doing the opposite. I thought the first character's index is 0 so key1 would be the evens and key2 would be the odds. I don't know if that is incorrect or if that is correct, but I made an error in my code.
Running a test on First Legion as the text with 23 as key1 and 17 as key2 should get
Czojq Ivdzle.
Alphabets:
BCDEFGHIJKLMNOPQRSTUVWXYZ XYZABCDEFGHIJKLMNOPQRSTUVW - 23 (key1) RSTUVWXYZABCDEFGHIJKLMNOPQ - 17 (key2)
here is the code I am having trouble with
public String encryptTwoKeys(String input, int key1, int key2){ StringBuilder encrypted = new StringBuilder(input); //Write down the alphabet String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; System.out.println(alphabet); //Compute the shifted alphabet String shifted1 = alphabet.substring(key1)+ alphabet.substring(0,key1); System.out.println(shifted1); String shifted2 = alphabet.substring(key2)+ alphabet.substring(0,key2); System.out.println(shifted2); for(int i = 0; i < encrypted.length(); i++) { //Look at the ith character of encrypted (call it currChar) char currChar = encrypted.charAt(i); //so I wrote this thinking that from the .isLowerCase if will run through to the end of the if (idx != -1 && idx%2 == 1) clause //the lowercase letters do atleast get worked on if (Character.isLowerCase(currChar) == true){ //Find the index of currChar in the alphabet (call it idx) int idx = alphabet.indexOf(Character.toUpperCase(currChar)); //If currChar is in the alphabet // so the belong seems to be wrong, but I thought if the index was 0 that 0%2 = 0, 2&2=0 etc. so that the shifted1 alphabet would be used, but instead it is the odd number index #s if(idx != -1 && idx%2 == 0){ //Get the idxth character of shiftedAlphabet (newChar) char newChar = Character.toLowerCase(shifted1.charAt(idx)); //Replace the ith character of encrypted with newChar encrypted.setCharAt(i, newChar); } if (idx != -1 && idx%2 == 1){ //Get the idxth character of shiftedAlphabet (newChar) char newChar = Character.toLowerCase(shifted2.charAt(idx)); //Replace the ith character of encrypted with newChar encrypted.setCharAt(i, newChar); } //this is where I think the uppercase letters should be processed, but they aren't happening at all, it is returning the original letters. //the else equalling if (Character.isLowerCase(currChar) == false) else {idx = alphabet.indexOf(currChar); //If currChar is in the alphabet if(idx != -1 && idx%2 ==0){ //Get the idxth character of shiftedAlphabet (newChar) char newChar = shifted1.charAt(idx); //Replace the ith character of encrypted with newChar encrypted.setCharAt(i, newChar); } if(idx != -1 && idx%2 ==1){ //Get the idxth character of shiftedAlphabet (newChar) char newChar = shifted2.charAt(idx); //Replace the ith character of encrypted with newChar encrypted.setCharAt(i, newChar); } } } } return encrypted.toString();} public void testCaesar2() { int key1 = 23; int key2 = 17; FileResource fr = new FileResource(); String message = fr.asString(); String encrypted = encryptTwoKeys(message, key1, key2); System.out.println(encrypted); String decrypted = encryptTwoKeys(encrypted, 26-key1, 26-key2); System.out.println(decrypted); } }
the original code when there is just one key which does work correctly including the uppercase letters is this in case it helps:
import edu.duke.*;
public class CaesarCipher { public String encrypt(String input, int key) { //Make a StringBuilder with message (encrypted) StringBuilder encrypted = new StringBuilder(input); //Write down the alphabet String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //Compute the shifted alphabet String shiftedAlphabet = alphabet.substring(key)+ alphabet.substring(0,key); System.out.println(alphabet); System.out.println(shiftedAlphabet); //Count from 0 to < length of encrypted, (call it i) for(int i = 0; i < encrypted.length(); i++) { //Look at the ith character of encrypted (call it currChar) char currChar = encrypted.charAt(i); if (Character.isLowerCase(currChar) == true){ //Find the index of currChar in the alphabet (call it idx) int idx = alphabet.indexOf(Character.toUpperCase(currChar)); //If currChar is in the alphabet if(idx != -1){ //Get the idxth character of shiftedAlphabet (newChar) char newChar = Character.toLowerCase(shiftedAlphabet.charAt(idx)); //Replace the ith character of encrypted with newChar encrypted.setCharAt(i, newChar); } //Otherwise: do nothing } else {int idx = alphabet.indexOf(currChar); //If currChar is in the alphabet if(idx != -1){ //Get the idxth character of shiftedAlphabet (newChar) char newChar = shiftedAlphabet.charAt(idx); //Replace the ith character of encrypted with newChar encrypted.setCharAt(i, newChar); } } } //Your answer is the String inside of encrypted return encrypted.toString();} public void testCaesar() { int key = 23; FileResource fr = new FileResource(); String message = fr.asString(); String encrypted = encrypt(message, key); System.out.println(encrypted); String decrypted = encrypt(encrypted, 26-key); System.out.println(decrypted); }
International Marketing And Export Management
ISBN: 9781292016924
8th Edition
Authors: Gerald Albaum , Alexander Josiassen , Edwin Duerr