int encrypt_file()

in reversing/SOMBRERO_ROJO/crypto/rc4block.c [739:820]


int encrypt_file(char *infile, char *outfile, unsigned char key[RC4_KEY_BYTES])
{
	struct stat st;
	unsigned int size = 0;
	unsigned char *input = NULL;
	unsigned char *output;
	rc4ctx encrypted_ctx;

	if(key == NULL)
	{
		memset(&encrypted_ctx.key, 0x20, RC4_KEY_BYTES);
	}
	else{
		memcpy(&encrypted_ctx.key, key, RC4_KEY_BYTES);
	}
	
	FILE *fp = fopen(infile, "rb");
	FILE *fo = fopen(outfile, "wb");

	if(fp == 0 || fo == 0)
	{
		#ifdef DEBUG
			printf("[*]encrypt_file: File handle problem\n");
		#endif
		return -1;
	}

	stat(infile, &st);
	size = st.st_size;

	if(size <= 0)
	{
		#ifdef DEBUG
			fprintf(stderr, "[*]encrypt_file: Stat File size  Error!");
		#endif
		fclose(fp);
		fclose(fo);
		return -1;
	}
	

	input = (unsigned char *) calloc(size+1, sizeof(unsigned char));

	if(input == NULL)
	{
		#ifdef DEBUG
			fprintf(stderr, "[*]encrypt_file: Memory Error!");
		#endif
		fclose(fp);
		fclose(fo);
		return -1;
	}


	if(fread(input, sizeof(unsigned char), size, fp) < 0)
	{
		#ifdef DEBUG
			fprintf(stderr, "[*]encrypt_file: fread Error!\n");
		#endif
		fclose(fp);
		fclose(fo);
		return -1;
	}
	

	output = rc4_block_encrypt(&encrypted_ctx, input, size);

	if(output == NULL)
	{
		return -1;
	}

	encrypted_ctx.decrypted_size = size;

	fwrite(&encrypted_ctx, sizeof(rc4ctx), 1, fo);
	fwrite(output, sizeof(unsigned char), encrypted_ctx.encrypted_size, fo);


	fclose(fo);
	fclose(fp);
	return 0;
}