Salut tout le monde,
Alors voilà je planche sur un petit Exo de cryptage, j'ai déjà passé quelques jours dessus à cause d'allocation dynamique mais cette fois j'arrive absolument pas à mettre la main sur le problème. Je débute encore dans la programmation et j'ai du mal avec l’algorithme ( me faut plus de pratique )
Mon programme s’exécute jusqu'à la fin mais bug, il m'affiche pourtant ce que je veux. Je pense que cela vient d'un problème d'allocation mémoire mais je sens que je ne suis pas assez expérimenté dans les pointeurs et les mallocs cette fois pour trouver le problème. J'ai retourné le problème de plusieurs façon en changeant les mallocs, en changeant les free ( le free(crypter) bug plus précisément ) etc ....
Voici mes fonctions avant le main, transDecBin je la met juste au cas où elle poserait problème aussi mais je pense pas.
Mon main : Il doit afficher mon mot crypté avec la Matrice mais à la fin du programme il se termine mal. J'ai de grosse allocation mémoire mais vu la taille en sortie du int * je suis un peu obligé :/ !
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 int *transDecBin(int Dec) // Méthode transformer le Décimal en Binaire { int *binaireOrdone; binaireOrdone = malloc(sizeof(int)*8); int i=0, j=0, k=0, l=0; int bin[20]; for(l=0; l< 8; l++) { bin[l]=Dec%2; Dec = Dec/2; k++; } for(i = k-1; i>= 0; i--) { binaireOrdone[j] = bin[i]; j++; } return binaireOrdone; } int *cryptage(int *motBin, int matrice[4][16], char *mot1) { int *coder; coder = malloc(sizeof(strlen(mot1)*32)); int j,p=0, b; int reste[4] = {0,0,0,0}; int o = 0; for(p=0; p < strlen(mot1)*32; p+=4) { for(j=0; j<16; j++)//COLONNE PAR COLONNE { //LIGNE PAR LIGNE for(b=0; b<4;b++) reste[b] = motBin[b+p]*matrice[b][j];// On * la I case de coder avec la I ligne de matrice et la J colonne de matrice coder[p+j+o]=reste[0]^reste[1]^reste[2]^reste[3]; } o+=12; } return coder; }
Pour l'instant la partie avec le fichier ne devrait pas poser problème, la suite de mon programme devrait m'envoyer tout mon mot crypter dans un fichier.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58 int main(int argc, char **argv) { int i, j, l=0; char mot[100]; FILE* f; int G4[4][16]={ // Matrice G4 {1,0,1,1,1,0,0,0,0,0,1,1,1,0,1,1}, {1,1,1,0,1,0,1,0,0,1,0,1,0,1,0,1}, {0,0,1,0,1,0,1,1,1,1,1,0,0,0,1,0}, {0,0,0,0,1,1,0,0,1,1,1,1,1,1,0,1} }; printf("Saisir un mot\n"); fflush(stdin); fgets(mot, 100, stdin); mot[strlen(mot)-1]='\0'; int *motEntier; motEntier = malloc(sizeof(int)*strlen(mot)*8); int *m; m = malloc(sizeof(int)*strlen(mot)*8); int *crypter; crypter = malloc(sizeof(int)*strlen(mot)*32); for(j =0; j < strlen(mot); j++) // Double boucle pour transformer le Decimal en Binaire { motEntier = transDecBin(mot[j]); //lettre par lettre for(i = 0; i < 8; i++) { m[l] = motEntier[i]; l++; } } crypter = cryptage(m, G4, mot); for(i=0; i < strlen(mot)*32; i++) printf("%d", crypter[i]); f = fopen("outc.txt", "w+"); if(f == NULL ) { for(i=0; i< strlen(mot)*32; i++) fprintf(f, "%d", crypter[i]); printf("\nL'ecriture a fonctionne"); fclose(f); } else printf("Fichier inexistant"); //AFFICHER tout le mot en Binaire /*for(k=0; k < 8*strlen(mot); k++) printf("%d", m[k]);*/ free(motEntier); free(m); free(crypter); return 0; }
Merci d'avance
Partager