Why does my output contain extra characters? Why is only the first line of every file via notepad++ being encrypted and not the entire file? Happy coding! P.S I have the Second Edition of C programming language by Kernighan and Ritchie
EDIT: This code is my code after I fixed it, the question's has been answered. Thank you guys!
Here is my source NEW code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define getchar() getc(stdin)
#define putchar() putc((c),stdout)
#define XOR_BYTE 0x9E
char * xorBuffer(char *buffer, long bufferSize){
int i;
for(i = 0;i <= bufferSize;i++){
buffer[i] ^= XOR_BYTE;
}
return buffer;
}
int xorFile(char *fileIn, char * fileOut){
FILE *fpi, *fpo;
char *fileBuffer = NULL;
fpi = fopen(fileIn,"rb");
fpo = fopen(fileOut,"wb");
if(NULL == fpi){
printf("Error opening input file %s: %s\n", fileIn, strerror(errno));
return 1;
}
if(NULL == fpo){
printf("Error opening output file %s: %s\n", fileOut, strerror(errno));
return 2;
}
fseek(fpi,0L,SEEK_END);
long fileSize = ftell(fpi);
fileBuffer = malloc(sizeof(char)* (fileSize + 1));
fseek(fpi,0L,SEEK_SET);
size_t length = fread(fileBuffer, sizeof(char), fileSize,fpi);
fileBuffer[length];
fileBuffer = (char *)xorBuffer(fileBuffer,fileSize);
int c;
for(c = 0;c < fileSize;c++){
putc(((fileBuffer[c])),fpo);
}
fclose(fpi);
fclose(fpo);
free(fileBuffer);
return 0;
}
int main(int argc, char*argv[]){
if(argc == 3){
if(xorFile(argv[1],argv[2]) == 0)
printf("File encryption was successful.");
else
printf("An error occured.");
}else{
printf("usage --- xor [input file][output file]");
}
}
FILE_BUFFER[i] != '\0'test and why are you testing it?