ROT13 (ROT-13) is a simple monoalphabetic substitution cipher over 26 characters of the English alphabet. ROT13 is a special case of Caesar cipher.

Description

To cipher and decipher the given text, ROT13 shuffles (rotates) the alphabet by 13 places. Which means that if we write down the alphabet in 2 rows (each containing 13 characters), than we can transform (encipher, decipher) the text by substituting the characters of the first row by characters of the second row (and vice versa).

ROT13 – encryption/decryption schema
ROT13 – encryption/decryption schema

Examples

ATTACKATDAWN ⇔ NGGNPXNGQNJA
ENEMYATTHEGATES ⇔ RARZLNGGURTNGRF
BLACKHAWKDOWN ⇔ OYNPXUNJXQBJA

Code

    /**
     * ROT13
     * @param text text containing only uppercase English characters
     * @return ciphertext - if the input was plaintext, plaintext - if the input was ciphertext 
     */
    public static String rot13(String text) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < text.length(); i++) {
            if (text.charAt(i) < 'A' || text.charAt(i) > 'Z') {
                throw new IllegalArgumentException("Text must contain only uppercase english letters");
            }
            builder.append((char) ('A' + ((text.charAt(i) - 'A' + 13) % 26)));
        }
        return builder.toString();
    }







       
 

Place for your banner

Here is the position ready for our customer's banners.