Created
March 27, 2011 21:41
-
-
Save tomhermann/889659 to your computer and use it in GitHub Desktop.
Rot13Codec Implementation for Java. Can decode offensive linux fortunes with this badboy.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.zombietank.util; | |
public class Rot13Codec { | |
/*** | |
* encode(encode(input)) = input | |
*/ | |
public static String decode(final String input) { | |
return encode(input); | |
} | |
public static String encode(final String input) { | |
StringBuilder encoded = new StringBuilder(input.length()); | |
for (char character : input.toCharArray()) { | |
encoded.append(Character.isLetter(character) ? rot13(character) : character); | |
} | |
return encoded.toString(); | |
} | |
private static char rot13(char letter) { | |
int shift = Character.toUpperCase(letter) <= 'M' ? 13 : -13; | |
return (char)(letter + shift); | |
} | |
private Rot13Codec() { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment