Skip to content

Instantly share code, notes, and snippets.

@EnigmaTriton
Last active April 5, 2020 15:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EnigmaTriton/d2e3a95f7abf852af9c543d95d001bd3 to your computer and use it in GitHub Desktop.
Save EnigmaTriton/d2e3a95f7abf852af9c543d95d001bd3 to your computer and use it in GitHub Desktop.
Solveur pour la micro-enigma du premier magazine Next-INpact
#include <stdio.h>
#include <string.h>
#define modulo 26
#define retour 0
#define aller 1
typedef int nxigma_rotor[2][modulo];
typedef int nxigma_reflector[modulo];
int ord(char c)
{
return c - 'a';
}
char chr(int i)
{
return 'a' + i;
}
int mod(int i)
{
int ret = i % modulo;
if (ret < 0)
{
return ret + modulo;
}
else
{
return ret;
}
}
int apply_rotor(nxigma_rotor rotor, int input, int direction)
{
return rotor[direction][input];
}
void init_rotor(nxigma_rotor rotor, char reflect2io[], char calibration)
{
int rshift = ord(calibration);
//printf("rshift = %d\n", rshift);
for (int i = 0; i < 26; ++i)
{
int trad = ord(reflect2io[i]);
rotor[retour][mod(i - rshift)] = mod(trad - rshift);
}
for (int i = 0; i < 26; ++i)
{
rotor[aller][rotor[retour][i]] = i;
}
}
int init_reflector(nxigma_reflector reflector, char reflection[])
{
for(int i = 0; i < modulo; ++i)
{
int rebond = ord(reflection[i]);
reflector[i] = rebond;
}
for(int i = 0; i < modulo; ++i)
{
if (reflector[reflector[i]] != i)
{
return i;
}
}
return -1;
}
int main(void)
{
nxigma_rotor rotorN, rotorX, rotorI;
nxigma_reflector reflectorNXI;
init_rotor(rotorN, "uwygadfpvzbeckmthxslrinqoj", 'n');
init_rotor(rotorX, "ajpczwrlfbdkotyuqgenhxmivs", 'x');
init_rotor(rotorI, "tagbpcsdqeufvnzhyixjwlrkom", 'i');
int i = init_reflector(reflectorNXI, "yruhqsldpxngokmiebfzcwvjat");
if (i >= 0)
{
fprintf(stderr, "'%c' => '%c'\n", chr(i), chr(reflectorNXI[i]));
fprintf(stderr, "'%c' => '%c'\n", chr(reflectorNXI[i]), chr(reflectorNXI[reflectorNXI[i]]));
}
char input[] = "avbkfrlkttcuc\0";
for(i = 0; i < strlen(input); ++i)
{
int cur = ord(input[i]);
cur = apply_rotor(rotorI, cur, aller);
cur = apply_rotor(rotorX, cur, aller);
cur = apply_rotor(rotorN, cur, aller);
cur = reflectorNXI[cur];
cur = apply_rotor(rotorN, cur, retour);
cur = apply_rotor(rotorX, cur, retour);
cur = apply_rotor(rotorI, cur, retour);
printf("%c", chr(cur));
}
printf("\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment