Untitled
unknown
plain_text
2 years ago
4.0 kB
11
Indexable
public class Solution { static String[][] LEFT = { {"M", "O"}, {"N", "W"}, {"O", "Y"}, {"P", "H"}, {"Q", "X"}, {"R", "U"}, {"S", "S"}, {"T", "P"}, {"U", "A"}, {"V", "I"}, {"W", "J"}, {"X", "R"}, {"Y", "C"}, {"Z", "B"}, {"A", "E"}, {"J", "K"}, {"C", "M"}, {"D", "F"}, {"E", "L"}, {"F", "G"}, {"G", "D"}, {"H", "Q"}, {"I", "V"}, {"B", "Z"}, {"K", "N"}, {"L", "T"} }; static String[][] CENTER = { {"C", "D"}, {"D", "K"}, {"E", "S"}, {"F", "I"}, {"G", "R"}, {"H", "U"}, {"I", "X"}, {"J", "B"}, {"K", "L"}, {"L", "H"}, {"M", "W"}, {"N", "T"}, {"O", "M"}, {"P", "C"}, {"Q", "Q"}, {"R", "G"}, {"S", "Z"}, {"T", "N"}, {"U", "P"}, {"V", "Y"}, {"W", "F"}, {"X", "V"}, {"Y", "O"}, {"Z", "E"}, {"A", "A"}, {"B", "J"} }; static String[][] RIGHT = { {"L", "V"}, {"M", "Z"}, {"N", "N"}, {"O", "Y"}, {"P", "A"}, {"Q", "I"}, {"R", "W"}, {"S", "G"}, {"T", "E"}, {"U", "K"}, {"V", "M"}, {"W", "U"}, {"X", "S"}, {"Y", "Q"}, {"Z", "O"}, {"E", "B"}, {"B", "D"}, {"C", "F"}, {"D", "H"}, {"A", "J"}, {"F", "L"}, {"G", "C"}, {"H", "P"}, {"I", "R"}, {"J", "T"}, {"K", "X"} }; static String[] REFLECTOR = { "A", "B", "C", "D", "E", "F", "G", "D", "I", "J", "K", "G", "M", "K", "M", "I", "E", "B", "F", "T", "C", "V", "V", "J", "A", "T" }; public static void main(String[] args) { String input = "HELLOWORLD"; System.out.println(encrypt(input)); } static String encrypt(String input) { System.out.println("Encrypting input " + input ); String encryptedMessage = ""; for (int i = 0; i < input.length(); i++) { int pos = (int) input.charAt(i) - 65; pos = lookupRotor(RIGHT, pos, true); pos = lookupRotor(CENTER, pos, true); pos = lookupRotor(LEFT, pos, true); pos = lookupReflector(REFLECTOR, pos); pos = lookupRotor(LEFT, pos, false); pos = lookupRotor(CENTER, pos, false); pos = lookupRotor(RIGHT, pos, false); encryptedMessage = encryptedMessage + (char)(pos + 65); } System.out.println("Encrypted message is " + encryptedMessage); return ""; }; // lookingFromTheRight if true you are looking at the rotor from right to left static int lookupRotor(String[][] rotor, int pos, boolean lookingFromTheRight) { String value = rotor[pos][0]; if (lookingFromTheRight) { value = rotor[pos][1]; } int posToMatch = 1; if (lookingFromTheRight) { posToMatch = 0; } for(int i = 0; i < rotor.length; i++) { if (rotor[i][posToMatch].equals(value)) { return i; } } return -1; } static int lookupReflector(String[] reflector, int pos) { String val = reflector[pos]; for(int i = 0; i < reflector.length; i++ ) { if (i == pos) { continue; } if (reflector[i].equals(val)) { return i; } } return -1; } }
Editor is loading...