2025-05-13 21:22:16
#xorf.c
#include <stdio.h>
#include <stdlib.h>
void xorFile(const char *input_filename, const char *key_filename, const char *output_filename) {
FILE *input_file = fopen(input_filename, "rb");
if (!input_file) {
perror("Error opening input file");
exit(1);
}
FILE *key_file = fopen(key_filename, "rb");
if (!key_file) {
perror("Error opening key file");
fclose(input_file);
exit(1);
}
FILE *output_file = fopen(output_filename, "wb");
if (!output_file) {
perror("Error opening output file");
fclose(input_file);
fclose(key_file);
exit(1);
}
//Динамическое выделение памяти для ключа
fseek(key_file, 0, SEEK_END);
size_t key_length = ftell(key_file);
fseek(key_file, 0, SEEK_SET);
unsigned char *key = malloc(key_length);
if (!key) {
perror("Memory allocation failed");
fclose(input_file);
fclose(key_file);
fclose(output_file);
exit(1);
}
fread(key, 1, key_length, key_file);
unsigned char input_byte;
size_t key_index = 0;
// Считываем входной файл и выполняем XOR с ключом
while (fread(&input_byte, 1, 1, input_file) > 0) {
// Выполняем XOR с текущим байтом ключа
unsigned char output_byte = input_byte ^ key[key_index];
fwrite(&output_byte, 1, 1, output_file);
// Переход к следующему байту ключа (зацикливаем)
key_index = (key_index + 1) % key_length;
}
free(key); // Освобождаем память
fclose(input_file);
fclose(key_file);
fclose(output_file);
printf("Output written to %s\n", output_filename);
}
int main(int argc, char *argv[]) {
if (argc != 4) {
fprintf(stderr, "Usage: %s <input file> <key file> <output file>\n", argv[0]);
return 1;
}
xorFile(argv[1], argv[2], argv[3]);
return 0;
}
Back to list