This function has two buffers: bufin and bufout . The bufin buffer reads information from a file of 8 megabytes in size, then this information is divided into blocks of 160,000, then each is encrypted and written to the bufout buffer, but since the size of the last block is less than 160,000, an error occurs during the execution of the program.
How to add the last block with zeros so that its size is equal to 160,000?
#include "stdafx.h" #include <stdio.h> #include "rijndael.h" #include <iostream> #include <time.h> using namespace std; #define KEYBITS 256 #define bufsize 160000 unsigned char bufin[bufsize],bufout[bufsize]; int main(int argc, char ** argv) { unsigned long rk[RKLENGTH(KEYBITS)]; unsigned char key[KEYLENGTH(KEYBITS)]; int i; int nrounds,realreads; char password[80], sfname[80],dfname[80]; FILE *input,*output; unsigned char plaintext[16]; unsigned char ciphertext[16]; clock_t t1,t2; float tm; /* if (argc < 3) { fputs("Missing argument\n", stderr); return 1; }*/ printf("Enter key: "); gets(password); printf("Enter source file name: "); gets(sfname); printf("Enter destination file name: "); gets(dfname); t1=clock(); for (i = 0; i < sizeof(key); i++) key[i] = *password != 0 ? (*password)++ : 0; input=fopen(sfname,"rb"); output = fopen(dfname, "wb"); if (output == NULL) { fputs("File write error", stderr); return 1; } nrounds = rijndaelSetupEncrypt(rk, key, 256); //do { realreads=fread(bufin,1,bufsize,input); int j=0; while(j<=realreads){ for (int i = 0; i < 16; i++) { plaintext[i] = bufin[i+j]; } rijndaelEncrypt(rk, nrounds, plaintext, ciphertext); for (int i = 0; i < 16; i++) { bufout[i+j]=ciphertext[i]; } j+=16; } fwrite(bufout,1,realreads,output); }//while(realreads==bufsize); t2=clock(); tm=(float)(t2-t1)/CLOCKS_PER_SEC; printf("t=%f\n",tm); fclose(output); }
l
. If not EOF and not 16, then would swear. - avp