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); } 
  • one
    If you had formatted both the code and the text, it is impossible to read. - Mirdin
  • @ sergey88, did you read about fopen (), fread () and fwrite ()? - avp
  • @ sergey88; To format a code, select it with the mouse and click on the {} button of the editor. - Nicolas Chabanovsky
  • one
    @ sergey88, but nothing happened to read about 3 functions? Either something concrete is not clear, or the program is compiled with errors (what?), Or does not produce the desired result? If the latter, what happens (what do you see on the screen)? - avp
  • one
    @ sergey88, so you read about fread? unsigned char ciphertext [16]; .... if (fread (ciphertext, sizeof (ciphertext), 1, input)! = 1) here in ciphertext will be your 16 bytes. I do not know exactly what your rijndaelDecrypt (rk, nrounds, ciphertext, plaintext) function does; but I suspect that it decrypts 16 bytes in the ciphertext buffer and places the decoded 16 bytes in plaintext. Apparently this is the transfer you expected to buffer2 . - Frankly, I would read like this int l = fread (ciphertext, 1, 16, input); and then checked l . If not EOF and not 16, then would swear. - avp

1 answer 1

rendered from comment.

"add zeros" should not up to 16000, but to the nearest number, more realreads and a multiple of 16.

Those. read, for example, 30 bytes, you need to add 2 zeros, get 32 ​​bytes, process this data and complete. Something will turn out like:

 if (!(realreads = fread(bufin,1,bufsize,input)) break; // конец данных int j, block_length = (realreads+15) & ~0xf; // cбросим 4 младшие бита, получим число, кратное 16 for (j = realreads; j < block_length; j++) bufin[j] = 0; realreads = block_length; j = 0; ... // Ваша обработка блока данных