cdebuggingencryptionprintf

Why do i get different results when I print out characters in the buffer when using a for loop and a printf function?


I have been practicing C and so I thought making a file encryption and decryption program would be good practice.However as I was working on the problem of displaying the encrypted form of the files content on the terminal, I came across a challenge with displaying the characters I stored in the buffer on the terminal, The values displayed when I use the printf function seem to be incomplete and when I use a for loop to iterate through and print each character in the buffer, some characters end up being ignored. I have tried debugging but there has been close to no progress. I would really appreciate it if someone could let me know what went wrong.


#include <stdio.h>
#include <stdbool.h>
void encrypt(char* path );
void decrypt(char* );

int main(void){
    char input;
    printf("Hello, please enter the path of the file you want to encrypt\n");
    char path[1024];
    scanf("%s",path);
    printf("File Path:%s\nFile encrypted content: ",path);


    FILE *file = fopen(path, "r+");

    if(file==NULL){
        printf("Cannot open file \n");
        
        system("pause");
        return 1;
    }
    int ch;
    int count = 0;
    char buffer[1024];
    
    while((ch=fgetc(file))!= EOF){
        int hc = (ch + (count*2))%128;
        printf("Original character: %c (ASCII value: %d) ", ch, ch);
        
        buffer[count] = hc;
        count++;
        printf("Encrypted character: %c (ASCII value: %d)\n ", hc, hc);
        if (count >= sizeof(buffer)-1){
            printf("Buffer size exceeded");
            break;
        }
        
        
    }
    
    buffer[count] = '\0';
    printf("This is the complete buffer\n\n");
    for(int i=0; i<= count; i++){
        
        printf("%c", buffer[i]);
    }
    printf("\nThis is the buffer printed as a string %s", buffer);
    fclose(file);
    
   system("pause");
   return 0;
}

I initially run the program with a simple text file of about three words as input but when the encrypted characters were printed out, they were fewer than expected. I tried to print each character with its encrypted form as the encryption loop runs and the results were okay. All values stored in the buffer were successfully encrypted. But when I tried to print our the values in buffer using printf the problem still persisted. I tried using a for loop and it prints our almost all characters and some are left out.


Solution

  • Some of the characters in the encrypted data are null characters (characters with value zero). When your for loop encounters a null character, it prints it and continues. When the printf encounters a null character, it stops.

    printf is designed this way because %s is for printing a C string, which is a string of characters whose end is marked by a null character. You cannot use printf with %s to print strings that contain internal null characters.

    Additionally, directly printing characters in encrypted data is problematic because some characters are “non-printable” characters with special meanings, and they may not be preserved through the process of handling standard output, particularly when it is directed to a terminal. They should be written to a file in binary mode or converted for display (e.g., display their values in decimal or hexadecimal instead of printing them directly).