2

Relating to this question: ROT-13 function in java?

What would be the code to decode rot13 in java? Do I simply reverse the signs?

Below is the code for encoding a String into rot13:

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;
            System.out.print(c);
        }
        System.out.println();
    }

}
Community
  • 1
  • 1
Jean-Paul
  • 19,910
  • 9
  • 62
  • 88
  • 1
    "Because there are 26 letters (2×13) in the basic Latin alphabet, ROT13 is its own inverse; that is, to undo ROT13, the same algorithm is applied, so the same action can be used for encoding and decoding." Wikipedia. – james.garriss Dec 17 '15 at 17:14
  • Yep, saw it, @Jean-Paul. It's cool. I just thought that the WP entry was helpful. – james.garriss Dec 17 '15 at 17:18

2 Answers2

8

You don't reverse the signs. The decoding method is identical to the encoding method.

For example : 'a' is encoded to 'n'. If you "encode" the 'n', it is decoded back to 'a'.

Eran
  • 387,369
  • 54
  • 702
  • 768
0

Because there are 26 letters (2×13) in the basic Latin alphabet, ROT13 is its own inverse; that is, to undo ROT13, the same algorithm is applied, so the same action can be used for encoding and decoding. The algorithm provides virtually no cryptographic security, and is often cited as a canonical example of weak encryption

The wikipedia article explains it pretty well