Last active
February 28, 2023 21:47
-
-
Save damianknopp/3a8c2ae3efe9edd46d9378a378a2592f to your computer and use it in GitHub Desktop.
decode rot N
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
import java.nio.file.*; | |
Path p1 = Path.of("rot.txt"); | |
String cipher = Files.readString(p1); | |
BiConsumer<String, Integer> decode = (cipher, rot) -> { | |
char[] cipherArray = cipher.toLowerCase().toCharArray(); | |
for (int i = 0; i < cipherArray.length; i++) { | |
if (!Character.isLetter(cipherArray[i])) { | |
continue; | |
} | |
char currentChar = cipherArray[i]; | |
int peek = (currentChar - 'a' - rot); | |
int shift = peek > 0 ? peek : (26 - Math.abs(peek)); | |
int minimumShift = shift % 26; | |
cipherArray[i] = (char)(minimumShift + 'a'); | |
} | |
String plainText = new String(cipherArray); | |
System.out.println(plainText); | |
}; | |
decode.accept("abbc", 13); | |
decode.accept("tqxxa iadxp", 12); | |
decode.accept("owwljgm jtcm asg", 8); | |
decode.accept("znk cnkkry ut znk hay", 6); | |
for (int i = 1; i < 26; i++) { | |
System.out.println("decode message with shift: " + i); | |
decode.accept(cipher, i); | |
System.out.println("-".repeat(20)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
output is