The function basically takes a char array (string) and read a character from the input file. Then, it adds the key's ascii code (at the proper position of the key) to the ascii code of the readed character, and puts it to the output file. The below code:
For encryption:
- Code: Select all
int ioEncryptFile(const char * source, const char * dest, const char * mode, char key[]) {
FILE *finput = fopen(source, "r"); // Open the source file in read mode
if(finput == NULL) // error at opening input descriptor
return false;
FILE *foutput;
if(strcmp(mode, OVW) == 0) {
foutput = fopen(dest, "w"); // Open the output file in write/binary mode
} else if(strcmp(mode, APP) == 0) {
foutput = fopen(dest, "a"); // Open the output file in append/binary mode
} else if((strcmp(mode, SAFETY) == 0) && (ioFileExists(dest) == true)) {
return false;
} else {
fclose(finput);
return NULL;
}
if(foutput == NULL) { // error at opening output descriptor
fclose(finput);
return false;
}
char c; /* To put the character readed */
char u; /* The character to write, after applied the key */
int keylen = strlen(key); /* array index */
int Y = 0; // index for the *loop*
while((feof(finput) == 0) && (ferror(finput) == 0) && (ferror(foutput) == 0)) {
/* if the array index has reached the lenght of the key */
if(Y == keylen) {
Y = 0; /* reinitialize the index or counter */
}
/* read a character from the input */
c = fgetc(finput);
/* control expression */
if(c != '\0') {
u = c + key[Y];
} else if (c == '\0') {
u = '\0';
}
/* put out the resulted encrypted character */
fputc(u, foutput);
/* advance the index or counter */
Y++;
}
/* Close file descriptors */
fclose(finput);
fclose(foutput);
return true;
}
For decryption:
- Code: Select all
int ioDecryptFile(const char * source, const char * dest, const char * mode, char key[]) {
FILE *finput = fopen(source, "r"); // Open the source file in read mode
if(finput == NULL) // error at opening input descriptor
return false;
FILE *foutput;
if(strcmp(mode, OVW) == 0) {
foutput = fopen(dest, "w"); // Open the output file in write/binary mode
} else if(strcmp(mode, APP) == 0) {
foutput = fopen(dest, "a"); // Open the output file in append/binary mode
} else if((strcmp(mode, SAFETY) == 0) && (ioFileExists(dest) == true)) {
return false;
} else {
fclose(finput);
return NULL;
}
if(foutput == NULL) { // error at opening output descriptor
fclose(finput);
return false;
}
char c; // the character readed
char u; // the character to write, result of '(char) cc + key'
int keylen = strlen(key);
int Y = 0; // index for the *loop*
while((feof(finput) == 0) && (ferror(finput) == 0) && (ferror(foutput) == 0)) {
/* if the array index has reached the lenght of the key */
if(Y == keylen) {
Y = 0; /* reinitialize the index or counter */
}
/* read a character from the input */
c = fgetc(finput);
/* control expression */
if(c != '\0') {
u = c - key[Y];
} else {
u = '\0';
}
/* put out the resulted encrypted character */
fputc(u, foutput);
/* advance the index or counter */
Y++;
}
/* Close file descriptors */
fclose(finput);
fclose(foutput);
return true;
}
The minimal macros like APP (append), OVW (overwrite) and SAFETY are defined in the same file as this functions (I'm working in my own library
The problem is that the function keeps a non-expected-characters at the end of the output file. I think the problem is both of the encrypt/decrypt functions, because the encrypted file has 1 byte more than the orig file, and the decrypted version of that, has 1 byte more.
Original -> X bytes
Encrypted version -> X + 1 bytes
Decrypted version -> X + 2 bytes
I think at the '\0' (NULL character) but,... nah, I put a conditional instruction in the encryption engine and does nothing.
Can anyone help me?
Thanks for all,
best regards!

