Created
September 22, 2021 03:20
-
-
Save Badbird5907/469953d10c88536f098fa62406adca09 to your computer and use it in GitHub Desktop.
ROT decoder/encoder
This file contains hidden or 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
public class ROTCipher { | |
private static final String ALPHABET = "abcdefghijklmnopqrstuvwxyz"; | |
public static String encode(String text,int rotations){ | |
String rotated = rotate(rotations); | |
rotated += rotated.toUpperCase(); | |
String alphabet = ALPHABET + ALPHABET.toUpperCase(); | |
StringBuilder sb = new StringBuilder(); | |
for (char c : text.toCharArray()) { | |
String s = c + ""; | |
if (s == " ") | |
sb.append(" "); | |
else if (!alphabet.contains(s)) | |
sb.append(s); | |
else{ | |
sb.append(rotated.toCharArray()[alphabet.indexOf(c)]); | |
} | |
} | |
return sb.toString(); | |
} | |
public static String decode(String text,int rotations){ | |
String rotated = rotate(rotations); | |
rotated += rotated.toUpperCase(); | |
String alphabet = ALPHABET + ALPHABET.toUpperCase(); | |
StringBuilder sb = new StringBuilder(); | |
for (char c : text.toCharArray()) { | |
String s = c + ""; | |
if (s == " ") | |
sb.append(" "); | |
else if (!alphabet.contains(s)) | |
sb.append(s); | |
else{ | |
sb.append(alphabet.toCharArray()[rotated.indexOf(c)]); | |
} | |
} | |
return sb.toString(); | |
} | |
private static String rotate(int rotations){ | |
String ret = ALPHABET; | |
for (int i = 0; i < rotations; i++) { | |
String s = ret.charAt(0) + ""; | |
ret = ret.substring(1); | |
ret += s; | |
} | |
return ret; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I like it