main.c 3.32 KB
#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;
}