Last active
May 5, 2019 00:14
-
-
Save dgozalo/19bdbd222924271b53ee18cb595b4392 to your computer and use it in GitHub Desktop.
Cipher / Decipher
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.io.IOException; | |
import java.nio.file.Files; | |
import java.nio.file.Paths; | |
import java.util.*; | |
import java.util.stream.Collectors; | |
import java.util.stream.Stream; | |
public class Cipher { | |
static Map<String, Integer> numericCipherAssignments = new HashMap<>() { | |
{ | |
put("A", 0); | |
put("B", 1); | |
put("C", 2); | |
put("D", 3); | |
put("E", 4); | |
put("F", 5); | |
put("G", 6); | |
put("H", 7); | |
put("I", 8); | |
put("J", 9); | |
put("K", 10); | |
put("L", 11); | |
put("M", 12); | |
put("N", 13); | |
put("O", 14); | |
put("P", 15); | |
put("Q", 16); | |
put("R", 17); | |
put("S", 18); | |
put("T", 19); | |
put("U", 20); | |
put("V", 21); | |
put("W", 22); | |
put("X", 23); | |
put("Y", 24); | |
put("Z", 25); | |
} | |
}; | |
private static Map<Integer, String> cipherReversedAssignments; | |
public static void main(String[] args) throws IOException { | |
cipherReversedAssignments = numericCipherAssignments.entrySet() | |
.stream() | |
.collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey)); | |
String keyFilePath = "src/key.txt"; | |
List<Integer> encodedKeyParts = readAndEncodeFile(keyFilePath); | |
// Deciphering for exercise 2 | |
decodeMessageWithKey(encodedKeyParts); | |
//Ciphering for exercise 3 | |
encodeMessageWithKey("OPERACION OVERLORD DIEZ JULIO CALAIS", encodedKeyParts); | |
} | |
private static void encodeMessageWithKey(String message, List<Integer> encodedKeyFragments) { | |
List<Integer> messageEncoded = Arrays.stream(message.replaceAll("\\s+", "").split("")) | |
.map(numericCipherAssignments::get).collect(Collectors.toList()); | |
StringBuilder cipheredMessage = new StringBuilder(); | |
for (int i = 0; i < messageEncoded.size() ; i++) { | |
int messagePart = messageEncoded.get(i); | |
int keyPart = encodedKeyFragments.get(i); | |
int result = (messagePart + keyPart) % 26; | |
cipheredMessage.append(cipherReversedAssignments.get(result)); | |
} | |
System.out.println(String.format("The result of the ciphering is: %1s", cipheredMessage.toString())); | |
} | |
private static void decodeMessageWithKey(List<Integer> encodedKeyFragments) throws IOException { | |
String messageFilePath = "src/message.txt"; | |
List<Integer> encodedMessageParts = readAndEncodeFile(messageFilePath); | |
StringBuilder decipheredMessage = new StringBuilder(); | |
for (int i = 0; i < encodedMessageParts.size(); i++) { | |
int messagePart = encodedMessageParts.get(i); | |
int keyPart = encodedKeyFragments.get(i); | |
int result = (messagePart - keyPart) % 26; | |
decipheredMessage.append(cipherReversedAssignments.get(result < 0 ? 26 + result : result)); | |
} | |
System.out.println(String.format("The result of the deciphering is: %1s", decipheredMessage.toString())); | |
} | |
private static List<Integer> readAndEncodeFile(String filePath) throws IOException { | |
return Files.lines(Paths.get(filePath)) | |
.flatMap(line -> Stream.of(line.split(""))) | |
.map(numericCipherAssignments::get) | |
.collect(Collectors.toList()); | |
} | |
} |
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
CIHJTUUHMLFRUGCZIBGDBQPNIPDNJGLPLLPYJYXM |
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
QXLATWCVZZAVLRQQLTKLTZJAQDQBASLCOTP |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment