Encrypt/Decrypt files by XOR

This is a discussion on Encrypt/Decrypt files by XOR within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by The Brain Not good. If you want to do it that way, you would have to do ...

  1. #16
    Amy amirahasanen1's Avatar
    Join Date
    Jul 2003
    Posts
    49
    Quote Originally Posted by The Brain
    Not good. If you want to do it that way, you would have to do something like this:
    Code:
    while(!source.eof() && j<codeNO)
    {
    	source.get(c);
    	file_array[j] = c;
    	j++;
    //At this point, you can test for eof and influence j's resulting value
    if(source.eof())
    	 codeNO = j;
    }
    Oh yes, true.
    It ain't illegal until you get caught..

  2. #17
    Amy amirahasanen1's Avatar
    Join Date
    Jul 2003
    Posts
    49
    The Program still gives a missing encrypted/decrypted file !!
    It ain't illegal until you get caught..

  3. #18
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    I am using the original test case:

    This program is desinged by: Amy
    This program is to encrypt/decrypt a certain file from the drive to another file on the drive as well
    using the original password: amy and still getting a similar result as in your original post. No problem though, closing in on a solution.
    Last edited by The Brain; 05-22-2005 at 11:40 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  4. #19
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    I'm not sure if you're familiar with the new and delete operators, but if it's easier (and allows you to avoid the loop), then what you could do is get the size of the file, create a char array using new for the exact size, and read it in in one fell swoop. Something like this:

    Code:
    std::fstream iFile(filename.c_str(), std::ios::in | std::ios::binary | std::ios::ate);
    	int size = iFile.tellg(); // Get the size of the file.
    	iFile.seekg(0, std::ios::beg); // Seek to the beginning of the file.
    				
    	char* temp = new char[size]; // Create an array of the size of the file.
    				
    	iFile.read(temp, size); // Read in our data to the array.
    iFile.close();

  5. #20
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    This is what I got so far.. it's giving me better results.. I can actually get this far after the second xor:

    This program is desinged by: Amy
    This program is to encrypt/decrypt a

    Code:
    	int j=0;
    	char file_array[32], enc_dec_array[32];
    	while(!source.eof())
    	{
     
    		// START PUTTING PART OF THE FILE INTO AN ARRAY CORRESPONDING THE CODE'S ARRAY ===========
     
    		
    		j=0;
    		while(!source.eof() && j<codeNO)
    		{
    			source.get(c);
    			file_array[j] = c;			
    			//At this point, you can test for eof and influence codeNO's resulting value
    			if(source.eof())
    				codeNO = j;
    			j++;
    		}
    
    // END PUTTING PART OF THE FILE INTO AN ARRAY CORRESPONDING THE CODE'S ARRAY =============
    // ************************************************
    // START OF PUTTING THE FILE ARRAY IN AN ENCRYPTED/DECRYPTED FORM IN ANOTHER ARRAY ==========
    
    		
    		for(int k=0; k<codeNO; k++)
    		{
    			enc_dec_array[k] = file_array[k] ^ code_array[k];
    			target.put(enc_dec_array[k]);
    		}	
    
    		// END OF PUTTING THE FILE ARRAY IN AN ENCRYPTED/DECRYPTED FORM IN ANOTHER ARRAY ============
    	}
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  6. #21
    Amy amirahasanen1's Avatar
    Join Date
    Jul 2003
    Posts
    49
    Quote Originally Posted by Lithorien
    I'm not sure if you're familiar with the new and delete operators, but if it's easier (and allows you to avoid the loop), then what you could do is get the size of the file, create a char array using new for the exact size, and read it in in one fell swoop. Something like this:

    Code:
    std::fstream iFile(filename.c_str(), std::ios::in | std::ios::binary | std::ios::ate);
    	int size = iFile.tellg(); // Get the size of the file.
    	iFile.seekg(0, std::ios::beg); // Seek to the beginning of the file.
     
    	char* temp = new char[size]; // Create an array of the size of the file.
     
    	iFile.read(temp, size); // Read in our data to the array.
    iFile.close();
    Well, I do not think I can use this way, because i Am asked to stick to a certain way; given by the prompt, here is the original prompt of the program:


    C++ provides three bitwise (bit-by-bit) operators (bitwise AND (&), bitwise OR ( | ) and the bitwise XOR ( ^ ).

    A function is to be implemented to receive the character (C) and the encryption byte (B) and return the encrypted character (D). Notice that this function is also used for decryption. Also, we want another function to encrypt/decrypt a “block” of characters of a given length stored in an array x[ ] using an encryption block of characters representing the secret “key” with the same length and to return the encrypted/decrypted block y[ ]. The key length should not exceed 32 characters.

    We want to use the above functions in a program to receive a file of characters from the HD and to encrypt its characters to form another file using your secret key. The program will also recover the original file using the same secret key.

    Notice that the file size might not be an integral multiple of the chosen key length. In this case, the last block on the file will be encrypted/decrypted using only part of the key.
    It ain't illegal until you get caught..

  7. #22
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Looking at that prompt, I can tell you that you'd be allowed to use new and delete. They didn't restrict how large your x[] array could be, only how large the KEY could be. And the x[] array is what I was talking about using new and delete for.



    Another hint I can give you is that you can do the encryption and decryption in one for() loop. Just start at 0, go to strlen() of your buffer, and keep a counter for your key array. I'll psuedo-code this, but this should also help you.

    Code:
    for (int x = 0, y = 0; x < strlen(crypt_buffer[]); x++)
    {
        //XOR crypt_buffer[x] against key[y]..
    
        // If y's value is equal to the last element in key[], set y to 0 and continue..
    }


    Hope this can help a little.

  8. #23
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    I've been able to determine that the target file is being written to successfully with the entire xor'd code.. however, when it comes time to read from the encrypted file, I think it's detecting eof prematurely for some reason
    Last edited by The Brain; 05-22-2005 at 01:07 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  9. #24
    Amy amirahasanen1's Avatar
    Join Date
    Jul 2003
    Posts
    49
    [QUOTE=Lithorien]Looking at that prompt, I can tell you that you'd be allowed to use new and delete. They didn't restrict how large your x[] array could be, only how large the KEY could be.[/code]

    Well they did not restrict it in the prompt, but my instructor restricted that by telling us in class
    But thanks a lot this helped me much for my personal knowledge..

    And The Brain, Yes.. I don't know why the error comes in this part !
    It ain't illegal until you get caught..

  10. #25
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    After I stopped being bullheaded and took hk_mp5kpdw's advice, you got a working program:

    Code:
    #include<iostream>
    #include<string>
    #include<fstream> 
    using namespace std;
    int main()
    {
    	char c;
    	ifstream source;
    	ofstream target;
    	string source_fname;
    	string target_fname;
     
    	cout<<"Please enter the path of the file to be encrypted/decrypted: "<<endl;
    	cin>>source_fname;
    	source.open(source_fname.c_str(),ios::binary);
    	cout<<endl;
    	cout<<"Please enter the path and name of the new file, that the first file is to be encrypted/decrypted to: "<<endl;
    	cin>>target_fname;
    	target.open(target_fname.c_str(),ios::trunc|ios::binary);
     
     
    	// START CODE INPUT INTO AN ARRAY =============================
    	string code;
    	cout<<"\n\nPlease input a code/password of not greater than 32 characters (including no spaces/blanks),";
    	cout<<"[Note: if a space is entered the program will consider the code/password to be only what before the";
    	cout<<"space/blank]: "<<endl;
    	cin>>code;
     
    	int codeNO; // number of characters of the code
    	codeNO = code.length();
     
    	while(codeNO>32)
    	{
    		cout<<"The code entered is greater than 32 characters, please reenter a new code: ";
    		getline(cin,code);
    		codeNO = code.length();
    	}
    	
    	char code_array[32];	
    	for(int i=0; i<codeNO; i++)
    	
    		code_array[i] = code[i];
    	
    	// END CODE INPUT INTO AN ARRAY ==================================
     
    	int j=0;
    	char file_array[32], enc_dec_array[32];
    	while(!source.eof())
    	{
     
    		// START PUTTING PART OF THE FILE INTO AN ARRAY CORRESPONDING THE CODE'S ARRAY ===========
     
    		
    		j=0;
    		while(!source.eof() && j<codeNO)
    		{
    			source.get(c);
    			file_array[j] = c;			
    			//At this point, you can test for eof and influence codeNO's resulting value
    			if(source.eof())
    				codeNO = j;
    			j++;
    		}
    
    // END PUTTING PART OF THE FILE INTO AN ARRAY CORRESPONDING THE CODE'S ARRAY =============
    // ************************************************
    // START OF PUTTING THE FILE ARRAY IN AN ENCRYPTED/DECRYPTED FORM IN ANOTHER ARRAY ==========
    
    		
    		for(int k=0; k<codeNO; k++)
    		{
    			enc_dec_array[k] = file_array[k] ^ code_array[k];
    			target.put(enc_dec_array[k]);
    		}	
    
    		// END OF PUTTING THE FILE ARRAY IN AN ENCRYPTED/DECRYPTED FORM IN ANOTHER ARRAY ============
    	}
     
    return 0;
    }


    Now if hk_mp5kpdw would like to elaborate on why this needed to be in binary.. I would be willing to listen and learn.
    Last edited by The Brain; 05-22-2005 at 02:09 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  11. #26
    Amy amirahasanen1's Avatar
    Join Date
    Jul 2003
    Posts
    49
    Oh, Thanks a lot for that Brain
    It ain't illegal until you get caught..

  12. #27
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    The reason it needed to be in binary is because, when you XOR something, the results of the XOR might be a non-ASCII character. This equals NULL in ascii, and when you detect eof(), that 0 will screw you up. I think, anyway.

  13. #28
    Amy amirahasanen1's Avatar
    Join Date
    Jul 2003
    Posts
    49
    Ahh, I got it, thanks a lot, Lithorien..
    It ain't illegal until you get caught..

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Create Copies of Files
    By Kanshu in forum C++ Programming
    Replies: 13
    Last Post: 05-09-2009, 08:53 AM
  2. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  3. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM
  4. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 09:44 AM
  5. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 04:30 PM

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