Blame view

src/main.c 3.32 KB
17ba8f8f   tevrard   Ajout du code
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
  #include <stdio.h>
  #include <stdlib.h>
  #include <string.h>
  #include <stdint.h>
  #include "../inc/stegano.h"
  #include "../inc/automatos.h"
  #include "../inc/vigenere.h"
  
  
  void help() {
  
  	printf("\nHow to use this program :\n");
  	printf("Hide message into img :\n");
  	printf("	./tp_tim -e -lsb <image.ppm> \n");
  	printf("	            -par <image.ppm> \n");
  	printf("You can crypt your message with -Vigenere at the end of the command\n\n");
  
  	printf("Recover message into img :\n");
  	printf("	./tp_tim -d -lsb <image.ppm> \n");
  	printf("	            -par <image.ppm> \n");
  	printf("You must specify -Vigenere at the end of the command if a cypher text is hiding into the image\n\n");
  
  	printf("\nEXAMPLES : \n");
  	printf("./tp_tim -e -lsb chaton.ppm -Vigenere\n");
  	printf("./tp_tim -d -lsb chaton_enc.ppm -Vigenere\n");
  
  	printf("\nNOTES :\n");
  	printf("Lors de la dissimulation d'un message, l'image qui en résulte sera sauvegardee sous le nom de 'encrypt.ppm'\n");
  
  }
  
  /* Permet de vider le buffer */
  void clean_stdin(void) {
  
  	int c;
  	do {
  		c = getchar();
  	} while (c != '\n');
  }
  
  int main(int argc, char *argv[])
  {
  	int vig = 0;
  	unsigned char *cle = NULL;
  	if (argc > 3) {
  
  		Image *I;
  		if (chargement_ppm(argv[3], &I) == -1) return -1;
  		printf("Chargement OK\n");
  		uint8_t max_size = (((I->width)/2) * ((I->height)/2) / 8);
  
  
  		/* ENCODE */
  		if(strcmp(argv[1], "-e") == 0) {
  
  			/* Recuperation du message à cacher */ 
  			unsigned char *msg_s = malloc(max_size);
  			printf("Message a cacher (taille max = %d): ", max_size);
      		fgets((char * restrict)msg_s, max_size, stdin);
      		
      		/* Vidage du buffer si msg > max_size */
      		if (strlen((const char *)msg_s) >= max_size-1.0) clean_stdin();
  
      		/* CHIFFREMENT AVEC VIGENERE */
      		if (argc == 5) {
      			vig = 1;
      			printf("plaintext : %s\n", msg_s);
  
      			cle = malloc(max_size);
      			memset( cle, 0, max_size);
  				printf("Cle vigenere : ");
      			fgets((char * restrict)cle, max_size, stdin);
      			vigenere(msg_s, cle, strlen((const char *)cle), 1);
  
      			printf("cypher : %s\n", msg_s);
      		} 
  
  			/* LSB Methode ou PARITÉ Methode ou ERREUR */
  			if(strcmp(argv[2], "-lsb") == 0) LSB_hide(&I, msg_s, 1, vig, cle);
  			else if (strcmp(argv[2], "-par") == 0) Parity_encode(&I, msg_s);
  			else {
  				printf("wrong option [ MUST BE -par OR -lsb ]\n");
  			}
  
  			sauvegarde_ppm("encrypt.ppm", &I);
  
  			free(msg_s);
  			if (argc == 5) free(cle);
  		}
  
  
  		/* DECODE */
  		else if(strcmp(argv[1], "-d") == 0) {
  
  			unsigned char *buff = malloc(max_size);
  			memset( buff, 0, max_size);
  
  			/* CHIFFREMENT AVEC VIGENERE */
      		if (argc == 5) {
      			vig = 1;
      			cle = malloc(max_size);
      			memset( cle, 0, max_size);
  				printf("Cle vigenere : ");
      			fgets((char * restrict)cle, max_size, stdin);
      			if (strlen((const char *)cle) <= 1) return -1;
      		} 
  
      		/* LSB Methode ou PARITÉ Methode ou ERREUR */
  			if(strcmp(argv[2], "-lsb") == 0) LSB_hide(&I, buff, 0, vig, cle);
  			else if (strcmp(argv[2], "-par") == 0) Parity_decode(&I, buff);
  			else {
  				printf("wrong option [ MUST BE -par OR -lsb ]\n");
  			}
  
  			if (argc == 5) {
  				printf("Cypher : %s\n", buff);
  				vigenere(buff, cle, strlen((const char *)cle), 0);
  				printf("out\n");
  				free(cle);
  			}
  
      		printf("plaintext : %s\n", buff);
  			free(buff);
  		}
  		else help();
  		free_image(&I);
  	}
  	else {
  		help();
  	}
  	return 0;
  }