Thread: Require help decrypting XOR.

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    22

    Require help decrypting XOR.

    Hello, all.

    After what seems like hours of searching on various search engines, and websites, I have decided to come here for help.

    I have this code:
    Code:
    for ( int i = 0; i < encrypt_input.length(); i++ ) {
    	      encrypt_input[i] = encrypt_input[i]^key[i];
    	      cout << encrypt_input[i];
    	  }
    Does anyone know how I would go about decrypting the encrypted result?

    Thanks in advance.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Yes, but it's somewhat complicated:

    Code:
    for ( int i = 0; i < encrypt_input.length(); i++ ) {
    	      encrypt_input[i] = encrypt_input[i]^key[i];
    	      cout << encrypt_input[i];
    	  }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    22
    Hi, and thanks for your reply.

    Unfortunately I do not know if you're being sarcastic, or not, as I just started learning cryptography involving C++.

    Would you mind further explaining?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The point of XOR is that it undoes itself (much like rot13); if you re-encrypt the encrypted thing you get the original back.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    0 ^ 0 == 0
    1 ^ 0 == 1
    0 ^ 1 == 1
    1 ^ 1 == 0

    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    22
    Quote Originally Posted by tabstop View Post
    The point of XOR is that it undoes itself (much like rot13); if you re-encrypt the encrypted thing you get the original back.
    That's funny, I've already tried that, although I will try something else, and get back to you.

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    22
    After going over a few things, I am getting somewhat of the desired output, but not all of it, and one or two characters are capitalized, when they are supposed to be lowercase.

    Are there any errors in the code snippet that anyone can point out?

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    As long as the key is in fact as long as the data, then you shouldn't have any problems. Why don't you post the code?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    22
    It's basically like this:
    Code:
    	if ( option == crypter ) {
    			cout << "\nEncrypt or decrypt? - ";
    			cin >> crypter_option;
    			if ( crypter_option == encrypt ) {
    				system("cls");
    				cout << "\nEnter text:\n";
    				cin >> encrypt_input;
    				//getline(cin, encrypt_input, '`');
    				//reverse(encrypt_input.begin(), encrypt_input.end());
    				system("cls");
    				for ( int i = 0; i < encrypt_input.length(); i++ ) {
    					encrypt_input[i] = encrypt_input[i]^key[i];
    					cout << encrypt_input[i];
    				}
    				cout << "\n";
    			}
    ......
    Code:
    string key = "ABCDEFGHIJK";

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by mkthnx001 View Post
    It's basically like this:
    Code:
    	if ( option == crypter ) {
    			cout << "\nEncrypt or decrypt? - ";
    			cin >> crypter_option;
    			if ( crypter_option == encrypt ) {
    				system("cls");
    				cout << "\nEnter text:\n";
    				cin >> encrypt_input;
    				//getline(cin, encrypt_input, '`');
    				//reverse(encrypt_input.begin(), encrypt_input.end());
    				system("cls");
    				for ( int i = 0; i < encrypt_input.length(); i++ ) {
    					encrypt_input[i] = encrypt_input[i]^key[i];
    					cout << encrypt_input[i];
    				}
    				cout << "\n";
    			}
    ......
    Code:
    string key = "ABCDEFGHIJK";
    Everything you've posted here works perfectly, assuming encrypt is defined appropriately and you type a string of 11 letters or less. You will probably want to make sure i doesn't walk off the end of key either (presumably you want to do i%11 or something).

  11. #11
    Registered User
    Join Date
    May 2009
    Posts
    22
    Quote Originally Posted by tabstop View Post
    Everything you've posted here works perfectly, assuming encrypt is defined appropriately and you type a string of 11 letters or less. You will probably want to make sure i doesn't walk off the end of key either (presumably you want to do i%11 or something).
    Just to be clear, using getline(cin, encrypt_input, '`'); will cause errors, unlike cin >> encrypt_input;, right?

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by mkthnx001 View Post
    Just to be clear, using getline(cin, encrypt_input, '`'); will cause errors, unlike cin >> encrypt_input;, right?
    Why? The only difference would be how it handles spaces and line ending - are you sure you want people to type in a back-quote to end the sentence? Seems a bit obscure, compared to the usual method of newline.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    May 2009
    Posts
    22
    Quote Originally Posted by matsp View Post
    Why? The only difference would be how it handles spaces and line ending - are you sure you want people to type in a back-quote to end the sentence? Seems a bit obscure, compared to the usual method of newline.

    --
    Mats
    I was asking because I seemed to be getting the wrong output when decrypting, while using said code, but after replacing the back-quote character with the new line character, the problem seems to have been solved.

    Although this brings upon another problem, how would I allow new lines (enter) being used in the input text, without it terminating? Would I have to use a different delimiter character? I don't want any common characters being able to terminate the input, is all.

  14. #14
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Try this:

    Code:
    #include <iostream>
    #include <string>
    #include <iterator>
    
    using namespace std;
    
    template< typename Buffer, typename Key >
    void xor_buffer( Buffer& buffer, Key const& key )
    {
    	for( typename Buffer::iterator seq = buffer.begin( ), fin = buffer.end( ); seq != fin; ++seq )
    	{
    		for( typename Key::const_iterator seq2 = key.begin( ), fin2 = key.end( ); seq2 != fin2; ++seq2 )
    		{
    			*seq ^= *seq2;
    		}
    	}
    }
    
    int main( void ) 
    { 
    	string 
    		key = "ABCDEFGHIJK", 
    		input;
    	for( ;; )
    	{
    		cout << "Enter some text to encode > ";
    		if( !getline( cin, input ) || input.empty( ) )
    			break;
    		xor_buffer( input, key );
    		copy( input.begin( ), input.end( ), ostream_iterator< string::value_type, char >( cout ) );
    		cout << endl;	
    	}		
    	return 0;
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you want to encode text including newlines, you do indeed need a different end-of-input indicator. Have you heard of "end-of-file"? It is most often used for FILES, rather than input from the console, but console applications can be given an end-of-file (or "end-of-input") indication from the keyboard in most common OS's. In Windows you press CTRL-Z, and in Linux/Unix/MacOS (later models at least), you use CTRL-D. That indicates "there is no more input". And it is unlikely to conflict with anything else.

    Alternatively, use something like "if you type %END% on a it's own line" [the exact string, you can choose what you like, as long as it's something that isn't likely to be on part of the normal in the input] it terminats the input.

    With either of the solutions, read the input one line (ending with newline) at a time, and encrypt/decrypt the input, until you get to the end-marker (do not encrypt the end marker if you use text something like the %END%, but output it so that if you read in the output again, it will still know where the end is). You may want to watch out for encrypting such a way that the output becomes unprintable too. There are keys that will produce that result - for example, a key that is exactly the same as the clear-text will produce a string of zero bytes. With classic C style strings, that won't work.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Couple of questions about XOR linked lists...
    By klawson88 in forum C Programming
    Replies: 5
    Last Post: 04-19-2009, 04:55 PM
  2. Simple Xor Encryption help
    By soadlink in forum Windows Programming
    Replies: 13
    Last Post: 10-18-2006, 11:51 AM
  3. what the XOR!!
    By the bassinvader in forum C Programming
    Replies: 10
    Last Post: 08-11-2006, 02:08 PM
  4. Function program not working...
    By sirSolarius in forum C++ Programming
    Replies: 7
    Last Post: 09-26-2003, 07:35 PM
  5. XOR lists?
    By bgrahambo in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2002, 10:02 PM