Thread: Help Me with an autocipher key in C!!!

  1. #1
    Registered User
    Join Date
    Jun 2020
    Posts
    9

    Help Me with an autocipher key in C!!!

    Hello,
    Can somebody please help me with this code. I've gotten somewhat an idea of what it can be but it is still giving me errors. I also don't understand the algorithm of an autokey cipher so could someone please explain in the program.THANKS!!!
    Code:
    #include <stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<ctype.h>
    #include<math.h>
    
    
    char key[];
    
    unsigned int easyendecrypt(char c);
    
    int main(void) {
        int c;
        while ((c = getc(stdin)) !=EOF) {
            putc(easyendecrypt(c), stdout);
        }
        printf("\n");
        return 0;
    }
    
    unsigned int easyendecrypt(char c) {
        char k;
    
    #ifdef DECRYPT
        fprintf(stderr, "decrypting with %s\n",key);
        k = 'A' + (c - key);
        key = k;
        return k;
    #else
        fprintf(stderr,"encrypting with %s\n",key);
        k = key + (c + key - 2 * 'A');
        key=c;
        return k;
    #endif
    }
    Attached Files Attached Files
    • File Type: c main.c (581 Bytes, 198 views)
    Last edited by Salem; 06-18-2020 at 12:17 AM. Reason: Reposted code without the font train wreck

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Well first, decide whether your global variable 'key' is a single char, or an array of chars.
    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
    Jun 2020
    Posts
    9
    It is an array so I’m trying to input that all though out my code

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Right, so you need several things.

    1. Your key needs a size.
    char key[100];

    2. Whenever you write a single char to the array, you need a subscript.
    key[some_integer_counter] = k;

    3. If you want to print the key using %s, you need to make sure there is a '\0' character in the right place.
    key[some_integer_counter] = '\0';
    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.

  5. #5
    Registered User
    Join Date
    Jun 2020
    Posts
    9
    ok, what about my logic of how autokeycipher works. Is this how it would happen to encrypt and decrypt?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > Is this how it would happen to encrypt and decrypt?
    Have you tested it?

    At some point, you'll have been given some example inputs and expected outputs.
    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.

  7. #7
    Registered User
    Join Date
    Jun 2020
    Posts
    9













    If (keyIsNotExhaust) Encryption = currChar + key; else Encryption = currChar + prevChar;

    If (keyIsNotExhaust) decryption = currChar - key; else decryption = currChar - decryption;

    Return encryption in the encryption part or decryption in decryption part

    store the encryption/decryption results in an array

    This is the logic I'm trying to go by but I'm having trouble putting that into my code above.

  8. #8
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338
    algorithm of an autokey cipher
    Autokey cipher - Wikipedia

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    
    #define maxLength 256
    
    #define key "KEYCODE"
    #define message "autokey encryption"
    
    #define encrypt(a,b) (((((a-64)+(b-64))%26)+65))
    
    int main(int args, char *argv[]) {
        
        int count, start = 0;
        char toEncode[maxLength], noSpaces[maxLength];
    
        count = 0;
        for (int i = 0; i < (strlen(message)); i++ ) {
            if (message[i] != ' ') {
                noSpaces[count++] = toupper(message[i]); 
            }
        } noSpaces[count] = '\0';
    
        int textLength = count, keyLength = strlen(key);
    
        count = 0;
        for (int i = 0; i < (textLength); i++ ) {
            if (i < keyLength) {
                toEncode[count++] = toupper(key[i]);
            } else {
                if (start == 0) start = i;
                toEncode[count++] = noSpaces[i-start];
            }
        } toEncode[count] = '\0'; 
    
        for (int i=0; i<(textLength); i++ ) {
            printf("%c", encrypt( toEncode[i], noSpaces[i]) );
        }
    
        return 0;
    }
    Code 4 Life: Autokey Cipher
    Practical Cryptography
    Last edited by Structure; 06-23-2020 at 09:01 PM.
    "without goto we would be wtf'd"

  9. #9
    Registered User
    Join Date
    Jun 2020
    Posts
    2
    The guys have given you good advice, but we already have this software, so I post it directly. This is called encryption, please do NOT change the terms! It becomes very hard to figure out what someone is talking about. Especially Americans have this tendency. For example what is [MF, its main function, but written like this, who can guess....We are considering releasing a GUI version of this software with which you can encrypt everything, even .exe files. Test it for yourself to be sure. I don't know if advertisement is allowed on this site, so we can advertise our online shop.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
                                                        /* Function declaration. */
    void Encrypt(char * FILENAME, char * NEW_FILENAME);
    
    int main ()
    {
        char f1[100];   // array big enough to hold expected input
        char f2[100];   // array big enough to hold expected input
    
        printf ("Please enter file for processing:\n");
        scanf  ("%99s", f1);    // set max characters to read to prevent buffer overflow
        printf ("Please enter the name of the file after processing:\n");
        scanf  ("%99s", f2);
    
        Encrypt(f1, f2);
        return 0;
    }
    
    void Encrypt(char * FILENAME, char * NEW_FILENAME)
    {
             printf("Processing started.\n");
             FILE *inFile;
             FILE *outFile;
             char key[50];         // array big enough to hold expected input
             char process[50];     // array big enough to hold expected input
             int keylen, keyidx;
    
             int Byte;       // fgetc returns int, so use an int
             int newByte;
    
             printf ("Please enter 'encryption or decryption'\n");
             scanf  ("%49s", process);
             printf ("Please enter the key\n");
             scanf  ("%49s", key);
             keylen = strlen(key);
             keyidx = 0;    // starting index into key
             printf("Opening files\n");
    
             inFile = fopen(FILENAME,"rb");
             outFile = fopen(NEW_FILENAME, "wb");
    
             if(inFile == NULL)
             {
                printf("Error: Can't Open inFile\n");
             }
             else if(outFile == NULL)
             {
                printf("Error: Can't open outFile\n");
             }
             else
             {
                     printf("File Opened, Encrypting\n");
                     while((Byte = fgetc(inFile)) != EOF)    // read a byte, check if EOF
                     {
                             if (!strcmp(process,"encryption"))
                             {
                                     newByte = Byte + key[keyidx];    // use key index
                                     if (newByte > 255) newByte -= 256;   // check for overflow
                             }
                             else if (!strcmp(process,"decryption"))
                             {
                                     newByte = Byte - key[keyidx];
                                     if (newByte < 0) newByte += 256;
                             }
                             else
                             {
                                newByte = Byte;
                             }
                             fputc(newByte, outFile);
                             keyidx++;
                             // loop to the start of the key if needed
                             if (keyidx >= keylen) keyidx = 0;
                     }
             }
             if (inFile != NULL) fclose(inFile);     // close your files when you're done
             if (outFile != NULL) fclose(outFile);
    }

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by Carry for cents
    we already have this software, so I post it directly. This is called encryption, please do NOT change the terms! It becomes very hard to figure out what someone is talking about.
    The term "autokey cipher" refers to a particular kind of encryption algorithm.

    Quote Originally Posted by Carry for cents
    We are considering releasing a GUI version of this software with which you can encrypt everything, even .exe files. Test it for yourself to be sure.
    A quick glance shows that you have implemented a kind of substitution cipher of the "XOR cipher" variety, although the mechanism used is addition rather than XOR. This category of ciphers is equivalent to a one-time pad -- hence unbreakable -- when you have at least as much key material as plaintext, but with insufficient key material the encryption would be rather weak. Since having as much key material as plaintext is usually not practical, in practice what you have is usually just suitable for a student exercise rather than commercial software.

    Since your example is not an autokey cipher, it is irrelevant to this discussion.

    Quote Originally Posted by Carry for cents
    I don't know if advertisement is allowed on this site, so we can advertise our online shop.
    You're free to add an advertisement to your forum signature or to post in the Projects and Job Recruitment forum, but I think you will need to do much more research about cryptography before you go into the business of writing and selling software to securely encrypt stuff.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Jun 2020
    Posts
    2
    Thank you very much for this positive critic. I have not given the full version of the software, only an example for students. In fact I don't intend to ever post the full software, only part of it. Also yes, I know the key is important, I was considering adding a key within the software itself, but I decided its better to leave it to the user, and add a warning that the quality depends on the key length.

    As for the rest, I was left with the impression that this is what the thread starter wants. It looks the same as here. Maybe I am wrong. Sorry if its so.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by Carry for cents
    Also yes, I know the key is important, I was considering adding a key within the software itself, but I decided its better to leave it to the user, and add a warning that the quality depends on the key length.
    I'm not sure what to make of this: either you know your cryptography and are just trolling (with a second account), or you don't know your cryptography and so have no clue as to why "considering adding a key within the software itself" is hilariously painful to those of us who do have some background in cryptography.

    Quote Originally Posted by Carry for cents
    As for the rest, I was left with the impression that this is what the thread starter wants. It looks the same as here. Maybe I am wrong. Sorry if its so.
    That article you linked to does indeed demonstrate an autokey cipher. Your sample program doesn't. This would normally be a Good Thing since showing students a sample that isn't exactly what they are trying to do forces them to learn rather than just copy, but that wasn't how you presented your program in post #9.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Tags for this Thread