XOR ---> text file save --> Some of text disappears after Decode

This is a discussion on XOR ---> text file save --> Some of text disappears after Decode within the C++ Programming forums, part of the General Programming Boards category; Ok, heres my problem, when I encrypt a basic string like hello world, and with a key like thisistheadminpass42$@67^&90(), and ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    28

    XOR ---> text file save --> Some of text disappears after Decode

    Ok, heres my problem, when I encrypt a basic string like hello world, and with a key like thisistheadminpass42$@67^&90(), and then decrypt, some of the text will just disappear.

    Is this because a .txt file can't save certain Ascii characters?

    TIA

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    How are you opening and reading from the file? You need to be using binary mode for reading/writing such data.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    xhi
    xhi is offline
    !anExpert
    Join Date
    Mar 2005
    Location
    pa
    Posts
    155
    Quote Originally Posted by hk_mp5kpdw
    How are you opening and reading from the file? You need to be using binary mode for reading/writing such data.
    just to tack on a bit here..
    youll also have to use the functions ofstream::write and ifstream::read for reading and writing binary data..
    Last edited by xhi; 01-02-2006 at 10:54 PM.

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    yeah, you'd either need to use binary reading or writing, or you could try using text, but you'd need to make sure that everything can be read back from teh file correctly. for example, if one of your letters turns into a newline '\n' it'll throw off your entire file.

    so you'd need to do something like this:
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    std::string encrypt(const std::string in);
    
    int main()
    {
    	std::string in;
    	std::fstream file("test.dat",std::ios::out|std::ios::trunc);
    
    	std::cout<<"Enter a word: ";
    	getline(std::cin,in,'\n');
    
    	/*
    	 * I'm "encrypting" the word, and outputting the result to console and
    	 * to file to read from later.
    	 */
    	in=encrypt(in);
    	std::cout<<in.c_str()<<std::endl;
    	file<<in.c_str();
    
    	/*
    	 * I'm closing and clearing the stream, then Re-Opening it for reading
    	 * in text mode
    	 */
    	file.close();
    	file.clear();
    	file.open("test.dat",std::ios::in);
    
    	/*
    	 * Now I get the result back from the file, en(de)crypt it, and then
    	 * output it to the console
    	 */
    	getline(file,in,'\n');
    	in=encrypt(in);
    	std::cout<<in<<std::endl;
    		
    	return 0;
    }
    
    std::string encrypt(const std::string in)
    {
    	std::string key="5b4s3v4rs618srge6yt7ra4vy7r8a9et7yne85r71gera5";
    	short int len;
    	register short int x;
    	register short int y;
    	std::string retval;
    
    	/*
    	 * I'm finding out the length of the string now to save processor time
    	 * later, and I'm making sure there's enough space in retval to hold
    	 * the result (which will be the same length as the string
    	 */
    	len=in.length();
    	retval.resize(len);
    
    	/*
    	 * here's the heart of the 'encryption'.  what it does is an XOR 
    	 * encryption, and if the resulting character isn't something that can
    	 * be seen on a screen (or read from a file correctly), then it moves
    	 * to the next character in the key, and re-encrypts the character in
    	 * the word.
    	 */
    	for(x=0,y=0; x<len; x++,y++)
    	{
    		retval[x]=in[x]^key[y];
    		
    		if(!isgraph(retval[x]))
    		{
    			x--;
    		}
    	}
    
    	return retval;
    }
    edit: you can use that code, but you're gonna wanna fix up the encrypt function, because I left some possible big time run-time errors in there...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  5. #5
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    krageon

    Ok, heres my problem, when I encrypt a basic string like hello world, and with a key like thisistheadminpass42$@67^&90(), and then decrypt, some of the text will just disappear.

    Is this because a .txt file can't save certain Ascii characters?

    Post your code as well please.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM

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