0

I have this Vigenère cipher code that works, but it spits out a number one on the first encryption when I put "Attack at Dawn" (POTTER). It comes out as 1inuhc Qi Xubf. What is causing this?

public class vigenere {

    public static void main(String[] args) {

        System.out.println();
        char[] message = args[0].toCharArray();
        int code;
        int index = 0;
        code = args[1].charAt(index%args[1].length()) - 96;
        for(int i = 0; i < message.length; i++){
            code = args[1].charAt(index%args[1].length()) - 96;
            if(65 <= message[i] && message[i] <= 90){
                index++;
                message[i] = (char) (65 + ((message[i] - 65) + code) % 26);
            }
            else if(97 <= message[i] && message[i] <= 122){
                index++;
                message[i] = (char) (97 + ((message[i] - 97) + code) % 26);
            }
            System.out.print(message[i]);
        }
    }
}
Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
Gxfire
  • 27
  • 2
  • This must be a new definition of "works" that I'm not familiar with. (Oh wait! I remember now. use it this way :-) ) – Stephen C Jan 31 '14 at 05:56
  • What was your desired output? I'm guessing `phmtgb ph wtae` based on http://sharkysoft.com/misc/vigenere. – Duncan Jones Jan 31 '14 at 08:16

1 Answers1

0

I think that you input is

text to be encrypted "Attack at Dawn"

key = potter (should be in all low case letter right) because of this line

code = args[1].charAt(index%args[1].length()) - 96;

I enter the key as Potter and get your result posted.but I use potter, it works fine.

tom87416
  • 542
  • 4
  • 9