Exemple #1
0
int main()
{
    printf("1. Create an new encrypted File\n");
    printf("2. Encrypt a file\n");
    printf("3. Decrypt an encrypted File\n");
    int choice;
    printf("Enter your choice : ");
    scanf("%d",&choice);
    switch(choice)
    {
    case 1:
        encrypt();
        getch();
        return 0;
    case 2:
        encrypt_old();
        getch();
        return 0;
    case 3:
        decrypt();
        getch();
        return 0;
    }
}
Exemple #2
0
int main(int argc, char *argv[])
{
	FILE *fp = NULL;
	unsigned char *buf = NULL;
	int filesize;
	int mode = MODE_NEW;

	if (argc < 3) {
		fprintf(stderr, "usage: %s <input file> <output file> [-d|-e]\n", argv[0]);
		return EXIT_FAILURE;
	}

	if ((argc > 3) && (argv[3][0] == '-')) {
		switch (argv[3][1]) {
		case 'd':
			mode = MODE_DEC_OLD;
			break;
		case 'e':
			mode = MODE_ENC_OLD;
			break;
		}
	}

	fp = fopen(argv[1], "rb");
	if (fp == NULL) {
		fprintf(stderr, "Error: could not open input file '%s'\n", argv[1]);
		return EXIT_FAILURE;
	}

	fseek(fp, 0, SEEK_END);
	filesize = ftell(fp);
	buf = (unsigned char*)malloc(filesize);
	if (buf == NULL) {
		fprintf(stderr, "Error: could not allocate %i bytes\n", filesize);
		fclose(fp);
		return EXIT_FAILURE;
	}

	fseek(fp, 0, SEEK_SET);
	if (fread(buf, filesize, 1, fp) != 1) {
		fprintf(stderr, "Error: could not read from input file\n");
		fclose(fp);
		return EXIT_FAILURE;
	}

	fclose(fp);
	fp = fopen(argv[2], "wb");
	if (fp == NULL) {
		fprintf(stderr, "Error: could not open output file '%s'\n", argv[2]);
		return EXIT_FAILURE;
	}

	switch (mode) {
	case MODE_NEW:
		crypt_new(buf, filesize);
		break;
	case MODE_DEC_OLD:
		decrypt_old(buf, filesize);
		break;
	case MODE_ENC_OLD:
		encrypt_old(buf, filesize);
		break;
	}

	fwrite(buf, filesize, 1, fp);
	fclose(fp);
	free(buf);

	return EXIT_SUCCESS;
}