basic encryption theory

This is a discussion on basic encryption theory within the C Programming forums, part of the General Programming Boards category; I am fairly new to the programming game, well, in C that is. Anyways, I wrote a small program that ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    7

    basic encryption theory

    I am fairly new to the programming game, well, in C that is. Anyways, I wrote a small program that should take user input, a single word and run it through a basic encryption algorithm, supplied in the source. The word is translated in decimal form and inserted into the algorithm so that it changes everytime you type something in. Here is the source.

    --------begenning of code-------------

    #include <stdio.h>
    main() {

    int unenc, enc;
    printf("Please enter a word that you would like encrypted --> \n");
    scanf("%s", &unenc);

    for ( enc = 2 + 533 + unenc * unenc - 456 * unenc - 65 + 34 )
    printf("Your word, encrypted is %s\n", enc);
    }

    ------------end of file---------------

    Now, I know that my for statement is the issue. I need to know how to use my unenc interger variable in the equation. What is the problem???

  2. #2
    UNIX chick
    Join Date
    Mar 2003
    Posts
    92

    Re: basic encryption theory

    Originally posted by y0gur7
    int unenc, enc;
    printf("Please enter a word that you would like encrypted --> \n");
    scanf("%s", &unenc);
    How did you compiler allow this?

    Edit: I need to type faster..

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    7
    okay, ive fixed it. My new version only accepts decimals, and it is also accompanied by a decryption program. however, my algorithm in the decrytion program is fouled up. Any suggestions, or help?
    Code:
    -----encrypt.c-----
    #include <stdio.h>
    main() {
    
    	int unenc, enc, pass;
    	printf("Please enter a decimal string that  you would like encrypted --> \n");
    	scanf("%d", &unenc);
    	printf("Now, please enter a password, decimal only please! -->\n");
    	scanf("%d", &pass);
    
    	enc = pass*2/(unenc+53+3)/(unenc+pass)+(unenc*72)/pass+34;
    
    	
    	printf("Your decimal string, encrypted is %d\n", enc);
    }
    -----end of encrypt.c-----
    
    ---start of decrypt.c-----
    
    #include <stdio.h>
    main() {
    
    	int unenc, enc, pass;
    	printf("Enter your encrypted decimal string for it to be decrypted-->\n");
    	scanf("%d", &enc);
    	printf("Please enter your password -->\n");
    	scanf("%d", &pass);
    
    	unenc = pass/2*(enc-53-3)*(enc-pass)-(enc/72)*pass-34;
    
    
    
    	printf("Your decimal string, unencrypted is %d\n", unenc);
    }
    
    -----end of decrypt.c-----

    Code tags added by Hammer.

  4. #4
    UNIX chick
    Join Date
    Mar 2003
    Posts
    92
    Your calculations look awfully cluttered, I'd put in a buch of parentheses. And remember; just reversing the operators doesn't necessarily reverse the calculation.

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    7
    That is true. I did add some parenthasis, and it just helped visually. My calculation however is still wrong. How would I go about reversing it? Any advice? General tips?

  6. #6
    Registered User
    Join Date
    Jun 2003
    Posts
    245
    You need to check your logic by running your program on paper rather than through a compiler. You'll find it's flawed.

    Always when doing things like this, ensure your logic is thoroughly tested on paper before writing a single line of code, otherwise you'll just be hitting the nearest brickwall with your head wondering why it doesn't work.

  7. #7
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Two steps to figure out how to decrypt it:

    1) Simplify your algorithm on paper.
    2) Then attempt to isolate enc.

    Good luck. I gave up trying.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  8. #8
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    Remember order of operations.
    enc = pass*2/(unenc+53+3)/(unenc+pass)+(unenc*72)/pass+34;
    simplifies to
    enc = ((pass*2)/(unenc+56)/(unenc+pass))+((unenc*72)/pass)+34;
    which in turn simplifies to
    enc = ((pass*2)/((uenc+56)*(uenc+pass))+((uenc/pass)*(72/pass))+34;

    It would be a lot better to use something like XOR encryption. You'll actually be able to undo it. It's not hard... look it up on Google.

    That said... the first thing you'll want to do when unencrypting will be this:

    uenc=enc-34;

    After that, you'll see that the remaining encryption is
    ((pass*2)/(unenc+56)/(unenc+pass))+((unenc/pass)*(72/pass))

    Yuck. Just examine that for a second and you'll realize there isn't a way to simplify that much more. In fact, you won't be simplifying it any more. Try a different algorithm - you have to use something you can undo.
    Away.

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Wow, 3 levels of "simplification", and it still looks like this:
    enc = ((pass*2)/((uenc+56)*(uenc+pass))+((uenc/pass)*(72/pass))+34;
    That's nasty Definately time to rethink. I agree XOR is a good place to start, it's easy to understand and simple to implement, and the best thing: write the encryption function, and you've automatically created the decryption one without even trying.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Registered User
    Join Date
    Jun 2003
    Posts
    7
    LOL!!! I know it looks nasty. I have it a little more organized, but worse looking. The problem is, decryption based on the system that I have is impossible. The encryption algorithm that i have here is dependant on 2 variables. Your word that you plan to encrypt and a passphrase. Unfortunatly, both are needed for decryption as well. that effectivly removes the whole idea of encryption. If you already know the word, why decrypt it? lol. Any suggestions in changing the entire algorithm process that I have here, and replacing it with a meathod that is reversable? Specific examples, cuz I am fairly new.

  11. #11
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    Yeah, as I said, look up XOR encryption. All you have to do to decrypt it is encrypt it the same way again.

    Here's a link
    Away.

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    There was an encryption contests here once before. Have a look through the source here:
    http://contests.cprogramming.com/ind...to...&theme=02
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  13. #13
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    You can also do some very simple encryption by reversing/switching bytes, nibbles, and words. Make everything a 32-bit value and simply switch the words from high to low or switch every other nibble in each word. These are simple to break, but they are easy to code as well.

    You can also do a type of RLE with text files, although it is not as efficient as it is with bitmaps.

  14. #14
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638

    algorithm

    There are many ways to do the same thing. Why breaking your decrypt and encrypt function up in to several individual mathmatical functions was not suggested I do not know. Meow!

    Being the same prog posted on the HDC leaves me with the questtion why only a number to encrypt? even with xor only numbers are easyer to crack then strings. Meow!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advice on writing a basic encryption program?
    By osiris^ in forum C Programming
    Replies: 2
    Last Post: 09-10-2007, 03:02 PM
  2. Problem with basic encryption program.
    By mmongoose in forum C++ Programming
    Replies: 5
    Last Post: 08-27-2005, 05:41 AM
  3. Basic encryption program???
    By Finchie_88 in forum C++ Programming
    Replies: 14
    Last Post: 09-10-2004, 10:01 AM
  4. basic encryption
    By devour89 in forum C++ Programming
    Replies: 5
    Last Post: 12-04-2002, 04:25 PM

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