Thread: Problem with simple XOR program

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    5

    Problem with simple XOR program

    Hey all, this is my first post here so if Im breaking any rules please let me know. Ive been programming since I was about 10 or 11, I started out with liberty basic and I remember the first program I wrote was

    Code:
    Input A
    Input B
    A+B=C
    Print C
    After that it was a little dabbleing in AI and then I fell in love with encryption. I wrote a Simple Substitution program, it was great! It had two passwords one the user entered and the other that was saved as a text file that if it wasnt found would delete the message and the program (which turned to to be a real pain during testing). I even had it set up so you could select one of three keys, I felt like not even the enigma could bet me out. Heck I even had the decoder automatically open up a .txt file and decode it. You didnt even have to tell the program what cipher you used! Then disaster struck, I found a way to break my master piece with pencil, paper and a highlighter if I could find it. So then me and my grand father ( A programmer of the punch card days) we decided to tackel the almigthy C++. We knew that simple substitution wasnt going to be enough, so off to google it was. Then I found this sight and there it was XOR and now I have version 1.0 done and I keep coming up with new ideas. Sorry I digress



    My problem is since Im currently using a DOS program and not a windows app, users cant copy and paste the material that they would like to encrpyt. So Im trying to get it so the user can open up wordpad and copy and paste into there and save it as a ritch-text format (.rtf) file. Then when you run the encryption program it opens the file up and you enter the key. I have a decoder that does the same thing but I cant get it to work with the encoder.




    Here is the working encoder and decoder

    Encoder:

    Code:
    /*XOR encryption program v1.0*/
    #include <fstream.h>
    #include <iostream.h>
    #include <stdlib.h>
    
    
    int main (void)
    {
    
    int message_length, cipher_length, j, k;  
    char message[500];    
    char cipher[100];           
    std::cout <<"Key in your message, up to 500 characters.";
    std::cout << "\n";
    std::cin.getline(message,500); 
    std::cout <<"Key in your cipher, up to 100 characters.";
    std::cout << "\n";
    std::cin.getline(cipher,100);     
    message_length = strlen (message); 
    cipher_length = strlen (cipher);   
    
    
    j=1;
    k=1;
    while((j<=message_length)){
                           message[j-1]=message[j-1]^cipher[k-1]; 
                           if((k==cipher_length)){
                                                k=0;
                                                }
                           j=j+1;
                           k=k+1;
                           }
    ofstream a_file ( "example.rtf" );
    a_file<< message;
    std::cout << message;                             
    std::cout << "\n";
    
    
    std::cout << "\n";
      system("PAUSE");
         return 0;
    
    
    }
    Decoder:
    Code:
    /*XOR Decryption program v1.0*/
    #include <fstream.h>
    #include <iostream.h>
    #include <stdlib.h>
    
    
    int main (void)
    {
    
    int message_length, cipher_length, j, k;  
    char message[500];    
    char cipher[100];           
    ifstream b_file ( "example.rtf" );
    b_file>> message;
    std::cout <<"Key in your cipher, up to 100 characters.";
    std::cout << "\n";
    std::cin.getline(cipher,100);     
    message_length = strlen (message); 
    cipher_length = strlen (cipher);   
    
    j=1;
    k=1;
    while((j<=message_length)){
                           message[j-1]=message[j-1]^cipher[k-1]; 
                           if((k==cipher_length)){
                                                k=0;
                                                }
                           j=j+1;
                           k=k+1;
                           }
    std::cout << message;                              
    std::cout << "\n";
    
    
    std::cout << "\n";
      system("PAUSE");
         return 0;
    
    
    }


    Here is the one thats not working
    Code:
    /* XOR encryption program v1.1b*/
    #include <fstream.h>
    #include <iostream.h>
    #include <stdlib.h>
    
    
    int main (void)
    {
    
    int message_length, cipher_length, j, k; 
    char message[500];    
    char cipher[100];      
    ifstream b_file ( "message.rtf" );
    b_file>> message;
    std::cout <<"Key in your cipher, up to 100 characters.";
    std::cout << "\n";
    std::cin.getline(cipher,100);    
    message_length = strlen (message); 
    cipher_length = strlen (cipher);   
    
    std::cout << "message is - " << message;      
    std::cout << "\n";
    std::cout << "cipher is - " << cipher;         
    std::cout << "\n";
    std::cout << "length of message is " << message_length; 
    std::cout << "\n";
    std::cout << "length of cipher is " << cipher_length; 
    std::cout << "\n";
    std::cout << "the first character of message is " << message[0]; 
    std::cout << "\n";
    std::cout << "the last character of message is " << message[message_length-1];
    std::cout << "\n";
    std::cout << "the first character of cipher is " << cipher[0]; 
    std::cout << "\n";
    std::cout << "the last character of cipher is " << cipher[cipher_length-1]; 
    std::cout << "\n";
    
    j=1;
    k=1;
    while((j<=message_length)){
                           message[j-1]=message[j-1]^cipher[k-1]; 
                           if((k==cipher_length)){
                                                k=0;
                                                }
                           j=j+1;
                           k=k+1;
                           }
    ofstream a_file ( "example.rtf" );
    a_file<< message;
    std::cout << message;                               
    std::cout << "\n";
    
    
    std::cout << "\n";
      system("PAUSE");
         return 0;
    
    
    }
    All the stuff in the middle acts as sort of like a error checking system. I really dont have any idea what im doing, Im not learning this A,B,C. Im just figuring out what I need as I go. So please dont smite me and make me feel little under your superior knowlege of C++. So any suggestions as to whats wrong?

  2. #2
    Banned
    Join Date
    Jun 2005
    Posts
    594
    ok first of all, your most likely using
    a command prompt and not a dos.
    dos is a primitve operating system.

    second of all you are using outdated header files


    #include <iostream>
    #include <fstream>

    and 3rd, system() calls are a no no,
    short reason why is because malicous
    program could replace the program system calls.

    4th :
    its a prefrence on using std::
    but if you dont like typing that infront of
    everything you can put

    using namespace std;

    and you wont have to put std::
    infront of everything, as for your problem
    a specfic question would be nice, but im in a good
    mood so give me a few minute to compile your code
    and see what i can figure out.

    and last but no least, if your going to do c++
    use strings instead of char arrays.
    Last edited by ILoveVectors; 08-16-2005 at 10:55 PM.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    5
    I knew what it was I just couldnt think of its name. Ill change it to using namespace std;, but what is this system call you speak of?

  4. #4
    Banned
    Join Date
    Jun 2005
    Posts
    594
    where you are using

    system("PAUSE");

    pause is actualyl a program your program is calling to
    pause it, PAUSE could be replaced witha malicous program.

    anyways here is your code, done a little better
    feel free to ask quesiton on it ill be aroudn for a while
    or you can message me on a messenger. below it i will post
    some code i helped some with earlier. because eventually
    you will find a problem with your code and that is
    you are no reading the files in bianry mode, so
    here goes nothing

    Code:
    /* XOR encryption program v1.1b*/
    #include <fstream>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main ()
    {
    	size_t message_length, cipher_length; 
    	string message;    
    	string cipher;      
    	ifstream b_file ( "message.rtf" );
    	if(b_file.is_open())
    	{
    		getline(b_file, message, '\n');
    	}
    	cout <<"Key in your cipher, up to 100 characters.";
    	getline(cin, cipher, '\n');   
    	message_length =  message.length();
    	cipher_length =   cipher.length();
    	
    	cout << "message is - " << message;      
    	cout << "\n";
    	cout << "cipher is - " << cipher;         
    	cout << "\n";
    	cout << "length of message is " << message_length; 
    	cout << "\n";
    	cout << "length of cipher is " << cipher_length; 
    	cout << "\n";
    	cout << "the first character of message is " << message[0]; 
    	cout << "\n";
    	cout << "the last character of message is " << message[message_length-1];
    	cout << "\n";
    	cout << "the first character of cipher is " << cipher[0]; 
    	cout << "\n";
    	cout << "the last character of cipher is " << cipher[cipher_length-1]; 
    	cout << "\n";
    	
    	for(int j = 0, int i = 0; j != message_length; j++, i++)
    	{
    		message[j] = message[j]^cipher[i];
    		if(i == cipher_length)
    		{
    			i = 0;
    		}
    	}
    	ofstream a_file ( "example.rtf" );
    	if(a_file.is_open())
    	{
    		a_file << message;
    	}
    	cout << message;                               
    	cout << "\n\n";
    	cin.get();
    	return 0;
    }
    and the other code
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    	string key = "Asdfasdghafdhadfhadf"; //must include <string> to use
    	string filenamein = "file.txt";
    	string filenameout = "newfile.txt";
    	ifstream in(filenamein.c_str(), ios::binary);
    	ofstream out(filenameout.c_str(), ios::binary);
    	char ch, y;
    	if(in.is_open())
    	{
    		for(int z = 0; in.read( (char*)&ch, sizeof(ch) ) ; z++)
    		{
    			if(z == key.length())
    			{
    				z = 0;
    			}
    			y = ch^key[z];
    			out.write (  (char*)&y, sizeof(y) );
    		}
    	}
    	in.close();
    	out.close();
    	return 0;
    }
    btw i used the program/source that i coded to color that
    any body interested in testing it, you can find a link to
    it on the boards here.
    Last edited by ILoveVectors; 08-16-2005 at 11:11 PM.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    5
    I get a few errors when I try to compile it. Mostly to do with the int i. I tried to fix it but I couldnt come up with anything.

  6. #6
    Banned
    Join Date
    Jun 2005
    Posts
    594
    what compiler do you use, and did you change that code at all?

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    5
    Im using Dev C++, I ran the orignal code and then I saved a copy and played around with it trying to get it to work. Im sorry I forgot to mention what complier Im using.

  8. #8
    Banned
    Join Date
    Jun 2005
    Posts
    594
    well if your having problem with the code you changed, you
    obviously going to need to repost your code so we can
    see where you messed up.

  9. #9
    Registered User
    Join Date
    Aug 2005
    Posts
    5
    I didnt save any of the changes I made since they didnt work. All I can see wrong with the orignal is there are a few missing ;'s but other then that I have no idea what so ever whats going on.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A simple file I/O problem
    By eecoder in forum C Programming
    Replies: 10
    Last Post: 10-16-2010, 11:00 PM
  2. Problem in simple code.
    By richdb in forum C Programming
    Replies: 6
    Last Post: 03-20-2006, 02:45 AM
  3. Problem in very simple code
    By richdb in forum C Programming
    Replies: 22
    Last Post: 01-14-2006, 09:10 PM
  4. Simple Initialization Problem
    By PsyK in forum C++ Programming
    Replies: 7
    Last Post: 04-30-2004, 07:37 PM
  5. Simple OO Problem
    By bstempi in forum C++ Programming
    Replies: 1
    Last Post: 04-30-2004, 05:33 PM