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