Thread: Xor Algorith (need help !)

  1. #1
    Registered User
    Join Date
    Jan 2021
    Posts
    3

    Xor Algorith (need help !)

    Hello, it's my first time posting on this forum, i need help w/ my xor algorithm :
    So basicly my whole program works, if this one condition is valid (and i don't understand why)
    If my message (taken from a file) is in caps lock, my key (inputed "gets")should not be in caps lock and vice versa.

    Here is my code, and any idea is welcomed !

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #defineMAX_BUFFER_SIZE10000
    typedef unsigned char byte;
    
    //strncat(key, key, strlen(phrase) - strlen(key));
    
    void repeat_key(byte * phrase, byte * key)
    {
      byte new[MAX_BUFFER_SIZE];
      int i;
      int cmpt = 0;
    
      for (i = 0; i < strlen(phrase); i++) {
        if (cmpt == strlen(key))
          cmpt = 0;
        new = key[cmpt];
        cmpt++;
      }
      new = '\0';
      strcpy(key, new);
    }
    
    void cut_key(byte * phrase, byte * key)
    {
      byte new[MAX_BUFFER_SIZE];
      int i;
      for (i = 0; i < strlen(phrase); i++) {
        new = key;
      }
      new = '\0';
      strcpy(key, new);
    }
    
    void xorCipher(byte * key, FILE * file_src_P, FILE * file_dest_P)
    {
      byte msg[MAX_BUFFER_SIZE];
      char output[MAX_BUFFER_SIZE];
      int i;
    
      fgets(msg, MAX_BUFFER_SIZE, file_src_P);
    
      if (strlen(key) > strlen(msg)) {
        cut_key(msg, key);
        for (i = 0; i < strlen(msg); i++) {
          output = msg ^ key;
        }
        output = '\0';
        if (fprintf(file_dest_P, "%s", output) < 0) {
          printf("Erreur fprintf !\n");
          exit(1);
        };
      } else if (strlen(key) < strlen(msg)) {
        repeat_key(msg, key);
        for (i = 0; i < strlen(msg); i++) {
          output = msg ^ key;
        }
        output = '\0';
        if (fprintf(file_dest_P, "%s", output) < 0) {
          printf("Erreur fprintf !\n");
          exit(1);
        };
      } else if (strlen(key) == strlen(msg)) {
        for (i = 0; i < strlen(msg); i++) {
          output = msg ^ key;
        }
        output = '\0';
        if (fprintf(file_dest_P, "%s", output) < 0) {
          printf("Erreur fprintf !\n");
          exit(1);
        };
      }
    }
    
    int main()
    {
      byte key[MAX_BUFFER_SIZE];
      FILE *f_s_p = fopen("source.txt", "r");
      FILE *f_d_p = fopen("destination.txt", "w");
      //FILE * f_s_p = fopen("destination.txt","r");
      //FILE * f_d_p = fopen("source.txt","w");
    
      printf("Entrez clef : ");
      gets(key);
    
      if (strlen(key) == 0) {
        printf("Entrez une clef valide !\n");
        exit(1);
      }
    
      if (f_s_p == NULL || f_d_p == NULL) {
        printf("Erreur fopen !\n");
        exit(1);
      }
    
      xorCipher(key, f_s_p, f_d_p);
    
      fclose(f_s_p);
      fclose(f_d_p);
    
      return 0;
    
    }
    Last edited by Salem; 01-04-2021 at 11:23 AM. Reason: removed crayola

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,713
    > So basicly my whole program works, if this one condition is valid (and i don't understand why)
    Surely not.....

    Code:
    foo.c:18:9: error: assignment to expression with array type
         new = key[cmpt];
             ^
    foo.c:21:7: error: assignment to expression with array type
       new = '\0';
           ^
    And a whole raft of warnings.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2021
    Posts
    3
    Could you explain please ?
    And quick update, my code is way too over complicated. Usin fgetc will help me i think. I just found out about it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Queue class algorith/code
    By verbity in forum C++ Programming
    Replies: 4
    Last Post: 02-03-2009, 10:13 PM
  2. Encode and Decode algorith for 2 to 7 bits
    By kingofpop in forum C Programming
    Replies: 2
    Last Post: 11-15-2008, 07:08 AM

Tags for this Thread