#include <stdio.h>
#include <iostream>
#include <fstream>
#include "enc_dec.h"
#include "rand_key.h"
int main()
{
unsigned char inbuff[AES_BLOCK_SIZE]; // AES_BLOCK_SIZE-битный блок
unsigned char outbuff[AES_BLOCK_SIZE]; // зашифрованный блок
unsigned char key[AES_KEY_SIZE]; // AES_KEY_SIZE-битный ключ
struct aes_key exp_key; // expanded key
unsigned char i;
char end[2] = "\0";
/* инициализируем файлы */
FILE *inFile;
FILE *crFile;
FILE *keyFile;
FILE *outFile;
inFile = fopen("input.txt","rb");
if(inFile == 0){
printf("Can't open input file");
return 0;
}
crFile = fopen("crput.txt","wb+");
if(crFile == 0){
printf("Can't open crypt file");
return 0;
}
outFile = fopen("output.txt","wb");
if(outFile == 0){
printf("Can't open output file");
return 0;
}
keyFile = fopen("key.txt","wb");
if(keyFile == 0){
printf("Can't open key file");
return 0;
}
/* заполняем ключ псевдослучайными числами */
for(i = 0; i < sizeof(key); i++)
FillRand(key, AES_KEY_SIZE);
/* выводим ключ шифрования в файл */
fwrite(&key, sizeof(unsigned char), AES_KEY_SIZE, keyFile);
/* шифрование */
while (!feof(inFile))
{
/* считываем исходный файл */
fread(&inbuff, sizeof(unsigned char), AES_BLOCK_SIZE, inFile);
/* инициализируем расширенный ключ */
aes_expand(&exp_key, key);
/* шифруем блок*/
aes_encrypt(&exp_key, inbuff, outbuff);
/* выводим зашифрованный блок в файл */
fwrite(&outbuff, sizeof(unsigned char), AES_BLOCK_SIZE, crFile);
}
/* добавляем в конец файла метку конца файла и переносим смещение файла в начало файла */
fputs(end, crFile);
fseek(crFile, 0, SEEK_SET);
while (!feof(crFile))
{
/* считываем шифротекст */
fread(&inbuff, sizeof(unsigned char), AES_BLOCK_SIZE, crFile);
/* инициализируем расширенный ключ */
aes_expand(&exp_key, key);
/* дешифруем блок */
aes_decrypt(&exp_key, inbuff, outbuff);
/* выводим расшифрованный блок в файл */
fwrite(&outbuff, sizeof(unsigned char), AES_BLOCK_SIZE, outFile);
}
/* завершаем работу */
fclose (inFile);
fclose (crFile);
fclose (keyFile);
fclose (outFile);
return 0;
}