XOR Encryption

This is a discussion on XOR Encryption within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by prog-bman Is your goal to xor everything in the file including newlines? Or do you just want ...

  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,801
    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,801
    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,801
    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,801
    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,801
    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
    21,650
    A null terminated character array is a C style string.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,650
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 2 of 4 FirstFirst 1234 LastLast
Popular pages Recent additions subscribe to a feed

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