Thread: Decrypting problems

  1. #1
    Registered User
    Join Date
    May 2018
    Posts
    12

    Decrypting problems

    Can someone help me with this issue. How can I decrypt the message, seems to me it's impossible to return the original message.

    Code:
    #include<stdio.h>
    #include<ctype.h>
    #include <stdio.h>
    #include <string.h>
    
    
    #define max_alfabeet 26
    const char * alfa = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    
    const char * rotor_sifrid [] = {
    "AJDKSIRUXBLHWTMCQGZNPYFVOE",
    "BDFHJLCPRTXVZNYEIWGAKMUSQO",
    "ESOVPZJAYQUIRHXLNFTGKDCMWB",
    "AKMFLGDQVZNTOWYHXUSPAIBRCJ",
    "FVPJIAOYEDRZXWGCTKUQSBNMHL"
    
    
    };
    
    
    const char * rotor_tyhim [] = {"S", "U", "W", "M", "T"};
    
    
    
    
    const char * reflector [] = {"VZBRGITYUPSDNHLXAWMJQOFECK",
    "YRUHQSLDPXNGOKMIEBFZCWVJAT"
    };
                          
                      
        struct rotor {
        int shift;
        int jan;
        const char * siffer;
        const char * void;
    };
    
    
    struct Enigma {
        int rotor_collection;
        const char * reflector;
        struct Rotor rotors [5];
    };
    
    
    // Initial setup
    struct Rotor New_Rotor (struct Enigma * machine, int rotor_number, int shift) {
        struct Rotor r;
        r.high = shift;
        r.and n = 0;
        r.siffer = rotor_products [rotor_number];
        r.tyhimik = rotor_tyhim [rotor_number];
      machine> rotor_count ++;
           
    return r;
       }
       
       // The function will show if and how many letters can be found in the list
       int str_ index (const char * str, int taht) {
        char * pos;
        int index;
        pos = strchr (str, taht);
        if (pos) index = (int) (pos - str);
        else index = -1;
        
    return index;
    }
    
    
       // Observes that the rotor is in a cavity and changes its location a moment later
       void rotor_ movement (struct rotor * rotor) {
        if (str_ index (rotor-> blank, (int) alpha [rotor-> shift])> = 0) {
            rotor-> = = 1;
        }
        rotor-> shift ++;
        rotor-> displacement = rotor-> displacement% max_alfabeet;
    }
     // Returns the location of the exit index.
    int rootor_tagasi (struct rotor * rotor, int index) {
        index = (index + rotor-> shift)% max_alfabeet;
        index = str_ index (rotor-> siffer, alpha [index]);
        index = (max_alfabeet + index - rotor-> shift)% max_alfabeet;
        
    return index;
    }
    
    
       // Generating machine + 5. Setting the rotor on the machine
       int main () {
       struct Enigma machine = {};
            int i, j, index, lim = 500;
            char box [lim];
        char alg [5];
        printf ("enter 5 digit sequence (bcdpl) n");
        gets (alg);
        for (i = 0; i <5; i ++) {
        machine.rootors [i] = new_rootor (& machine, i, str_ index (rotor_friders [i], (int) alg [i]));
        }
        
    FILE * f = fopen ("tere.txt", "r");
    if (f)
    {
      fgets (box, lim, f);
    }
    fclose (f);
       printf ("Original text from file:% s", box);
       printf ("Output Text:");
    for (j = 0; j <lim; j ++) {
    if (box [j] == '0') {
    break;
    
    
                                   }
    // Missing symbols that do not belong to the alphabet used
            if (! isalpha (box [j]) {
    memmove (& box [j], & box [j + 1], strlen (box) - j);
                j--;
    continue;
            }
        
              box [j] = toupper (box [j]);
            index = str_ index (alpha, box [j]);
            rotor motion (& machine rotors [0]);
        
            if (str_ index (machine rotors [1] .tyhimik, alpha [machine rotors [1] .direction)> = 0) {
                rotor movement (& machine rotors [1]);
            }
            // when the rotor has passed through the gap and noted the following, the next rotor will move forward
            for (i = 0; i <machine rotor_count - 1; i ++) {
                if (machine rotors [i] .jarg) {
                    machine rotors [i] .and n = 0;
                    rotor motion (& machine rotors [(i + 1)% 5]);
                }
             }
             
    // Moves to a keystroke
    for (i = 0; i <machine rotor_count; i ++) {
                index = rootor_tagasi (& machine.rootors [i], index);
            }
    box [j] = reflector [index];
            index = str_ index (alpha, box [j]);
            
    // similar to the above
            for (i = machine.rotor_country - 1; i> = 0; i--) {
                index = rootor_tagasi (& machine.rootors [i], index);
            }
    
    
            box [j] = alpha [index];
            printf ("% c", box [j]); // typing the answer alphabetically
        }
         f = fopen ("tere.txt", "w");
    if (f)
    {
      fputs (box, f);
    }
    fclose (f); // close the file
    
    
        printf ("n");
        system ("pause");
    }
    Last edited by asylumcyclop; 1 Week Ago at 07:57 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    36,838
    Perhaps you can post better code - you know, actually compiles.

    > machine> rotor_count ++;
    What?

    > int str_ index (const char * str, int taht) {
    What?

    > if (machine rotors [i] .jarg)
    What!?
    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 john.c's Avatar
    Join Date
    Dec 2017
    Posts
    493
    Quote Originally Posted by asylumcyclop View Post
    Can someone help me with this issue. How can I decrypt the message, seems to me it's impossible to return the original message.
    I always thought that to decrypt with the enigma you just feed in the cipher text with the same initial settings used to create it.
    Let him who is not come to logic be plagued with continuous and everlasting filth.
    - John of Salisbury, 1160

  4. #4
    Registered User
    Join Date
    May 2018
    Posts
    12
    True, that's how it works!

    taht is letter, jarg is next. Still nothing, any ideas?
    Last edited by asylumcyclop; 1 Week Ago at 03:09 PM.

  5. #5
    Registered User
    Join Date
    May 2018
    Posts
    12
    Here is an example how this program should work.

    https://snag.gy/1Fd02A.jpg

    https://snag.gy/QHyaXI.jpg

    The final result sould look like this. The cipher must return the initial message into the folder.
    Last edited by asylumcyclop; 1 Week Ago at 04:08 PM.

  6. #6
    Registered User john.c's Avatar
    Join Date
    Dec 2017
    Posts
    493
    Is this a joke?
    The program you've posted is senseless.
    It has spaces where they are not allowed.
    It's missing punctuation characters in some places.
    It refers to struct members that do not exist.
    So it will not compile and is impossible to make sense of.

    You need to post a real program.
    Let him who is not come to logic be plagued with continuous and everlasting filth.
    - John of Salisbury, 1160

  7. #7
    Registered User
    Join Date
    May 2018
    Posts
    12
    Not sure what happened in line 137th translated into english.

    So far, I have managed to open the textfile, read it line by line, and display it on the console:

    I'm working on Enigma, and my code is almost complete but I am having problems with reading sentences from a textfile (txt), storing it in an array, decoding it, and storing it again in a textfile. Storing it back in a txt file gets me nowhere.



    Code:
    #include<ctype.h>
    #include <stdio.h>
    #include <string.h>
    
    #define max_alfabet 26
    
    const char *alfa = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    
    const char *rotor_ciphers[] = {
        "AJDKSIRUXBLHWTMCQGZNPYFVOE",
        "BDFHJLCPRTXVZNYEIWGAKMUSQO",
        "ESOVPZJAYQUIRHXLNFTGKDCMWB",
        "AKMFLGDQVZNTOWYHXUSPAIBRCJ",
        "FVPJIAOYEDRZXWGCTKUQSBNMHL"
        
    };
    
    const char *rotor_notches[] = {"S","U", "W","M","T"};
    
    
    const char *reflector[] = {"VZBRGITYUPSDNHLXAWMJQOFECK",
                             "YRUHQSLDPXNGOKMIEBFZCWVJAT"
    };
                          
                      
        struct rotor {
        int offset;
        int next;
        const char *cipher;
        const char * notch;                   
    };
    
    struct Enigma {
        int             rotors_max;
        const char      *reflector;
        struct rotor rotors[5];
    };
    
    //initial setup
    struct rotor new_rotor(struct Enigma *machine, int rotor_number, int offset) {
        struct rotor r;
        r.offset = offset;  
        r.next = 0;
        r.cipher = rotor_ciphers[rotor_number];
        r.notch = rotor_notches[rotor_number];
        machine-> rotors_max++; 
           return r;
       }
       
       //The function will show if and how many letters can be found in the list
       int str_index(const char *str, int letter) {
        char *pos;
        int index;
        pos = strchr(str, letter);
        if (pos) index = (int) (pos - str);
        else index = -1;
        
        return index;
    }
    
     //Observes that the rotor is in a cavity and changes its location a moment later
       void rotor_rotate(struct rotor *rotor) {
        if(str_index(rotor->notch, (int)alfa[rotor->offset]) >= 0) {
            rotor->next = 1;
        }
        rotor->offset++;
        rotor->offset = rotor->offset % max_alfabet;
    }
    // Returns the location of the exit index.
    int rotor_turnover(struct rotor *rotor, int index) {
        index = (index + rotor->offset) % max_alfabet;
        index = str_index(rotor->cipher, alfa[index]);
        index = (max_alfabet + index - rotor->offset) % max_alfabet;
        
          return index;
    }
    
     // Generating machine + set up the 5th rotor on the machine
       int main() {
           struct Enigma machine = {}; 
            int i, j, index, limit=500;
            char box[limit];
        char alg[5];
        printf("insert 5 sequence combination (bcdpl)\n");
        gets(alg);
        for (i=0;i<5;i++){
            machine.rotors[i] = new_rotor(&machine,i, str_index(rotor_ciphers[i],(int) alg[i]));
        }
        
        FILE * f = fopen ("tere.txt", "r");
        if (f)
        {
              fgets(box, limit, f);
        }
        fclose (f);
           printf("input:%s\n", box);
           printf("output:");
        for (j = 0; j < limit; j++){
            if (box[j]=='\0'){
                break;
    
                                   }
            //    Missed symbols that do not belong to the alphabet                     
            if (!isalpha(box[j])) {  
                     memmove(&box[j], &box[j + 1], strlen(box) - j);
                j--;
                continue;                
            }
        
              box[j] = toupper(box[j]);
            index = str_index(alfa, box[j]);
            rotor_rotate(&machine.rotors[0]);
            
            if(str_index(machine.rotors[1].notch, alfa[machine.rotors[1].offset]) >= 0 ) {
                rotor_rotate(&machine.rotors[1]); 
            }
          
    // when the rotor has passed through the gap and noted the following, the next rotor will move forward
            for(i=0; i < machine.rotors_max - 1; i++) {
                if(machine.rotors[i].next) {
                    machine.rotors[i].next = 0;
                    rotor_(&machine.rotors[(i+1)%5]); 
                }
             
         }
         //Moves to a keystroke
            for(i=0; i < machine.rotors_max; i++) {
                index = rotor_turnover(& machine.rotors[i], index); 
            }
            
            box[j] = reflector[index];
            index = str_index(alfa, box[j]);
            
        // similar
            for(i = machine.rotors_max - 1; i >= 0; i--) {
                index = rotor_turnover(&machine.rotors[i], index); 
            }
    
            box[j] = alfa[index];
            printf("%c", box[j]); //typing the answer 
        }
         f = fopen ("tere.txt", "w");
        if (f)
        {
              fputs(box, f);
        }
        fclose (f); //close the file
        printf("\n\n");
        system("pause"); }
    Last edited by Salem; 2 Days Ago at 08:59 AM. Reason: Removed crayola tags from the source code

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    36,838
    Your indentation could be better, and it still does NOT compile!
    Code:
    #include<ctype.h>
    #include <stdio.h>
    #include <string.h>
    
    #define max_alfabet 26
    
    const char *alfa = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    
    const char *rotor_ciphers[] = {
      "AJDKSIRUXBLHWTMCQGZNPYFVOE",
      "BDFHJLCPRTXVZNYEIWGAKMUSQO",
      "ESOVPZJAYQUIRHXLNFTGKDCMWB",
      "AKMFLGDQVZNTOWYHXUSPAIBRCJ",
      "FVPJIAOYEDRZXWGCTKUQSBNMHL"
    };
    
    const char *rotor_notches[] = { "S", "U", "W", "M", "T" };
    
    const char *reflector[] = {
      "VZBRGITYUPSDNHLXAWMJQOFECK",
      "YRUHQSLDPXNGOKMIEBFZCWVJAT"
    };
    
    struct rotor {
      int offset;
      int next;
      const char *cipher;
      const char *notch;
    };
    
    struct Enigma {
      int rotors_max;
      const char *reflector;
      struct rotor rotors[5];
    };
    
    //initial setup
    struct rotor new_rotor(struct Enigma *machine, int rotor_number, int offset)
    {
      struct rotor r;
      r.offset = offset;
      r.next = 0;
      r.cipher = rotor_ciphers[rotor_number];
      r.notch = rotor_notches[rotor_number];
      machine->rotors_max++;
      return r;
    }
    
    //The function will show if and how many letters can be found in the list
    int str_index(const char *str, int letter)
    {
      char *pos;
      int index;
    
      pos = strchr(str, letter);
      if (pos)
        index = (int) (pos - str);
      else
        index = -1;
    
      return index;
    }
    
    //Observes that the rotor is in a cavity and changes its location a moment later
    void rotor_rotate(struct rotor *rotor)
    {
      if (str_index(rotor->notch, (int) alfa[rotor->offset]) >= 0) {
        rotor->next = 1;
      }
      rotor->offset++;
      rotor->offset = rotor->offset % max_alfabet;
    }
    
    // Returns the location of the exit index.
    int rotor_turnover(struct rotor *rotor, int index)
    {
      index = (index + rotor->offset) % max_alfabet;
      index = str_index(rotor->cipher, alfa[index]);
      index = (max_alfabet + index - rotor->offset) % max_alfabet;
      return index;
    }
    
     // Generating machine + set up the 5th rotor on the machine
    int main()
    {
      struct Enigma machine = { };
      int i, j, index, limit = 500;
      char box[limit];
    
      char alg[5];
      printf("insert 5 sequence combination (bcdpl)\n");
      gets(alg);
    
      for (i = 0; i < 5; i++) {
        machine.rotors[i] =
            new_rotor(&machine, i, str_index(rotor_ciphers[i], (int) alg[i]));
      }
    
      FILE *f = fopen("tere.txt", "r");
      if (f) {
        fgets(box, limit, f);
      }
      fclose(f);
    
      printf("input:%s\n", box);
      printf("output:");
    
      for (j = 0; j < limit; j++) {
        if (box[j] == '\0') {
          break;
        }
    
        // Missed symbols that do not belong to the alphabet
        if (!isalpha(box[j])) {
          memmove(&box[j], &box[j + 1], strlen(box) - j);
          j--;
          continue;
        }
    
        box[j] = toupper(box[j]);
        index = str_index(alfa, box[j]);
        rotor_rotate(&machine.rotors[0]);
    
        if (str_index(machine.rotors[1].notch, alfa[machine.rotors[1].offset]) >= 0) {
          rotor_rotate(&machine.rotors[1]);
        }
    
        // when the rotor has passed through the gap and noted the following, the next rotor will move forward
        for (i = 0; i < machine.rotors_max - 1; i++) {
          if (machine.rotors[i].next) {
            machine.rotors[i].next = 0;
            rotor_(&machine.rotors[(i + 1) % 5]);
          }
        }
    
        //Moves to a keystroke
        for (i = 0; i < machine.rotors_max; i++) {
          index = rotor_turnover(&machine.rotors[i], index);
        }
    
        box[j] = reflector[index];
        index = str_index(alfa, box[j]);
    
        // similar
        for (i = machine.rotors_max - 1; i >= 0; i--) {
          index = rotor_turnover(&machine.rotors[i], index);
        }
    
        box[j] = alfa[index];
        printf("%c", box[j]);       //typing the answer
      }
    
      f = fopen("tere.txt", "w");
      if (f) {
        fputs(box, f);
      }
      fclose(f);                    //close the file
      printf("\n\n");
      system("pause");
    }
    
    
    $ gcc -Wall -Wextra -g main.c
    main.c: In function ‘main’:
    main.c:92:3: warning: implicit declaration of function ‘gets’ [-Wimplicit-function-declaration]
       gets(alg);
       ^
    main.c:132:9: warning: implicit declaration of function ‘rotor_’ [-Wimplicit-function-declaration]
             rotor_(&machine.rotors[(i + 1) % 5]);
             ^
    main.c:142:12: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
         box[j] = reflector[index];
                ^
    main.c:160:3: warning: implicit declaration of function ‘system’ [-Wimplicit-function-declaration]
       system("pause");
       ^
    /tmp/ccH7ZPyc.o: In function `main':
    /home/sc/Documents/main.c:92: warning: the `gets' function is dangerous and should not be used.
    /home/sc/Documents/main.c:132: undefined reference to `rotor_'
    collect2: error: ld returned 1 exit status
    And yes, your use of gets() is an ACTUAL buffer overflow if the user types in the expected 5 characters.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. encrypting and decrypting assignment
    By shayd2110 in forum C Programming
    Replies: 4
    Last Post: 05-01-2017, 12:35 AM
  2. Decrypting Ciphertext
    By ubmattpangolin in forum General Discussions
    Replies: 11
    Last Post: 09-02-2016, 06:21 PM
  3. Require help decrypting XOR.
    By mkthnx001 in forum C++ Programming
    Replies: 15
    Last Post: 05-17-2009, 07:15 PM
  4. Decrypting OTP ciphertext
    By slippy in forum C++ Programming
    Replies: 11
    Last Post: 12-25-2007, 06:59 AM
  5. decrypting
    By cyberCLoWn in forum C++ Programming
    Replies: 11
    Last Post: 12-30-2003, 04:20 PM

Tags for this Thread