Search code examples
javaencryptiondecodesubstitution

ROT-N (or ROT-X) function in Java


Currently, I'm writing a program which performs ROT-1 until and including ROT-25 on a given String using Java. In the beginning of my research, I found this code:

public class Rot13 { 

public static void main(String[] args) {
    String s = args[0];
    for (int i = 0; i < s.length(); i++) {
        char c = s.charAt(i);
        if       (c >= 'a' && c <= 'm') c += 13;
        else if  (c >= 'A' && c <= 'M') c += 13;
        else if  (c >= 'n' && c <= 'z') c -= 13;
        else if  (c >= 'N' && c <= 'Z') c -= 13;
        StdOut.print(c);
    }
    StdOut.println();
}
}

After some trouble shooting I got to this:

private static void rotALL(String input) {
//Loop 25 times, starting with ROT-1 and ending at ROT-25 (every possibliity besides the original input)
    for (int i = 1; i < 26; i++) {
        int rot = 26 - i;
        System.out.print("ROT" + rot + ": ");    
        for (int charIndex = 0; charIndex < input.length(); charIndex++) {
            char c = input.charAt(charIndex);

            int inta = 97; //a in the ASCII table
            int intaWithRot = inta + rot;
            int intA = 65; //A in the ASCII table
            int intAWithRot = intA + rot;

            int intaWithRotPlusOne = intaWithRot + 1;
            int intaWithRotPlusi = intaWithRot + i;
            int intAWithRotPlusOne = intAWithRot + 1;
            int intAWithRotPlusi = intAWithRot + i;

            if (c >= inta && c <= intaWithRot) {
                c += rot;
           } else if (c >= intA && c <= intAWithRot) {
                c += rot;
            } else if (c >= intaWithRotPlusOne && c <= intaWithRotPlusi) {
                c -= rot;
            } else if (c >= intAWithRotPlusOne && c <= intAWithRotPlusi) {
                c -= rot;
            }
            System.out.print(c);
        }
        System.out.println();
    }

Now I have to problems:

  1. When I put in "grfg qngn", which is "test data" using ROT-13, my output for ROT-13 is "ROT13: test d{t{", the "{" and "a" are 26 places apart from eachother in the ASCII table, but I dont know why this error occurs, when letters such as the "e" are displayed correctly.

  2. How do I change this algorithm so it loops through ROT-1 through ROT-25? I figured this should do the trick, but I am missing something.

Thanks in advance and kind regards!


Solution

  • /**
     * Returns a list with Strings which are rotated ROT-n. n = 26 - listIndex
     *
     * Source: http://www.rot-n.com/?page_id=4 
     *
     * @param input the string to mutate
     * @param numeric include numeric values
     * @return a list with mutated strings
     */
    private static List<String> rotN(String input, boolean numeric) {
        List<String> output = new ArrayList<>();
        for (int n = 0; n < 26; n++) {
            String result = "";
            int length = input.length();
    
            for (int i = 0; i < length; i++) {
                char ascii = input.charAt(i);
                char rotated = ascii;
                //Capital letters are 60 to 90
                if (ascii > 64 && ascii < 91) {
                    rotated = (char) (rotated + n);
                    if (rotated > 90) {
                        rotated += -90 + 64;
                    }
                    if (rotated < 65) {
                        rotated += -64 + 90;
                    }
                } else if (ascii > 96 && ascii < 123) { //Lowercase letters are between 97 and 122
                    rotated = (char) (rotated + n);
                    if (rotated > 122) {
                        rotated += -122 + 96;
                    }
                    if (rotated < 97) {
                        rotated += -96 + 122;
                    }
                }
                //Numeric values are between 48 to 57 
                if (numeric && ascii > 47 && ascii < 58) {
                    rotated = (char) (rotated + n);
                    if (rotated > 47) {
                        rotated += -57 + 47;
                    }
                    if (rotated < 58) {
                        rotated += -47 + 57;
                    }
                }
                result += (char) rotated;
            }
            output.add(result);
        }
        return output;
    }
    

    This is the solution I found, which is working.