Like Tree2Likes

XOR Encryption & Decrypt.

This is a discussion on XOR Encryption & Decrypt. within the C Programming forums, part of the General Programming Boards category; Please view my code below... Code: #include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { char data[100]; int key; int ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    67

    XOR Encryption & Decrypt.

    Please view my code below...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
        int main(void)
        {
            char data[100];
            int key;
            int length;
            int i;
    
            printf("Enter 2 digits key: ");
            scanf("%d", &key);
    
            flushall();
    
            printf("\nEnter data to encrypt: ");
            scanf("%[^\n]", &data);
    
            length = strlen(data);
    
            for(i=0; i<length; i++)
            {
                data[i] = data[i]^key;
            }
    
            printf("\n%s\n", data);
    
        }
    Did my code above encrypt my string successfully? Because I just started working on this so I not sure that did my string has been protected. Also how can I decrypt it after I encrypt it? Very thanks you.

  2. #2
    Registered User
    Join Date
    Oct 2011
    Posts
    67
    Ok...now i have updated my code...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
        int main(void)
        {
            char data[100];
            int key;
            int length;
            int i;
    
            /* Start Encrypt */
    
            printf("Enter 2 digits key: ");
            scanf("%d", &key);
    
            flushall();
    
            printf("\nEnter data to encrypt: ");
            scanf("%[^\n]", &data);
    
            length = strlen(data);
    
            for(i=0; i<length; i++)
            {
                data[i] = data[i]^key;
            }
    
            printf("\n==============");
            printf("\nEncrypted Data");
            printf("\n==============");
            printf("\n%s\n", data);
    
            flushall();
    
            /* Start Decrypt */
    
            printf("\nEnter unlock key: ");
            scanf("%d", &key);
    
            for(i=0; i<length; i++)
            {
                data[i] = data[i]^key;
            }
    
            printf("\n==============");
            printf("\nDecrypted Data");
            printf("\n==============");
            printf("\n%s\n\n", data);
    
    
    
        }
    Now my problem is how can I keep looping to lets user input key as long as they have inputed incorrect keys?

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Delete the & before the scanf() for data. Data is a string, so it is it's own address.

    A while loop is a good loop to use for a repeating effort by the user.

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    67
    Well, I know that I should use while loop but how can my program determine that the key which input by the user is incorrect?

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Put it into a variable, which is itself encoded, perhaps?

    Read it from a file?

    The thing is, to work, it has to have a good design to it, and that includes key handling/storage. One weak link and your security is *POOF* gone!

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    67
    Ok, I have updated my code so that the key store into another variable...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
        int main(void)
        {
            char data[100];
            int key;
            int length;
            int i, pass;
    
            /* Start Encrypt */
    
            printf("Enter 2 digits key: ");
            scanf("%d", &key);
            
            /* Store key into pass */
            pass = key;
    
            flushall();
    
            printf("\nEnter data to encrypt: ");
            scanf("%[^\n]", data);
    
            length = strlen(data);
    
            for(i=0; i<length; i++)
            {
                data[i] = data[i]^key;
            }
    
            printf("\n==============");
            printf("\nEncrypted Data");
            printf("\n==============");
            printf("\n%s\n", data);
    
            flushall();
    
            /* Start Decrypt */
    
            do 
            {
    
                printf("\nEnter unlock key: ");
                scanf("%d", &key);
    
                for(i=0; i<length; i++)
                {
                    data[i] = data[i]^key;
                }
    
            } while (key != pass);
    
    
            printf("\n==============");
            printf("\nDecrypted Data");
            printf("\n==============");
            printf("\n%s\n\n", data);
    
    
    
        }
    Now the problem is when the user input invalid key it will loop back then ask again. Now is the main problem, after user input the valid key in the 2nd time. My program accept the key but it still print the encrypt data but not the decrypt data. How could I solve this problem?

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    67
    I have solve this problems ~ ^^

  8. #8
    Registered User
    Join Date
    Oct 2011
    Posts
    67
    Please view my code below.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
        void encrypt(); 
        void decrypt(char data[], int pass, int length);
    
        int main(void)
        {
            encrypt();
            decrypt(data[], pass, length);
        }
    
        void encrypt()
        {
            char data[100];
            int key;
            int i;
            int pass;
            int length;
    
            /* Start Encrypt */
    
            printf("Enter 2 digits key: ");
            scanf("%d", &key);
            
            /* Store key into pass */
            pass = key;
    
            flushall();
    
            printf("\nEnter data to encrypt: ");
            scanf("%[^\n]", data);
    
            length = strlen(data);
    
            for(i=0; i<length; i++)
            {
                data[i] = data[i]^key;
            }
    
            printf("\n==============");
            printf("\nEncrypted Data");
            printf("\n==============");
            printf("\n%s\n", data);
    
        }
    
        void decrypt(char data[], int pass, int length)
        {
            int i, key;
    
            do 
            {
                flushall();
    
                printf("\nEnter unlock key: ");
                scanf("%d", &key);
    
            } while (key != pass);
        
            for(i=0; i<length; i++)
            {
                data[i] = data[i]^key;
            }
    
            printf("\n==============");
            printf("\nDecrypted Data");
            printf("\n==============");
            printf("\n%s\n\n", data);
    
        }
    error C2065: 'data' : undeclared identifier

    Why? I am sure I already pass the data...

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Adak View Post
    The thing is, to work, it has to have a good design to it, and that includes key handling/storage. One weak link and your security is *POOF* gone!
    ... and so is your data.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Declare data[] in main(), and then pass it to all your functions. You're not doing that now.

  11. #11
    Registered User
    Join Date
    Oct 2011
    Posts
    67
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
        void encrypt(); 
        void decrypt(char data[100], int pass, int length, int key, int i);
    
        int main(char data[100], int pass, int length, int key, int i)
        {
            encrypt();
            decrypt(data, pass, length, key, i);
        }
    
        void encrypt()
        {
            char data[100];
            int key;
            int i;
            int pass;
            int length;
    
            /* Start Encrypt */
    
            printf("Enter 2 digits key: ");
            scanf("%d", &key);
            
            /* Store key into pass */
            pass = key;
    
            flushall();
    
            printf("\nEnter data to encrypt: ");
            scanf("%[^\n]", data);
    
            length = strlen(data);
    
            for(i=0; i<length; i++)
            {
                data[i] = data[i]^key;
            }
    
            printf("\n==============");
            printf("\nEncrypted Data");
            printf("\n==============");
            printf("\n%s\n", data);
    
        }
    
        void decrypt(char data[100], int pass, int length, int key, int i)
        {
            printf("%d", pass); // Pass already change....why?
    
            do 
            {
                flushall();
    
                printf("\nEnter unlock key: ");
                scanf("%d", &key);
    
            } while (key != pass);
        
            for(i=0; i<length; i++)
            {
                data[i] = data[i]^key;
            }
    
            printf("\n==============");
            printf("\nDecrypted Data");
            printf("\n==============");
            printf("\n%s\n\n", data);
    
        }
    Now the declare problem has solve...but now is the loop problems. Even when the user input valid key my program continue looping...I wonder why?

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Not this:
    Code:
    int main(char data[100], int pass, int length, int key, int i)
    
    but this:
    int main(void) 
    {
        char data[100];
        int pass, length, key, i;
    
        //all your other code for main in here
    
        return 0;
    }

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Siaw Ys View Post
    Code:
    int main(char data[100], int pass, int length, int key, int i)
        {
            encrypt();
            decrypt(data, pass, length, key, i);
        }
    Now the declare problem has solve...but now is the loop problems. Even when the user input valid key my program continue looping...I wonder why?
    No the declare problem is not solved... what you've done now is create a main() function that will not work correctly with most C compilers. Variables are not declared in the fuction's top line... they are declared inside the fuction, before any active code...
    Code:
    int main (void)
     { 
       int var1;
       int var2;
       // etc.
    Now please don't be offended, I'm trying to help you here...

    I've watched you across several threads now and I can say with a fair bit of confidence that not only are you not "getting it", you don't seem to get that you are not getting it... A lot of your code seems to come from guesswork... the result of either not understanding your lessons or not paying attention to C's rules.

    What you need to do is pull out a C textbook (or a good online tutorial) and STUDY the thing... don't just look at the code snippets, and don't just skim over the words... read every word, repeat until you understand... type up and compile all the examples, change the code, break it, fix it, play with it until you understand what works and what doesnt... then move on to the next page... repeat until you've completed the whole book.

    The thing is that you're coming here for very simple "first week" stuff you should be able to figure out on your own by now... We have no problem answering questions, that's why we volunteer our time... But there comes a point where it's obvious someone isn't learning and is relying upon us to correct their foul-ups... at which point you risk losing whatever minimal support you have.

    You can be sure that, once in the workplace, this strategy is going to make you a prime target for sabotage. People will hand you bad code, just to watch you fail... and that might even start happening here!

    Seriously... you need to LEARN C... rather than begging for help.
    Last edited by CommonTater; 12-10-2011 at 08:22 AM.

  14. #14
    Registered User
    Join Date
    Oct 2011
    Posts
    67
    I understand what you mean. All the problem which I post is the problem which I has try to figure by my own but I still can't solve so I need help. I also try to just rely by myself solve it by myself as many as I can but sometimes I really can't solve some problems. Of course, before posting any question, I has also read all my note then I think. I solve some but not all so that why I request help. Yes, some of my coding is guessing, but I also trust that I can learn after what you all have teach me. So, from the guess it will become an knowledge...But I just hope to let you know that I no intent to beg, at least I sure that I have try my best to solve before request all of you help...
    Last edited by Siaw Ys; 12-10-2011 at 08:31 AM.

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Siaw Ys View Post
    I understand what you mean. All the problem which I post is the problem which I has try to figure by my own but I still can't solve so I need help. I also try to just rely by myself solve it by myself as many as I can but sometimes I really can't solve some problems. Of course, before posting any question, I has also read all my note then I think. I solve some but not all so that why I request help. Yes, some of my coding is guessing, but I also trust that I can learn after what you all have teach me. So, from the guess it will become an knowledge...But I just hope to let you know that I was not beg, at least I sure that I have try my best to solve before request all of you help...
    Do you not have a course textbook you can study, for a better understanding.

    The problem here is not that you are running into increasingly complex problems as would be expected throughout a course... The problem is that you do not appear to be grasping even the basics of program organization, variable declaration, etc.; day one stuff.

    Remember... teaching C is your professor's job... not ours.

    You will never learn programming by guesswork, it's never happened before and it's not about to happen anytime soon. All you do is move yourself repeatedly from one screw up to the next and eventually you will become so confused and frustrated you will give up. The only way is as I described above... by deliberate study. (In other words: You can't dumbass your way through this)

    There are all kinds of free tutorials on the web. This doesn't need to cost you money you don't have... but you do need to pay attention to the lessons and you need to understand how this works if you are ever to be any good at it.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Encrypt/Decrypt text
    By patneel in forum C Programming
    Replies: 6
    Last Post: 09-06-2011, 02:55 PM
  2. Decrypt this!
    By francoissoft in forum Contests Board
    Replies: 21
    Last Post: 07-29-2007, 11:11 AM
  3. decrypt/encrypt
    By wonderpoop in forum C Programming
    Replies: 15
    Last Post: 10-18-2006, 06:10 PM
  4. Encrypt/Decrypt
    By bitWise in forum C Programming
    Replies: 2
    Last Post: 10-14-2001, 03:48 PM
  5. how to encrypt/decrypt
    By bitWise in forum C Programming
    Replies: 3
    Last Post: 10-13-2001, 01:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21