Написать программу для реализации процедуры скремблирования данных
Текст программы скремблирования:
Код:
#include <stdio.h>
#include <stdlib.h>
#define X 8
#define Y 17
void main(int argc, char* argv[]) {
if (argc < 3) exit(0);
FILE* inputFile;
if ((inputFile = fopen(argv[1], "rb")) == NULL) {
printf("Input File Error!\n");
exit(1);
}
FILE* outputFile;
if ((outputFile = fopen(argv[2], "wb")) == NULL) {
printf("Output File Error!\n");
exit(1);
}
unsigned char buf;
unsigned char B[Y];
unsigned char result;
unsigned char A;
unsigned char mask;
unsigned char temp;
int c = 0;
while (!feof(inputFile)) {
result = 0;
mask = 128;
fread(&buf, sizeof(unsigned char), 1, inputFile);
if (feof(inputFile)) break;
for (int i = 0; i < 8; i++) {
if (mask & buf) A = 1;
else A = 0;
if (c < X) {
result = result | (mask & buf);
B[c] = A;
}
else if (c < Y) {
temp = A ^ B[c - X];
result = result | (temp << (7 - i));
B[c] = temp;
}
else {
temp = A ^ B[Y - X] ^ B[0];
result = result | (temp << (7 - i));
for (int j = 0; j < Y - 1; j++) {
B[j] = B[j + 1];
}
B[Y - 1] = temp;
}
mask = mask >> 1;
c++;
}
fwrite(&result, sizeof(unsigned char), 1, outputFile);
}
fclose(inputFile);
fclose(outputFile);
}
Текст программы дескремблирования:
Код:
#include <stdio.h>
#include <stdlib.h>
#define X 8
#define Y 17
void main(int argc, char* argv[]) {
if (argc < 3) exit(0);
FILE* inputFile;
if ((inputFile = fopen(argv[1], "rb")) == NULL) {
printf("Input File Error!\n");
exit(1);
}
FILE* outputFile;
if ((outputFile = fopen(argv[2], "wb")) == NULL) {
printf("Output File Error!\n");
exit(1);
}
unsigned char buf;
unsigned char B[Y];
unsigned char result;
unsigned char _B;
unsigned char mask;
unsigned char temp;
int c = 0;
while (!feof(inputFile)) {
result = 0;
mask = 128;
fread(&buf, sizeof(unsigned char), 1, inputFile);
if (feof(inputFile)) break;
for (int i = 0; i < 8; i++) {
if (mask & buf) _B = 1;
else _B = 0;
if (c < X) {
result = result | (mask & buf);
B[c] = _B;
}
else if (c < Y) {
temp = _B ^ B[c - X];
result = result | (temp << (7 - i));
B[c] = _B;
}
else {
temp = _B ^ B[Y - X] ^ B[0];
result = result | (temp << (7 - i));
for (int j = 0; j < Y - 1; j++) {
B[j] = B[j + 1];
}
B[Y - 1] = _B;
}
mask = mask >> 1;
c++;
}
fwrite(&result, sizeof(unsigned char), 1, outputFile);
}
fclose(inputFile);
fclose(outputFile);
}
Верю... Надеюсь... Люблю...