#include #include #include #include long diviser (long x, long y) { if (y == 0) return 0x7FFFFFFF; return x / y; } long carre (long x) { return x * x; } long mod (long m, long n) { long r; r = n % m; if (r < 0) r += m; return r; } int hasard () { int x; x = 0; for (;;) { if (random(6) == 0) return x; x++; } } #define NEW #define TEST #define TESTF /* #define TESTC */ #ifndef TEST /* char codex[] = { 105, 2, 106, 4, 112, 15, 96, 3, 114, 1, 109, 72, 23, 51, 108, 16, 90, 89, 81, 56, 111, 6, 94, 63, 2, 98, 61, 56, 103, 7, 27, 101, 13, 94, 55 }; */ char codex[] = { 48, 51, 16, 29, 48, 10, 6, 56, 64, 13, 14, 5, 41, 44, 50, 15, 39, 60, 48, 10, 2, 29, 66, 52, 40, 47, 36, 16, 67, 23 }; /* char codex[] = { 16, 45, 11, 19, 78, 29, 32, 28, 25, 74, 9, 3, 85, 84, 12, 63, 69, 19, 4, 76, 45, 3, 28, 11, 60, 28, 25, 29, 70, 34, 80, 24, 84, 31, 11, 67, 19, 66, 54, 55, 73, 12, 47, 53, 62, 36, 10, 14, 43, 13, 80, 34, 84, 18, 36 }; */ #else char codex[] = "BONJOURCECIESTUNTESTDEDECODAGEAUREVOIR"; /* transopsition rectangulaire */ /* char codex[] = "BRSSCAIOCTTOURNEUDDRZJCNEAEZOITDGVZUEEEEOZ"; */ /* transposition diagonale 4x3 */ /* char codex[] = "BNUEOOCIJRCESUEDTTTDNSEECDEROGUVRAEOIR"; */ /* char codex[] = "BOOEUNCRJICESTTDEUESNDTECGOREDEAAVUOIR"; */ /* transposition cle TRESOR */ /* char codex[] = "NEUDDROITDGVOCTTOURUEEEEOJCNEAEBRSSCAI"; */ /* transposition inverse cle TRESOR */ /* char codex[] = "USBORTRTODCDEUNAEEUNJGCDOTOEIEIEUAECRS"; */ #endif char code[] = "QWERTYUIOPASDFGHJKLZXCVBNM"; #ifdef TEST #define LCODEX (sizeof(codex)-1) #else #define LCODEX (sizeof(codex)) #endif char codex_t[LCODEX]; int buf[1000]; init_codex () { int i; char buf[1000]; #ifdef TEST for (i=0; iv) #define distance_codex (it_codex->distance) #define MAXKEYS 10 int n_keys = 0; char key[MAXKEYS][LCODEX+1]; caractere lire1 (FILE *f) { char c; for (;;) { c = fgetc (f); if (c == EOF) return EOF; if (c >= 'A' && c <= 'Z') return c - 'A'; if (c >= 'a' && c <= 'z') return c - 'a'; #if SEP return NCAR-1; #endif } } caractere lire (FILE *f) { caractere c; c = lire1 (f); if (c != EOF) lng_model++; return c; } caractere lire_fichier (void *p) { return lire (p); } struct chaine { char *s; int n; }; caractere lire_chaine (struct chaine *p) { caractere c; if (p->n <= 0) return EOF; c = p->s[0]; p->s++; p->n--; return c; } trier (caractere esartin[], int frequence1[]) { int i, j, k; for (i=0; i frequence1[esartin[j]] || (frequence1[i] == frequence1[esartin[j]] && random(2))) { for (k=i-1; k>=j; k--) esartin[k+1] = esartin[k]; esartin[j] = i; goto suite; } } esartin[i] = i; suite: ; } } calcul_frequence (FILE *f, compte frequence[], int frequence1[NCAR], int frequence2[NCAR][NCAR], caractere esartin[], caractere (*lir)(/* void * */), void *p) { char buf[LNG]; int i, j, k; long x; caractere c; x = 0; for (i=0; i= MAXCOMPTE) break; c = lire (f); if (ptr_debut < LCODEX) debut[ptr_debut++] = c; if (c == EOF) break; x %= NS/NCAR; x *= NCAR; x += c; } for (i=0; i r) return i; } } generer (compte frequence[], int n) { long x; caractere c; int i; /* x = random (NS/NCAR); */ x = x1 / NCAR; for (i=0; i', ':', '@', 't', '\\', 'K' #ifdef OP_ANAFREQ , '?' #endif }; char *codops[] = { "+", "-", "*", "/", "%", "&", "|", " XOR ", ".", "~", "0", "1", "2", "F", "N", "d", "s", "c", "(1+)", "(-1+)" , "2*", "1/2*", "r", ">", ":", "@", "t", "\\", "K" #ifdef OP_ANAFREQ , "?" #endif }; #define NBROP (sizeof(codop)/sizeof(codop[0])) #if (sizeof(arite)/sizeof(arite[0]) != NBROP) || op_fin != NBROP #error Erreur dans la definition des operateurs #endif #ifdef REEL typedef double nombre; #else typedef long nombre; #endif anafreq_ordre (char *s, char *u, int ordre) { int i, j, i1, j1, k, l; char s1[LCODEX]; int freq1[NCAR]; char esarti[NCAR]; int freq2[NCAR][NCAR]; /*long*/ nombre corresp [NCAR] [NCAR]; char tri_corresp[NCAR*NCAR][2]; char transcod [2][NCAR]; nombre frequence_rel, freq_rel; /* for (i=0; i=k; l--) { tri_corresp[l+1][0] = tri_corresp[l][0]; tri_corresp[l+1][1] = tri_corresp[l][1]; } tri_corresp[k][0] = i; tri_corresp[k][1] = j; goto suite; } } tri_corresp[i*NCAR+j][0] = i; tri_corresp[i*NCAR+j][1] = j; suite: ; } for (i=0; i<2; i++) for (j=0; j= LCODEX) break; } if (l == 0) { l = c+1; if (l > x + y) break; c = 0; } else { l--; c++; } } for (l=0; l= LCODEX) return; } } } det_cle_num (int *cle_num, int *cle_inv, char *cle) { int c; int i, j, l; l = strlen (cle); j = 0; for (c=0; c<0x100; c++) { for (i=0; i 0) { transpos_cle (u, s, key[x%n_keys], z); return; } else u[i] = s[i]; break; default : printf ("operation %d non definie\n", op); u[i] = 0; } } } long val_codex; #define NUMA 1 #define N_CAR NCAR long valeur (char *s) { char s1[LCODEX+1]; int i; long val; for (i=0; iv[V_FREQUENCE] = valeur (s); it->v[V_REPET] = v_repet (s) + 1; it->distance = distance_v (it->v, v_opt); } set_codex (struct item *it) { int i, j, k, lk; char buf[1000]; #if 0 memcpy (it->s, codex, LCODEX); /* memcpy (it->t, codex_t, LCODEX); */ strcpy (it->f, "C"); #else /* n_transpos++; if (n_transpos >= LCODEX) n_transpos = 2; */ if (n_keys > 0 && random(2)) { i = random (n_keys); lk = strlen (key[i]); for (j=0; js[j] = key [i] [mod(lk,j)] - 'A' + 1; sprintf (it->f, "K%d", i); anafreq (it->s, it->t); calcul_v (it, it->t); return; } n_transpos = hasard (); if (random(2)) n_transpos = 1; for (j=0; js[j] = codex[mod(LCODEX,j*n_transpos)]; sprintf (buf, "C%d", n_transpos); strcpy (it->f, buf); #endif /* it->val = val_codex; */ anafreq (it->s, it->t); if (flag_anafreq) { calcul_v (it, it->t); /* it->v[V_FREQUENCE] = valeur (it->t); it->v[V_REPET] = v_repet (it->t); */ } else { for (k=0; kv[k] = v_codex[k]; it->distance = distance_codex; /* it->v[V_FREQUENCE] = v_frequence_codex; it->v[V_REPET] = v_repet_codex; it->val = val_codex; */ } } init_tableau () { int i; anafreq (codex, codex_t); if (flag_anafreq) { calcul_v (it_codex, codex_t); /* v_codex[V_FREQUENCE] = valeur (codex_t); v_codex[V_REPET] = v_repet (codex_t); distance_codex = distance_v (v_codex, v_opt); */ } else { calcul_v (it_codex, codex); /* v_codex[V_FREQUENCE] = valeur (codex); v_codex[V_REPET] = v_repet (codex); distance_codex = distance_v (v_codex, v_opt); */ } for (i=0; if); for (i=0; if[i] == 'C' || it->f[i] == 'c' /*|| it->f[i] == 'k'*/) goto suite; } return LM+1; suite: for (i=0; is, meilleur[i].s, LCODEX)) return LM+1; /* if (it->val > meilleur[i].val) */ /* if (abs(it->val - val_opt) < abs (meilleur[i].val - val_opt)) */ /* if (abs (it->v[V_FREQUENCE] - v_opt[V_FREQUENCE]) < abs (meilleur[i].v[V_FREQUENCE] - v_opt[V_FREQUENCE])) */ if (it->distance < meilleur[i].distance) { /*if (memcmp (it->s, meilleur[i].s, LCODEX)) {*/ for (j=LM-2; j>=i; j--) memcpy (meilleur+j+1, meilleur+j, sizeof(struct item)); memcpy (meilleur+i, it, sizeof(struct item)); return i; /*}*/ } } return LM+1; } #define NMA 6 int nma = NMA; afficher_s (char *s) { int i; char s1[LCODEX]; char c; #if NMA > 6 if (!flag_anafreq) #endif for (i=0; i 'Z') c = '.'; printf ("%c", c /*'A' + (((s[i]-NUMA)%N_CAR)+N_CAR)%N_CAR*/); } #if NMA <= 6 printf ("\n"); #else /* printf (" "); */ #endif anafreq (s, s1); #if NMA > 6 if (flag_anafreq) #endif for (i=0; i 'Z') c = '.'; printf ("%c", c /*'A' + (((s1[i]-NUMA)%N_CAR)+N_CAR)%N_CAR*/); } } afficher_meilleurs () { int i; char buf[200]; /* clrscr (); */ gotoxy (1, 1); for (i=0; i=LFORMULE) { set_codex (tableau+k); /* memcpy (tableau[k].s, codex, LCODEX); memcpy (tableau[k].t, codex_t, LCODEX); strcpy (tableau[k].f, "c"); tableau[k].val = val_codex; */ } else { op = random (NBROP); operation (op, tableau[i].s, tableau[j].s, tableau[k].s, x, y, z); anafreq (tableau[k].s, tableau[k].t); switch (arite[op]) { case 0 : sprintf (tableau[k].f, "%c", codop[op]); break; case 1: if (op == op_transpos) sprintf (tableau[k].f, "%c%d %s", codop[op], x, tableau[i].f); else if (op == op_diag) sprintf (tableau[k].f, "%c%X(%d,%d)%s", codop[op], z, x, y, tableau[i].f); else if (op == op_transpos_cle) sprintf (tableau[k].f, "%c%X(%d)%s", codop[op], z, x, tableau[i].f); else sprintf (tableau[k].f, "%c%s", codop[op], tableau[i].f); break; case 2 : sprintf (tableau[k].f, "(%s%c%s)", tableau[i].f, codop[op], tableau[j].f); break; } if (flag_anafreq) { calcul_v (tableau+k, tableau[k].t); /* tableau[k].v[V_FREQUENCE] = valeur (tableau[k].t); tableau[k].v[V_REPET] = v_repet (tableau[k].t); */ } else { calcul_v (tableau+k, tableau[k].s); /* tableau[k].v[V_FREQUENCE] = valeur (tableau[k].s); tableau[k].v[V_REPET] = v_repet (tableau[k].s]); */ } } status = inserer (tableau+k); if (status < NMA) afficher_meilleurs (); if (kbhit ()) return 0; return 1; } int donnees[] = { 37, 0, 11, 28, 26, 65, 60, 13, 31, 13, 14, 25, 30, 4, 64, 111, 124, 43, 64, 8, 7, 65, 24, 23, 25, 20, 17, 12, 78, 16, 25, 0, 26, 16, 18, 12, 29, 65, 28, 12, 5, 21, 2, 26, 78, 17, 3, 16, 4, 65, 11, 78, 11, 21, 13, 21, 19, 65, 85, 69, 78, 11, 9, 69, 6, 4, 9, 26, 11, 65, 29, 16, 19, 65, 11, 8, 78, 23, 5, 9, 26, 4, 71, 25, 1, 20, 30, 23, 23, 8, 19, 99, 11, 21, 30, 0, 86, 39, 21, 12, 4, 20, 31, 75, 124, 107, 35, 78, 15, 17, 30, 0, 5, 65, 11, 12, 78, 6, 25, 12, 18, 4, 71, 46, 15, 13, 0, 12, 27, 0, 21, 13, 78, 18, 25, 23, 86, 13, 2, 73, 56, 0, 30, 73, 86, 5, 6, 7, 29, 65, 0, 4, 86, 2, 14, 29, 11, 65, 9, 21, 31, 18, 4, 6, 30, 0, 0, 0, 86, 5, 2, 73, 40, 19, 9, 15, 3, 18, 75, 99, 7, 13, 76, 28, 86, 0, 71, 28, 0, 65, 15, 9, 25, 8, 19, 27, 11, 65, 13, 19, 19, 2, 71, 13, 11, 18, 76, 21, 23, 15, 9, 12, 15, 20, 20, 69, 6, 4, 14, 7, 26, 18, 76, 23, 19, 17, 21, 12, 29, 4, 2, 17, 23, 15, 19, 73, 10, 4, 31, 69, 23, 15, 14, 4, 15, 20, 20, 111, 16, 0, 9, 29, 15, 18, 24, 12, 7, 20, 2, 26, 78, 4, 26, 10, 7, 20, 6, 7, 26, 65, 0, 66, 55, 17, 8, 10, 15, 13, 21, 21, 5, 4, 73, 99, 39, 13, 76, 28, 86, 0, 71, 8, 27, 18, 31, 12, 86, 17, 11, 28, 29, 8, 9, 16, 4, 18, 71, 26, 26, 0, 24, 16, 19, 18, 71, 13, 11, 65, 8, 23, 23, 6, 8, 7, 29, 65, 13, 69, 48, 19, 2, 3, 27, 18, 64, 69, 23, 20, 19, 27, 11, 65, 28, 10, 5, 18, 14, 11, 7, 13, 5, 17, 19, 65, 23, 6, 27, 19, 102, 9, 23, 65, 5, 12, 26, 4, 76, 8, 23, 13, 1, 8, 7, 18, 13, 11, 2, 4, 73, 99, 100, 40, 0, 69, 15, 65, 6, 73, 27, 15, 76, 31, 25, 14, 71, 8, 24, 4, 15, 69, 19, 15, 19, 27, 11, 65, 13, 16, 2, 19, 2, 26, 78, 5, 9, 22, 86, 7, 11, 8, 3, 0, 2, 17, 5, 65, 22, 28, 7, 65, 28, 10, 3, 19, 21, 8, 7, 4, 2, 17, 86, 4, 19, 27, 11, 65, 0, 0, 5, 65, 6, 7, 7, 12, 13, 16, 14, 107, 0, 27, 15, 2, 5, 0, 3, 25, 73, 99, 100, 37, 75, 4, 6, 19, 2, 26, 78, 67, 32, 0, 5, 65, 0, 27, 15, 15, 8, 22, 86, 12, 30, 26, 26, 4, 30, 0, 5, 65, 74, 73, 35, 24, 31, 17, 19, 19, 2, 26, 78, 5, 9, 22, 86, 17, 21, 12, 10, 8, 15, 17, 31, 14, 9, 26, 76, 65, 28, 9, 3, 18, 14, 12, 27, 19, 31, 69, 18, 4, 20, 99, 30, 19, 3, 21, 30, 4, 19, 0, 11, 18, 76, 1, 19, 65, 41, 6, 29, 21, 30, 4, 18, 0, 10, 28, 29, 65, 31, 0, 86, 19, 6, 25, 30, 14, 30, 17, 19, 19, 6, 0, 11, 15, 24, 69, 23, 65, 41, 8, 30, 14, 0, 0, 25, 15, 71, 43, 1, 15, 13, 21, 23, 19, 19, 12, 66, 65, 28, 4, 4, 65, 2, 17, 11, 12, 28, 9, 19, 65, 93, 99, 100, 65, 76, 69, 86, 65, 71, 73, 78, 37, 75, 16, 24, 65, 9, 6, 3, 65, 29, 16, 19, 65, 9, 28, 2, 65, 30, 10, 31, 65, 3, 12, 78, 39, 30, 4, 24, 2, 2, 73, 30, 14, 30, 17, 23, 65, 13, 8, 3, 0, 5, 22, 90, 107, 71, 73, 78, 65, 76, 69, 86, 65, 41, 78, 23, 65, 9, 16, 2, 65, 13, 8, 3, 0, 5, 22, 86, 7, 8, 28, 10, 19, 9, 69, 5, 8, 71, 29, 11, 19, 30, 12, 16, 8, 6, 7, 26, 4, 102, 69, 86, 65, 71, 73, 78, 65, 76, 44, 2, 0, 11, 0, 11, 77, 76, 32, 5, 17, 6, 14, 0, 4, 64, 69, 19, 21, 71, 5, 11, 18, 76, 36, 24, 6, 11, 8, 7, 18, 76, 0, 24, 65, 19, 27, 11, 12, 14, 9, 19, 15, 19, 69, 100, 104, 43, 23, 23, 15, 3, 12, 78, 0, 24, 17, 19, 15, 19, 0, 1, 15, 76, 21, 25, 19, 19, 12, 28, 0, 76, 4, 3, 25, 71, 15, 11, 12, 1, 0, 5, 65, 2, 29, 28, 0, 2, 2, 19, 19, 2, 26, 64, 107, 102, 33, 81, 0, 23, 27, 11, 18, 76, 9, 81, 32, 19, 5, 15, 18, 76, 13, 31, 18, 19, 6, 28, 8, 29, 16, 19, 65, 38, 27, 3, 0, 2, 1, 86, 34, 8, 5, 7, 15, 64, 69, 52, 14, 9, 8, 30, 0, 30, 17, 19, 65, 3, 12, 12, 0, 30, 20, 3, 4, 71, 8, 78, 39, 30, 0, 28, 20, 20, 73, 11, 15, 76, 84, 65, 88, 94, 99, 15, 20, 76, 23, 19, 21, 8, 28, 28, 65, 8, 66, 51, 6, 30, 25, 26, 4, 76, 0, 2, 65, 20, 78, 23, 65, 9, 8, 20, 0, 21, 24, 27, 4, 76, 0, 24, 65, 86, 81, 95, 85, 76, 21, 25, 20, 21, 73, 2, 70, 5, 9, 19, 65, 3, 78, 43, 13, 14, 0, 88, 107, 109, -1 }; int sl (char *str) { int i; int s; s = 0; for (i=0; i=0; i++) { putchar (donnees[i] ^ buf[i%l]); } printf ("Appuyer sur Entree pour continuer...\n"); gets (buf); } main (int argc, char *argv[]) { FILE *f; int i; char buf[1000], buf1[1000]; long val; long v[NV]; /* afficher_donnees (); */ printf ("%d %d %d\n", NBROP, sizeof(arite)/sizeof(arite[0]), op_fin); init_codex (); randomize (); for (i=0; i