Cryptage

Contenu du snippet

Et bien c'est tout simple. C'est un programme en C ANSI (console dos) qui crypte le fichier passer en argument.
Le cryptage utilise une clé et la fonction "pseudo aléatoire". En clair chaque caractères de votre fichier subbit une inversion de bit avec un caractère pris "au hasard" dans votre mot de passe.
Ca peut paraît dangereux mais y a pas de soucis on retrouvera toujours son fichier une fois décrypter, enfin si on se trompe pas de password parceque dans ce cas là je vous souhaite bien du courage pour retrouver votre fichier.

Source / Exemple :


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <conio.h>

struct trans
{
	char o_chemin[256];
	char o_fichier[256];
	char dest_chemin[256];
	char dest_fichier[256];
};

long fsize(FILE*);
void transfert(struct trans *,int );
int decrypt(int ,int );
int crypt(int ,int );

int main(int argc, char **argv)
{
	int T_char;
	int reper = 0;
	int i;
	char fichier[256];
	char chemin[256];
	struct trans *tr;
	char choix;

	if(argc==2)
	{
		/* décompose l'adresse du fichier en 2 */
		/* son Chemin dans "chemin" et son nom dans "fichier" */
		strcpy(fichier,argv[1]);
		T_char = strlen(fichier);
		while(fichier[T_char-(++reper)] != 92);	/* != '\'	*/
		reper--;
		strncpy(chemin,fichier,T_char-reper);
		chemin[T_char-reper] = '\0';
		i = reper;
		while(fichier[T_char-reper-1] != '\0')
			fichier[i-reper] = fichier[T_char-(reper--)];
/**********************************************************************************/
		/* définir la structure trans pour le cryptage */
		tr = (struct trans*)malloc(1 * sizeof(struct trans));
		strcpy(tr->o_chemin,chemin);
		strcpy(tr->o_fichier,fichier);
		strcpy(tr->dest_chemin,chemin);
		strcpy(tr->dest_fichier,"cryp_");
		strcat(tr->dest_fichier,fichier);
/**********************************************************************************/
		printf("\n\n");
		printf("\t\tÉÍÍÍÍÍÍÍËÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»\n");
		printf("\t\tº   1   º    Crypter le fichier    º\n");
		printf("\t\tÌÍÍÍÍÍÍÍÎÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ͹\n");
		printf("\t\tº   2   º   Decrypter le fichier   º\n");
		printf("\t\tÈÍÍÍÍÍÍÍÊÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ͌\n\n\n");

		do
		{
			printf("Entrez votre choix : ");
			scanf("%c", &choix);
			printf("\n\n\n",choix);
		}while((choix!='1') && (choix!='2'));

		fflush(stdin);
		transfert(tr, choix);
	}
	return(EXIT_SUCCESS);
}

void transfert(struct trans *ploc,int choix)
{
	char pwd[128];
	char pwd2[128];
	int T_char;
	char origin[256];
	char dest[256];
	long oct = 0;
	long size = 0;
	int c;
	int i = 0;
	FILE *fcopier = NULL;
	FILE *fcoller = NULL;
	time_t start, finish;
	double durer;
	do
	{
		printf("\n\tVeuillez entrer votre mot de passe : ");
		fgets(pwd,128,stdin);
		printf("\tConfirmer votre mot de passe : ");
		fgets(pwd2,128,stdin);
	}while(strcmp(pwd,pwd2)!=0);
	T_char=strlen(pwd);

	strcpy(origin,ploc->o_chemin);
	strcat(origin,ploc->o_fichier);
	strcpy(dest,ploc->dest_chemin);
	strcat(dest,ploc->dest_fichier);
	printf("%s\n",dest);
	start = time(NULL);
	fcoller=fopen(dest,"wb");
	fcopier=fopen(origin,"rb");
	size = fsize(fcopier);
	switch(choix)
	{
	case '1':
		printf("\n\n\t\tCRYPTAGE\n\n");
		while((c = getc(fcopier)) != EOF)
		{
			c = crypt(c,pwd[rand() % T_char]);
			putc(c,fcoller);
			if(oct++%(size/100) == 0)
				printf("%d%%\r",i++);
		}
		break;
	case '2':
		printf("\n\n\t\tDECRYPTAGE\n\n");
		while((c = getc(fcopier)) != EOF)
		{
			c = decrypt(c,pwd[rand() % T_char]);
			putc(c,fcoller);
			if(oct++%(size/100) == 0)
				printf("%d%%\r",i++);
		}
		break;
	}
	printf("\n");
	fclose(fcopier);
	fclose(fcoller);
	finish = time(NULL);
	durer = difftime(finish,start);
	printf("%d octect en %.0f secondes\n\n",size,durer);
	system("pause");
	remove(origin);
	rename(dest,origin);
}

/*taille du fichier (binaire, pas texte) */
long fsize(FILE* fd)
{
	long savepos, size;

	savepos = ftell(fd); /* sauvegarder la position */
	fseek(fd, 0, SEEK_END); /* aller en fin */
	size = ftell(fd); /* lire la taille */
	fseek(fd, savepos, SEEK_SET); /* rétablir la position */
	return size; 
}

int crypt(int c,int d)
{
	return(d^c);
}

int decrypt(int c, int d)
{
	return(d^c);
}

Conclusion :


Le code est un peu brouillon mais pour ma défence c'est l'un de mes premier code alors je testais souvent plusieurs truc dans un programme, mais bon ça marche.
Il y a aussi un menu "Crypter/DECRYPTER" au cas ou je trouverai une autre manière de crypter mais en faite non : la fonction crypt() et decrypt sont identique. Comme le code est assez simple vous devriez pas avoir de soucis pour le modifier.
En faite pour la petit astuce j'ai mis le prog dans "%USERPROFILE%\SendTo" ça simplifie encore plus les chose.

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.