Thread: Function program not working...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    27

    Function program not working...

    Can someone explain why this doesn't work?

    Code:
    #include <iostream.h>
    #include <string.h>
    
    char* encrypt(char *passD, char *phraseD);
    char* decrypt(char *passD, char *encryptedD);
    
    void main()
    {
    	char *encrypted, pass[10], phrase[30], *passer, *phraser, choice;
    	int x, d=0;
    		cout << "\nPlease type a password:\n";
    		cin.getline(pass, 9);
    		
    		cout << "\nPlease type a phrase to encrypt:\n";
    		cin.getline(phrase, 29);
    		passer = &pass[0];
    		phraser = &phrase[0];
    		encrypted = encrypt(passer,phraser);
    		cout << "\n--------------------\nEncrypted phrase is:\n "<< encrypted;		
    		cout << "\n\nHey, this works!\n" << decrypt(passer, encrypted) << "\n";
    
    }
    
    char* encrypt(char *passD, char *phraseD)
    {
    	char encrypted[60];
    	int x, d = 0;
    		for (x=0; x < strlen(phraseD); x++)
    		{
    			if(d > strlen(passD))
    				d = 0;
    			encrypted[x] = *(phraseD+ x) ^ *(passD +d);
    			d++;
    		}
    		return &encrypted[0];
    }
    
    
    char* decrypt(char *passD, char *encryptedD)
    {
    	char phrase[60];
    	int x, d = 0;
    		for (x=0; x < strlen(encryptedD); x++)
    		{	
    			if(d > strlen(passD))
    				d = 0;
    			phrase[x] = *(encryptedD+ x) ^ *(passD +d);
    			d++;
    		}
    	return &phrase[0];
    }
    Thanks!
    sirSolarius

  2. #2
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    Which part are you questioning?
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    27
    It just doesn't encrypt or decrypt anything. Try it.

    sirSolarius

  4. #4
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    Your encription is or-ing one letter plus a value with one letter plus a value that is in binary:

    1001 exclusive or'ed with 0011 becomes 1010
    -----------------------
    1001
    0011
    ------
    1010

    That is were there is two 1's they become zero and one 1 and one 0 become 1.

    I do belive you can not return from that value because you would not know where you originally started.
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    27
    Originally posted by velius
    1001 exclusive or'ed with 0011 becomes 1010
    -----------------------
    1001
    0011
    ------
    1010
    Ok.

    1010
    0011
    ----------
    1001

    That gives you the original number.

    sirSolarius

  6. #6
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    Yes that does but there is no guarantee that can work every time. Remember the 0 is created both by two 0's and two 1's.
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

  7. #7
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Yes, it always works. For those who know boolean algebra:

    a xor b = (a and !b) or (b and !a)

    or, to use simpler notation:

    a!b + b!a

    (a xor b) xor b = (a!b + b!a)!b + b!(a!b + b!a)

    = a!b!b + b!a!b + b(!a + b)(!b + a) [The second part uses deMorgan's law that to invert a large expression, one can invert every term and swap AND and OR]

    = a!b + 0 + b(!a!b + ab)

    = a!b + b!a!b + bab

    = a!b + ab

    = a(!b + b)

    = a(1)

    = a

    So, for all a, b:

    (a xor b) xor b == a

    Alternately, one could approach this using the fact that xor is a commutative operator; that is, (a xor b) xor c == a xor (b xor c).

    By this property:
    (a xor b) xor b = a xor (b xor b)
    = a xor (0)
    = a

    As long as you know b, the "key" that you xor with, you can always recover a (the original value) from a xor b (the encoded value).

    Oh, the above boolean algebra holds equally true for any bit length a, b -- but note, the constant '1' in the boolean algebra represents a number with all positions as '1', so if a and b are 4 bits, '1' is '1111'.
    Last edited by Cat; 09-26-2003 at 06:38 PM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  8. #8
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    You have tought me something I did not know about xor'ing. Thank you, Cat.
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. prime number program with function
    By mackieinva in forum C Programming
    Replies: 17
    Last Post: 09-20-2007, 08:36 AM
  2. Consumer program
    By wise_ron in forum C Programming
    Replies: 11
    Last Post: 09-27-2006, 05:21 AM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM