0

I'm writing a program that encodes a file using xor and print the encrypted text into another file. It technically works, however the output contains several symbols rather than only lowercase characters. How would I tell the program to only print lowercase letters, and be able to decode it back?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>




int main(int argc, char *args[]){
  FILE *inFile, *outFile, *keyFile;
  int key_count = 0;
  int encrypt_byte;
  char key[1000];

  inFile = fopen("input.txt", "r"); 
  outFile = fopen("output.txt", "w"); 
  keyFile = fopen("key.txt", "r");




  while((encrypt_byte = fgetc(inFile)) !=EOF)
    {
      fputc(encrypt_byte ^ key[key_count], outFile); //XORs
      key_count++;
      if(key_count == strlen(key)) //Reset the counter
     key_count = 0;
    }
        printf("Complete!");

    fclose(inFile);
    fclose(outFile);
    fclose(keyFile);
    return(0);  
}

Here is the output I get:

ÕââÐåæœ¶è”ó

I just want it to only use lowercase letters

Jacklyn
  • 19
  • 6

2 Answers2

1

You can't. You either XOR all data of the file, or you don't. XOR-ing will result in non-printable characters.

What you can do though, is first XOR it and then encode it base64.

To get your original text/data back, do the reverse.

See also How do I base64 encode (decode) in C?

Community
  • 1
  • 1
Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
-3

use the function tolower() here there is an example:

#include<stdio.h>
#include<ctype.h>

int main()
{
    int counter=0;
    char mychar;
    char str[]="TeSt THis seNTeNce.\n";

    while (str[counter])
    {
        mychar=str[counter];
        putchar (tolower(mychar));
        counter++;
    }
    return 0;
}