Frustration

This is a discussion on Frustration within the C++ Programming forums, part of the General Programming Boards category; k, I understand what you said but It stil won't work for me??? Here is my decrypt code. Code: #include ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    208

    Angry Frustration

    k,
    I understand what you said but It stil won't work for me???
    Here is my decrypt code.


    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <fstream.h>
    
    int main()
    { char a[502];
      char b[502];
      char c[502];
      ofstream a_file("unencrypt.txt");
      ifstream b_file("encrypt1.txt");
      cout<<"\n\t\t*******Your Encrypted Text***********";
      b_file>>c;
      cout<<c;
         for(int x=0; x<502; x++)
         {
           a[x]=b[x]^c[x];
           cout<<a;
           a_file<<a;
         }
         a_file.close();
         b_file.close();
         cout<<"\n\n Well Thats Your Files. The Unencrypted\ntext will be in a txt file\named unencrypt.txt";
          system("PAUSE");
          return 0;}


    And it still gives me a bunch of gibbity gork text (which i know is encrypted) I want it to output the text that was input into another program and encrypted to a text file??????
    here is my encrytion source????


    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <fstream>
    
    
    int main()
    { char a[502];
      char out[502];
      char c[502];
      char b[20]="DgFGH51ja8Md-klagf1";
      ofstream a_file("encrypt1.txt");
      cout<<"\n\n\t\t********Please Input Less Then 500 Chars*********\n\n";
      cin>>a;
      cout<<"\n\n\t\t ***********This Is Your Encoded Text*************** \n\n";
      for(int x=0; x<502; x++)
      {
        c[x]=a[x]^b[x];
        cout<<c[x];
        a_file<<c[x];
      }
      
      cout<<"\n\nWell Thats It. Encrypted Text Will Be Written In A 'encrypt.txt' file\n\n";
        a_file.close();
        system("PAUSE");
        return 0;
    }

    Please can you help me???????

    Thanx


    Kas

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    208
    by the way I am looking for some help with XOR encryption and such.

  3. #3
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317
    Kas,

    No need to have 2 threads to deal with your 1 problem.

    for(int problem=1;problem<code;problem++) createthread();

    Give me a couple of hours. I'll try and look at your code and see what's wrong. My youngest is asking for me... and you don't want to make a 3 year old your enemy
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

  4. #4
    Unregistered
    Guest
    either b and a need to be the same length or b has to be a single char.

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    194

    Re: Frustration

    You need to work on your use of streams.
    First, you declare c as an array of 502 chars, then you attempt to read a file into c using only 1 >>. If your lucky that will get the first char from the file into the first index (0) of the array c.
    A good way to read from files is either 1 char at a time checking for eof each iteration of a loop, or to read line by line using a string. You still have to check for eof each iteration


    Here is a function i wrote to do XOR (de)/(en)cryption to a file. It uses iostream.h stdio.h and string.h
    Check out the code challenge of this issue of the c journal
    http://www.cprogramming.com/codej/issue2.html
    It uses FILE *'s instead of streams, you should be able to change it becuase it is commented
    Code:
    int XOR(char * filename, unsigned long key)
    {
    	char * outfilename = NULL;
    	int len = strlen(filename);
    	unsigned char buffer;
    
    	if( (filename[len-4] == '.') && (filename[len-3] == 'e') && (filename[len-2] == 'n') && (filename[len-1] == 'c') )
    	{
    		// our input file is encoded then we will create a file without the .end extension
    
    		outfilename = new char[len+1]; //make room for the name+\0
    		strcpy(outfilename,filename); //copy the string name
    		outfilename[len-4] = '\0'; //put the \0 before the .enc extension to cut it off
    
    	}
    	else
    	{
    		outfilename = new char[len+5]; //make room for the name + .enc + \0
    		strcpy(outfilename,filename); //copy the file name
    		strncat(outfilename,".enc",4); //add the .enc extension
    
    	}
    
    	input =	fopen(filename,"rb");
    	if( input == NULL)
    	{
    		cout << "Error opening file " << filename << endl;
    		delete [] outfilename; //free the memory before leaving
    		outfilename = NULL;
    		return 1;
    	}
    	
    	output = fopen(outfilename,"wb");
    	if( output == NULL )
    	{
    		cout << "Error creating output file " << outfilename << endl;
    		delete [] outfilename; //free the mem before leaving
    		outfilename = NULL;
    		return 1;
    	}
    	while( ! feof(input) )
    	{
    		//get some data
    		if( fread(&buffer,sizeof(unsigned char),1,input) != 1 )
    		{
    			//if we didnt get any data, but we are not at the eof, then an error has occured
    			if( ! feof(input) )
    			{
    				delete [] outfilename;
    				outfilename = NULL;
    				fclose(input);
    				fclose(output);
    				
    				return 1;
    			}
    		}
    		else
    		{
    			//xor that data
    			buffer ^= key; 
    		
    			//write some data
    			fwrite(&buffer,sizeof(unsigned char),1,output);
    		}
    	}
    	//close the files and free that memory
    	fclose(input);
    	fclose(output);
    	delete [] outfilename;
    
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. arrays and piping and frustration
    By ihatec++ in forum C++ Programming
    Replies: 13
    Last Post: 05-28-2008, 07:31 AM
  2. Frustration with tutorial.
    By A34Chris in forum C Programming
    Replies: 19
    Last Post: 03-18-2008, 04:08 AM
  3. Mainboard Frustration
    By jrahhali in forum Tech Board
    Replies: 5
    Last Post: 08-26-2004, 11:01 PM
  4. Nested loop frustration
    By caroundw5h in forum C Programming
    Replies: 14
    Last Post: 03-15-2004, 09:45 PM
  5. Frustration
    By curlious in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 09-29-2003, 03:22 PM

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