I am writing the implementation of the TEA block encryption algorithm for files. It is necessary to split the file into blocks of 64 bits, with the last block, if necessary, finish to a 64-bit multiplicity by setting the missing bytes to 0x01. Question: how to check the block for multiplicity and finish these missing bytes?

FILE *openFile, *resultFile; char buffer[8], key[8] = "01234567"; if ((openFile = fopen("c:/dev/encoding/test.xlsx", "rb")) == NULL) { printf("Cannot open file.\n"); exit(1); } if ((resultFile = fopen("c:/dev/encoding/test.enc", "wb")) == NULL) { printf("Cannot open file.\n"); exit(1); } fread(buffer, sizeof(char), 8, openFile); do { encrypt(buffer, key); if ((fwrite(buffer, sizeof(char), 8, resultFile)) != 8) { printf("Fatal Error writing output file!!!\n"); exit(-1); } fread(buffer, sizeof(char), 8, openFile); } while (!feof(openFile)); fclose(openFile); fclose(resultFile); 
  • The essence of the question is completely unclear. Who bothers you to read files and write in blocks of 64 bits? - Vlad from Moscow
  • fread returns the number of bytes actually read and if it is suddenly less than 8, then add - Mike
  • @Mike, and how to check? I'm sorry I'm new to C - svnvav
  • if ((fread (buffer, sizeof (char), 8, openFile))! = 8) will this check be correct? - svnvav
  • Well, I would still save the variable in advance, you still have to supplement. those. add 8-length bytes - Mike

2 answers 2

Why check? just before reading into the buffer, initialize it with the specified value. Those bytes of the buffer that are not enough bytes from the file will remain with this value ...

  • Unfortunately, I did not understand what you mean. The fact is that the program works, only extra characters appear at the end of the decrypted file. - svnvav
  • I mean that you should put 8 bytes 0x01 before fread in buffer. If 8 bytes are read, they will all be overwritten, if less, the rest will remain 0x01. - Akina
  • Well thank you. Corrected post. But the code still does not work correctly. How to get rid of these extra bytes when decrypting? - svnvav
  • Just keeping somewhere the original size of the data (or other information about the size of the data). Otherwise, there is no possibility, seeing the end bytes 0x01, to determine whether they are meaningful or supplemented. - Akina
  • How then to encrypt / decrypt? On assignment, I have only a file and a key; I did not find any clear examples of how TEA works with C files on the Internet. - svnvav

In general, that's what happened. Now, however, does not match the title of the topic. I throw the code of the entire file, suddenly someone will come in handy, especially not on the Internet. If there are more ways, please write.

 #include "tea.h" void main(int argc, char **argv){ FILE *srcFile, *resultFile; if ((srcFile = fopen("c:/dev/encoding/test.xlsx", "rb")) == NULL) { printf("Cannot open file.\n"); exit(1); } if ((resultFile = fopen("c:/dev/encoding/test.enc", "wb")) == NULL) { printf("Cannot open file.\n"); exit(1); } encodeFile(srcFile, resultFile, "01234567"); fclose(srcFile); fclose(resultFile); if ((srcFile = fopen("c:/dev/encoding/test.enc", "rb")) == NULL) { printf("Cannot open file.\n"); exit(1); } if ((resultFile = fopen("c:/dev/encoding/res.xlsx", "wb")) == NULL) { printf("Cannot open file.\n"); exit(1); } decodeFile(srcFile, resultFile, "01234567"); fclose(srcFile); fclose(resultFile); return 0; } void encodeFile(FILE* srcFile, FILE* resultFile, uint32_t* key){ printf("Start encoding\n"); fseek(srcFile, 0L, SEEK_END); long size = ftell(srcFile); fseek(srcFile, 0L, SEEK_SET); if ((fwrite(&size, sizeof(long), 1, resultFile)) != 1) { printf("Fatal Error writing output file!!!\n"); exit(-1); } char buffer[8]; while (!feof(srcFile)) { fread(buffer, sizeof(char), 8, srcFile); encrypt(buffer, key); if ((fwrite(buffer, sizeof(char), 8, resultFile)) != 8) { printf("Fatal Error writing output file!!!\n"); exit(-1); } } printf("Encoding complete\n"); } void decodeFile(FILE* srcFile, FILE* resultFile, uint32_t* key){ printf("Start decoding\n"); char buffer[8]; long size; fread(&size, sizeof(long), 1, srcFile); printf("%i\n", size); for (size; size > 8; size -= 8){ fread(buffer, sizeof(char), 8, srcFile); decrypt(buffer, key); if ((fwrite(buffer, sizeof(char), 8, resultFile)) != 8) { printf("Fatal Error writing output file!!!\n"); exit(-1); } } fread(buffer, sizeof(char), 8, srcFile); decrypt(buffer, key); if ((fwrite(buffer, sizeof(char), size, resultFile)) != size) { printf("Fatal Error writing output file!!!\n"); exit(-1); } printf("Decoding complete\n"); } void encrypt(uint32_t* v, uint32_t* k){ /* set up */ uint32_t v0 = v[0]; uint32_t v1 = v[1]; uint32_t sum = 0; uint32_t i; /* a key schedule constant */ uint32_t delta = 0x9e3779b9; /* cache key */ uint32_t k0 = k[0]; uint32_t k1 = k[1]; uint32_t k2 = k[2]; uint32_t k3 = k[3]; /* basic cycle start */ for (i = 0; i < 32; i++) { sum += delta; v0 += ((v1 << 4) + k0) ^ (v1 + sum) ^ ((v1 >> 5) + k1); v1 += ((v0 << 4) + k2) ^ (v0 + sum) ^ ((v0 >> 5) + k3); } /* end cycle */ v[0] = v0; v[1] = v1; } void decrypt(uint32_t* v, uint32_t* k){ /* set up */ uint32_t v0 = v[0]; uint32_t v1 = v[1]; uint32_t sum = 0xC6EF3720; uint32_t i; /* a key schedule constant */ uint32_t delta = 0x9e3779b9; /* cache key */ uint32_t k0 = k[0]; uint32_t k1 = k[1]; uint32_t k2 = k[2]; uint32_t k3 = k[3]; /* basic cycle start */ for (i = 0; i < 32; i++) { v1 -= ((v0 << 4) + k2) ^ (v0 + sum) ^ ((v0 >> 5) + k3); v0 -= ((v1 << 4) + k0) ^ (v1 + sum) ^ ((v1 >> 5) + k1); sum -= delta; } /* end cycle */ v[0] = v0; v[1] = v1; }