Skip to content

Instantly share code, notes, and snippets.

@Badbird5907
Created September 22, 2021 03:20
Show Gist options
  • Save Badbird5907/469953d10c88536f098fa62406adca09 to your computer and use it in GitHub Desktop.
Save Badbird5907/469953d10c88536f098fa62406adca09 to your computer and use it in GitHub Desktop.
ROT decoder/encoder
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;
}
}
@JustDoom
Copy link

I like it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment