2

I was reading a book on cryptology and thought it would be a cool idea to write a small program that encodes and decodes a message.

I started out with a simple substitution cipher which would substitute a letter with a different letter, shifted a certain set amount. IE. one example would be: a->c, b->d, etc. just shifted by 2.

My code doesn't seem to be substituting the letters correctly. It always outputs the same thing I input, even thought the Key is set. Here is my code:

public class Keyshift {

public static int Key = 0;

public static void selectKey(){
    Scanner in = new Scanner(System.in);
    System.out.println("Enter an integer");
    int a = in.nextInt();
    Key = Key + a;
}

public static void encode(){

    String input = "";
    Scanner in = new Scanner(System.in);
    System.out.println("Enter a string");
    input = in.nextLine();

    String[] inputLetters = input.split("");
    String[] output = new String[inputLetters.length];

    String dictLowercase = new String("abcdefghijklmnopqrstuvwxyz");
    String dictUppercase = new String("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
    String[] dictLowerArray = dictLowercase.split("");
    String[] dictUpperArray = dictUppercase.split("");


    for(int i=0;i<inputLetters.length;i++){
        for(int c=0;c<25;c++){
            if (inputLetters[i]==dictLowerArray[c]){
                output[i] = dictLowerArray[((c+Key) % 26)];
            }
            else if (inputLetters[i]==dictUpperArray[c]){
                output[i] = dictUpperArray[((c+Key) % 26)];
            }
            else {
                output[i] = inputLetters[i];
            }
        }
    }
    System.out.println("Your encoded message is: ");
    System.out.println(Arrays.toString(output));   
    //System.out.println(Key);
}
/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
    selectKey();
    encode();
}

}
user3642365
  • 549
  • 1
  • 6
  • 16
  • 1
    In all actuality, if you converted everything to `char`, you could just add your offset in, since a `char` is an unsigned `int`. You'd have to handle the wrapping yourself, though. – Makoto Jul 04 '14 at 05:43

2 Answers2

1
        String dictLowercase = new String("abcdefghijklmnopqrstuvwxyz");
        String dictUppercase = new String("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
        char[] dictLowerArray = dictLowercase.toCharArray();//Use toCharArray
        char[] dictUpperArray = dictUppercase.toCharArray();
        char[] inputLetters = input.toCharArray();
        char[] output=new char[inputLetters.length];
        for(int i=0;i<inputLetters.length;i++){
            for(int c=0;c<25;c++){
                if (inputLetters[i]==(dictLowerArray[c])){
                    output[i] = (char) (dictLowerArray[c]+Key % 26);//Logic Issue
                }
                else if (inputLetters[i]==(dictUpperArray[c])){
                    output[i] = (char) (dictUpperArray[c]+Key % 26);
                }
                else {
                      output[i] = inputLetters[i];//If not alphabets
                }                
            }
          }
  • Go for char manipulation rather than String for convinience.
  • Use .toCharArray() instead of split
  • You can directly use character+Key % 26 on character.
akash
  • 22,664
  • 11
  • 59
  • 87
1

First try to compare to Strings with equals and not ==. Moreover, you failed to specify any break point, in case you found something has been modified. Say, the first character of inputLetters matches with first letter of dictLowerArray, now the iteration again goes inside the second loop, this time only the last else block gets executes and the same value, as in inputLetters gets copied to the output array.

Try this loop:

for(int i=0;i<inputLetters.length;i++){
        for(int c=0;c<25;c++){
            flag = false;
            if (inputLetters[i].equals(dictLowerArray[c])){
                output[i] = dictLowerArray[((c+Key) % 26)];             
                break;
            }
            else if (inputLetters[i].equals(dictUpperArray[c])){
                output[i] = dictUpperArray[((c+Key) % 26)];
                break;              
            }
            else {
                flag = true;
            }
        }
        if (flag) {
            output[i] = inputLetters[i];
        }
    }
nIcE cOw
  • 24,468
  • 7
  • 50
  • 143