Im trying to write my custom file encrypter, but the outfile fp2 is empty so I'm doing something wrong here:
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#defineSIZE(a) sizeof(a) / sizeof(a[0])
void encrypt(const char *fileName, const char *newFileName, const char *KWORD,
int K)
{
FILE *fp, *fp2;
fp = fopen(fileName, "rb");
fp2 = fopen(newFileName, "a+");
if (!fp) {
printf("\nError: Could not open the file %s\n", fileName);
} else {
printf("\nFile has been opened successfully\n");
int block[8192], xorblock[8192];
char banner[128];
size_t allBytes, byteCount =
fread(banner, sizeof(banner), sizeof(banner), fp);
printf("BASE FILE BANNER: %s\n", banner);
fseek(fp, 0, SEEK_SET);
while (byteCount) {
byteCount = fread(block, sizeof(block), sizeof(block), fp);
allBytes += byteCount;
printf("\rByte has been read: %zu\r", byteCount);
for (size_t i = 0; i < SIZE(block); i++) {
K = K % strlen(KWORD);
volatile char kwordPos = KWORD[K];
xorblock = block ^ (int) kwordPos;
K++;
}
fwrite(xorblock, sizeof(xorblock), sizeof(xorblock), fp2);
}
printf("\nAll bytes read:%zu\n", allBytes);
}
fclose(fp);
fclose(fp2);
FILE *fp2read;
fp2read = fopen(newFileName, "rb");
char newBanner[128];
fread(newBanner, sizeof(newBanner), sizeof(newBanner), fp2read);
printf("\nNEW FILE BANNER: %s\n", newBanner);
}
int main()
{
const char *myFileName = "Example.txt";
char newFileName[17] = "_NEW_";
const char *keyWord = "Keyword";
int Key = 'K';
strcat(newFileName, myFileName);
encrypt(myFileName, newFileName, keyWord, Key);
printf("%s%zu\n", newFileName, strlen(newFileName));
printf("\nAll done.Quitting now.\n");
return 0;
}
fsanitizer is showing that something is crashing pretty badly:
File has been opened successfullyBASE FILE BANNER: C plus plus
All bytes read:0
AddressSanitizer:DEADLYSIGNAL
================================================== ===============
==571==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x7f2229726005 bp 0x7ffe1b18abe0 sp 0x7ffe1b18a320 T0)
==571==The signal is caused by a READ memory access.
==571==Hint: address points to the zero page.
AddressSanitizer:DEADLYSIGNAL
AddressSanitizer: nested bug in the same thread, aborting.