Skip to content

Instantly share code, notes, and snippets.

@youssef3wi
Created December 20, 2023 20:41
Show Gist options
  • Select an option

  • Save youssef3wi/264c89d14f4a8ae8ff72b32db39cab88 to your computer and use it in GitHub Desktop.

Select an option

Save youssef3wi/264c89d14f4a8ae8ff72b32db39cab88 to your computer and use it in GitHub Desktop.
Vous êtes enseignant et vous soupçonnez vos élèves d'échanger des messages secrets pendant vos cours.
import java.util.HashMap;
import java.util.Map;
/**
* Vous êtes enseignant et vous soupçonnez vos élèves d'échanger des messages secrets pendant vos cours.
* <p>
* Vous avez confisqué une feuille à Alice. Il semble qu'elle préparait un message pour son ami Bob.
* La première phrase de la feuille est en clair, la seconde est codée. La méthode semble être un chiffrement par substitution.
* Chaque lettre du texte en clair correspond toujours à la même lettre chiffrée. Par chaque, la phrase initiale contient toutes les lettres de l'alphabet,
* ce qui vous permet de déduire la correspondance complète des lettres.
* <p>
* Vous avez également confisqué une feuille à Bob, qui préparait un message pour Carol.
* Cette feuille contient également une première phrase en claire contenant toutes les lettres de l'alphabet (quelle chance !),
* et une seconde phrase chiffrée.
* La correspondance des lettres semble différente par rapport à Alice et Bob.
* <p>
* Enfin, vous avez intercepté un message de Carol, qui ne contient qu'une seule phrase chiffrée.
* Grâce à vos talents de persuation, Carol a avoué que le message était destiné à Alice.
* <p>
* Carole est l'une des élèves les plus intelligents de votre classe. Elle a certainement chiffré deux fois son message.
* Vous devrez donc le déchiffrer une première fois avec la substitution "Bob/Carol" et une deuxième fois avec la substitution "Alice/Bob".
* <p>
* Quel était le message que Carol voulait transmettre à Alice ? écrivez une fonction qui le renverra en clair,
* à partir des phrases des deux feuilles confisquées et du message chiffré de Carol.
* <p>
* Tous les textes ne contiennent que les lettres majuscules et des espaces. Les espaces ne sont jamais transformés par les différents chiffrements.
* <p>
* Les 4 premiers paramètres (les messages à utiliser pour établir les correspondances) contiennent toujours toutes les lettres de l'alphabet.
*/
public class CodinGame {
/**
* @param clearMsgAb The message Alice wanted to send to Bob, in clear text.
* @param cipheredMsgAb The message Alice wanted to send to Bob, ciphered according the mapping Alice-Bob.
* @param clearMsgBc The message Bob wanted to send to Carol, in clear text.
* @param cipheredMsgBc The message Bob wanted to send to Carol, ciphered according the mapping Bob-Carol.
* @param cipheredMsgCba The message Carol wanted to send to Alice, ciphered according the mapping Alice-Bob, then the mapping Bob-Carol.
* @return The message Carol wanted to send to Alice, in clear text.
*/
static String decrpyt(String clearMsgAb, String cipheredMsgAb, String clearMsgBc, String cipheredMsgBc, String cipheredMsgCba) {
// processing Bob-Carol
final Map<Character, Character> mappings = new HashMap<>();
for (int idx = 0; idx < clearMsgBc.length(); idx++) {
mappings.put(cipheredMsgBc.charAt(idx), clearMsgBc.charAt(idx));
}
StringBuilder clearMsgCb = new StringBuilder();
for (int idx = 0; idx < cipheredMsgCba.length(); idx++) {
clearMsgCb.append(mappings.get(cipheredMsgCba.charAt(idx)));
}
// processing Alice-Bob
for (int idx = 0; idx < clearMsgAb.length(); idx++) {
mappings.put(cipheredMsgAb.charAt(idx), clearMsgAb.charAt(idx));
}
StringBuilder clearMsgCba = new StringBuilder();
for (int idx = 0; idx < clearMsgCb.length(); idx++) {
clearMsgCba.append(mappings.get(clearMsgCb.charAt(idx)));
}
// Get the cleared text
return clearMsgCba.toString();
}
public static void main(String[] args) {
String clearMsgAb = "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG";
String cipheredMsgAb = "FWK CYTIN AROXL BOD MYHSV OZKR FWK PQUG EOJ";
String clearMsgBc = "PACK MY BOX WITH FIVE DOZEN LIQUOR JUGS";
String cipheredMsgBc = "YCAR HX MKZ OTJU STNW BKIWL FTDEKV QEPG";
String cipheredMsgCba = "SOJN TYDNN JN CKVJFQ DFW SOR SRDTORV JN WXUC";
String clearMsgCba = decrpyt(clearMsgAb, cipheredMsgAb, clearMsgBc, cipheredMsgBc, cipheredMsgCba);
System.out.println(clearMsgCba); // THIS CLASS IS BORING AND THE TEACHER IS DUMB
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment