Search code examples
javaencryptioncaesar-cipher

How to loop a numerical value in a range when reaching the end of the range


I am trying to create a basic encryption program that converts character values to numerical form, adds on a "key" and then converts them back to char form, thus encrypting the word.

However I cannot figure out how to make the numerical value 90 or greater (char Z), loop back to 65 (char A).

public class Encode {

    /**
     * Main method - this does not need to be changed at all.
     */
    public static void main(String[] args) {
        testing();
        userInteraction();
    }

    /**
     * This method is used to test the encrypt method.
     */
    public static void testing() {

        String testMessage1 = "test";
        int testKey1 = 11;
        String result = encrypt(testMessage1, testKey1);
        System.out.println("Encrypted result: "+result);
    }

    /**
     * This method changes each character in a String to a
     * different character based on the key passed in as
     * an integer. The new String created by the encryption
     * process is returned.
     *
     * @param message   the String to be encoded
     * @param key       the integer value used for encryption
     * @return          a new encoded String
     */
    public static String encrypt(String message, int key) {
        System.out.println("encoding: "+message+", with key: "+key);
        String encodedMessage = "";
        message=message.toUpperCase();

        int length = message.length();
        for(int i=0;i<length;i++)
        {
            int characterValue = (int)message.charAt(i);
            characterValue = characterValue + key;
                if(characterValue>90)
                {
                    characterValue = (65 + key ); // <---- What needs to go here? In order to make value loop back to A.
                }

            char myChar = (char)characterValue;
            encodedMessage=encodedMessage+myChar;
        }


        return encodedMessage;
    }

Solution

  • First of all let's just start by saying that your encryption is ambiguous. That beeing said, change characterValue = (65 + key ); to characterValue = ((characterValue - 90) + 65);
    Also, you were adding the key two times on characterValue>90.

    Alright, let me explain a bit more with examples.
    In the line characterValue = characterValue + key; you already have the key added to the letter, so I removed it from the code inside if. I don't think this part needs an example.

    Next part, the sum itself:
    Let's say someone types AZTEST, with the key 11, the result should be (adding key and starting over if bigger than 90): LLFPEF

    A(65):(76)L
    Z(90):(101->76)L
    T(84):(95->70)F
    E(69):(80)P
    S(83):(94->69)E
    T(84):(95->70)F
    

    Using 'S' as example, we have 83 + 11 = 94. 94 is bigger than 90, so here is what we have to do: First, we find how much it exceeds (94 - 90 = 4). Now that we have that number, we just have to start the counting over. A=65, so 65+4=69. 69 = E.

    The problem is that Z is going to become the same as another value. The only key I see Z differ from A, in this case, is key=0, but the encryption would be broken for there is no change in the original text.