Thread: XOR Encryption

  1. #16
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    Quote Originally Posted by prog-bman View Post
    Is your goal to xor everything in the file including newlines? Or do you just want to xor the human readable items?
    Yes, I think. I just want it to be able to read the entire file, regardless of how many newlines there are, and encrypt it. I think that's what you ment.

  2. #17
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Example how to do so. Looking at you xor that needs some work. Its a simple process don't overcomplicate it by trying to use syntax tricks.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    int main()
    {
    	//This will hold the files contents
    	char *fileContents = NULL;
    
    	std::ifstream file("file.txt", std::ios::binary);
    
    	//Could not open the file, break out
    	if(!file){
    		return 0;
    	}//if
    
    	//Seek to the end of the file
    	file.seekg(0, std::ios::end);
    
    	//Grab the get pointers location, this will be how many items the
    	//file contains
    	size_t length = file.tellg();
    
    	//Seek back to the beginning of the file
    	file.seekg(0, std::ios::beg);
    
    	//Allocate the char to the number of characters in the file
    	fileContents = new char[length];
    
    	//Read the whole file in
    	file.read(fileContents, length);
    
    	//Close the file out
    	file.close();
    
    	//Loop through all the characters int the file
    	for(size_t i = 0; i < length; i++){
    		char c = fileContents[i];
    
    		//Encryption here 
    	}//for
    
    	//Make sure to clean up
    	delete [] fileContents;
    
    	return 0;
    }

  3. #18
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    Quote Originally Posted by prog-bman View Post
    Code:
    char c = fileContents[i];
    What does "c" do?

  4. #19
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    c would be the character I would xor encrypt if I was doing it

  5. #20
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    So I'm assuming I'm supplying the encrypting - just asking. BTW, it cannot find the file "test.txt". It's sitting write next to it in the folder-I even supplied the file path. It still is saying that it can't find it. What's wrong?

  6. #21
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    Ummm... it's not working- it just can't find the file!

  7. #22
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    That was just an example file. I know sometimes if you name a file it can actually end up being this testing.txt.txt. Therefore the program can't find it.

  8. #23
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    Quote Originally Posted by prog-bman View Post
    That was just an example file. I know sometimes if you name a file it can actually end up being this testing.txt.txt. Therefore the program can't find it.
    I'm not typing in the name of the file, I just left it at "test.txt" - it still can't find it.

  9. #24
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    You can use the same structure as you had before for opening the file. That was just example code to show you how to read in a whole file and process each character for your encryption.

  10. #25
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    Ok I'm probably starting to sound real stupid now, but am I supposed to encrypt "char c" or "fileContents", and whichever I use, do I use the encryption method from my previous posts? Sorry if I'm getting annoying...

  11. #26
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    http://www.cprogramming.com/tutorial/xor.html

    c would be the character to encrypt.

    You will need some place to hold your encrypted characters. Could be a char * or a std::string. That I leave up to you.

    Code:
    char c = fileContents[i];
    
    c = c ^ key
    
    buffer[i] = c;
    You could also just xor the contents of fileContents as well. Same result different method

    Code:
    fileContents[i] = fileContents[i] ^ key
    Last edited by prog-bman; 08-08-2007 at 08:15 PM.

  12. #27
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    Does anyone know how to convert a character array to a string? I have to following but it doesn't work:

    Code:
     for (int i = 0; i < x; i++)
            {
                out = value[i];
            }
    Out being the string, value being the character array, and x being the size of the character array.

  13. #28
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    A null terminated character array is a C style string.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #29
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    A null terminated character array is a C style string.
    I meant a std::string, not C style string.

  15. #30
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    std::string has a constructor that takes a null terminated C style string as its argument. If your char array is not null terminated, use the constructor that takes a range.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed