EOF problem

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jan 2008
Posts: 80
Reputation: kartouss is an unknown quantity at this point 
Solved Threads: 0
kartouss's Avatar
kartouss kartouss is offline Offline
Junior Poster in Training

EOF problem

 
0
  #1
Feb 25th, 2008
Hello i am using AES to encrypt/decrypt data/ pixel values.. saved in a file myfile.txt and the AES encrypts tha data and writes the ciphertext in a file ciphertext.txt
When decrypting the data i am getting some of the pixel values decrypted correctly followed by garbage..

Code to open myfile.txt and save ciphertext to Ciphertext.txt file
  1. char d;
  2. ifstream myfile1 ("myfile.txt");
  3. ofstream myfile3 ("Ciphertext.txt");
  4.  
  5. myfile1.get(d); //priming read
  6.  
  7. while(!myfile1.eof())
  8. {
  9. for ( j=0; j<BC; j++)
  10. for ( i=0; i < 4; i++)
  11. {
  12. a[i][j] = d; // plaintext
  13. //cout<<a[i][j]; // to chk how many plaintext is copied
  14. myfile1.get(d); //reads first element of next block
  15. }
  16.  
  17. Encrypt(a, rk);
  18. counter1();
  19.  
  20. if (myfile3.is_open())
  21. {
  22. for(j=0; j< BC; j ++)
  23. for ( i=0; i<4; i++)
  24. myfile3<<a[i][j];
  25. }
  26. else
  27. cout << "Unable to open file";
  28. }
  29. myfile3.close();
  30. myfile1.close();

Code to open Ciphertext.txt and save decrypted data to text.txt file
  1. char buffer[4][4];
  2. char dxc;
  3. ifstream myfile4 ("Ciphertext.txt");
  4. ofstream myfile5 ("Text.txt");
  5.  
  6. myfile4.get(dxc); //priming read
  7.  
  8. while(!myfile4.eof())
  9. {
  10. for ( j=0; j<BC; j++)
  11. for ( i=0; i < 4; i++)
  12. {
  13. buffer[i][j]=dxc;
  14. //a[i][j] = dxc; // plaintext
  15. //cout<<a[i][j]; // to chk how many plaintext is copied
  16. myfile4.get(dxc); //reads first element of next block
  17. }
  18.  
  19. for ( j=0; j<BC; j++)
  20. for ( i=0; i < 4; i++)
  21. {
  22. a[i][j] = buffer[i][j];
  23.  
  24. }
  25. Decrypt(a, rk);
  26. counter2();
  27.  
  28. if (myfile5.is_open())
  29. {
  30. for(j=0; j<BC; j ++)
  31. for ( i=0; i<4; i++)
  32. myfile5<<a[i][j];
  33. }
  34. else
  35. cout << "Unable to open file";
  36. }
  37. myfile5.close();
  38. myfile4.close();

E.g:- pixel values im myfile.txt
-13224404 -14211046 -14605042 -13682410 -12298975 -11245015 -10322644 -9532112 -8609993 -7490750 -6240432 -5516455 -4990625 -4332695 -3675534 -3412107 -3082376 -2753413 -2687618 -2884997 -3082374 -3082628 -3345799 -3740555 -3675016 -3675013 -3675267 -3609474 -3412093 -3214714 -3017333 -2885749 -2557300 -2425716 -2622326 -2885500 -3082625 -3345797 -4069263 -4727191 -5319066 -6108833 -6766499 -7424678 -8609203 -9333176 -9925051 -10714820 -11240648 -11503818 -11635406 -11437774 -11437776 -11634903 -11503318 -11239892 -11503066 -11503066 -11503064 -11503318 -11503317 -11503317 -11503315 -11503315 -11109329 -11109329 -10978000 -10978000 -10912461 -10912461 -10715850 -10715850 -10847949 -10847949 -10782667 -10848460 -10914252 -10914252 -10849229 -10849227 -10782918 -10782916 -10848709 -10914502 -11046088 -11111881 -11177674 -11177674 -11506639 -11506639 -11506639 -11506639 -11440846 -11440846 -11440846 -11440846
-13158611 -14276582 -14670578 -13682410 -12364768 -11245015

Pixel values in text.txt
-13224404 -14211046 -14605042 -13682410 -12298975 -11245015 -10322644 -9532112 \Ó¶¬6Ewƒ›SÆû53yè

However, some of the encrypted text is the same as EOF and thus i cant read the text
that after it. What should i do?
How to remove the EOF character from the encrypted text...
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 256
Reputation: FireNet will become famous soon enough FireNet will become famous soon enough 
Solved Threads: 6
FireNet's Avatar
FireNet FireNet is offline Offline
Posting Whiz in Training

Re: EOF problem

 
0
  #2
Feb 25th, 2008
Use binary mode to open files which deal with encrypted text.

That should solve the problem with the EOF char

P.S Refer to my reply to your mail.

If you get it working post the working program here
See what you can, remember what you need

Fourzon | Earn via Coding
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,673
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: EOF problem

 
0
  #3
Feb 25th, 2008
As I'd said privately, you don't want to change the EOF char in your ciphertext - that will corrupt the data.

In order to read in the file, without the EOF character halting the read, do so in a binary fashion. Grab 16 bytes, then place them in your 4x4 array. Life will be good.

Here's a sample of how to read in binary mode.
  1. #include<iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5. int main( )
  6. {
  7. fstream f;
  8. unsigned char buff[16];
  9. int i, j;
  10.  
  11. f.open( "ciphertext.txt", ios::binary | ios::in );
  12. if( !f )
  13. {
  14. cout << "error opening file, quitting" << endl;
  15. return -1;
  16. }
  17.  
  18.  
  19. for( j = 0; j < 10; j++ ) //we just want to see a sample of the file
  20. {
  21. f.read( (char *)buff, 16 );
  22. for( i = 0; i < 16; i++ )
  23. cout << int ( buff[i] ) << " " ; //display ASCII values
  24. cout << endl;
  25. }
  26.  
  27.  
  28. return 0;
  29. }
This will give you a dump of the ASCII values for the 160 bytes, and that should be enough for you to get past the at least the first occurrence EOF (decimal 26) character.

For reading the full file, control the loop as:
  1. while( f.read( (char *)buff, 16 ) )
For this to work right, you must ensure your encoding process creates a file that is a multiple of 16 bytes in size.
"We Americans got so tired of being thought of as dumb by the rest of the world that we went to the polls last November and removed all doubt."
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,333
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 234
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: EOF problem

 
0
  #4
Feb 25th, 2008
Various subtleties, but 'EOF character' is not a good idiom to maintain.
http://faq.cprogramming.com/cgi-bin/...&id=1043284351
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,673
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: EOF problem

 
0
  #5
Feb 25th, 2008
Originally Posted by Dave Sinkula View Post
Various subtleties, but 'EOF character' is not a good idiom to maintain.
http://faq.cprogramming.com/cgi-bin/...&id=1043284351
Using EOF to find end of file is not what this thread is about. It's NOT about reading till EOF char is encountered, but how to get past the EOF character.

Contrary to your reference, there is an EOF character (check your neighborhood ASCII chart) and reading it in text mode will cause end of input. The OP has file of encrypted data which may happen to include the value that equates to the EOF character, and that's what's causing problems.
"We Americans got so tired of being thought of as dumb by the rest of the world that we went to the polls last November and removed all doubt."
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,333
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 234
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: EOF problem

 
0
  #6
Feb 25th, 2008
Originally Posted by vmanes View Post
Using EOF to find end of file is not what this thread is about. It's NOT about reading till EOF char is encountered, but how to get past the EOF character.

Contrary to your reference, there is an EOF character (check your neighborhood ASCII chart) and reading it in text mode will cause end of input. The OP has file of encrypted data which may happen to include the value that equates to the EOF character, and that's what's causing problems.
File systems vary, there is absolutely no need for a file to contain an 'EOF character'. On a common system, a particular character in a file opened in text mode might be translated into a value that causes the eof signal to be raised.

This is not because an 'EOF character' exists, it is because a binary file is being examined in text mode. Blurring such subtleties by repeating 'EOF character' to me is a bit of a disservice.

If the file is opened in the correct mode for the input, then there is no need to watch out for things that aren't there.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,673
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: EOF problem

 
0
  #7
Feb 25th, 2008
Not disputing what you say, Dave.

It's a matter of what the OP's problem is here, and how I (and at least one other) have been trying to help him around it.

In short, he read in a plain text file, encrypts it, stores it back - a collection of bytes in the range 0-255.

He (attempted) to open the encrypted file, as a text file, and read it back for decryption. This fails when a byte valued 26 (decimal) is encountered. We're trying to get the point across that, at least for reading the ciphertext, he must do so in binary mode. The resulting plaintext can be written back in text mode, assuming correct decryption.
"We Americans got so tired of being thought of as dumb by the rest of the world that we went to the polls last November and removed all doubt."
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: EOF problem

 
0
  #8
Feb 25th, 2008
Originally Posted by FireNet View Post
P.S Refer to my reply to your mail.
Originally Posted by vmanes View Post
As I'd said privately....
No one should be answering private requests for help. That's because no one should be asking for private help. Help is what these forums are for. Keep all questions and answers in the forums, please.

Originally Posted by vmanes View Post
Not disputing what you say, Dave.

It's a matter of what the OP's problem is here, and how I (and at least one other) have been trying to help him around it.
And Dave is trying to clear up misinformation being passed to the OP. Though not directly related to his problem, telling him there's an EOF character is teaching him 'bad grammar' which he will pass on to the next person. So don't argue with Dave. He's trying to help, too.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 80
Reputation: kartouss is an unknown quantity at this point 
Solved Threads: 0
kartouss's Avatar
kartouss kartouss is offline Offline
Junior Poster in Training

Re: EOF problem

 
0
  #9
Feb 26th, 2008
  1. char d;
  2. ifstream myfile1 ("myfile.txt");
  3. ofstream myfile3 ("ciphertext.txt");
  4.  
  5. myfile1.get(d); //priming read
  6.  
  7. while(!myfile1.eof())
  8. {
  9. for ( j=0; j<BC; j++)
  10. for ( i=0; i < 4; i++)
  11. {
  12. a[i][j] = d; // plaintext
  13. //cout<<a[i][j]; // to chk how many plaintext is copied
  14. myfile1.get(d); //reads first element of next block
  15. }
  16. Encrypt(a, rk);
  17. counter1();
  18.  
  19. if (myfile3.is_open())
  20. {
  21. for(j=0; j< BC; j ++)
  22. for ( i=0; i<4; i++)
  23. myfile3<<a[i][j];
  24. }
  25. else
  26. cout << "Unable to open file";
  27. }
  28. myfile3.close();
  29. myfile1.close();
  30.  
  31. unsigned char buff[4][4];
  32. fstream myfile4;
  33. myfile4.open( "ciphertext.txt", ios::binary | ios::in );
  34. if( !myfile4 )
  35. {
  36. cout << "error opening file, quitting" << endl;
  37. return -1;
  38. }
  39. ofstream myfile5 ("Text.txt");
  40.  
  41. while(myfile4.read( (char *)buff, 16 ))
  42. {
  43. for( i = 0; i < 4; i++ )
  44. for( j = 0; j < 4; j++ )
  45. buff[i][j];
  46.  
  47. for ( j=0; j<BC; j++)
  48. for ( i=0; i < 4; i++)
  49. {
  50. a[i][j] = buff[i][j];
  51.  
  52. }
  53. Decrypt(a, rk);
  54. counter2();
  55.  
  56. if (myfile5.is_open())
  57. {
  58. for(j=0; j<BC; j ++)
  59. for ( i=0; i<4; i++)
  60. myfile5<<a[i][j];
  61. }
  62. else
  63. cout << "Unable to open file";
  64. }
  65. myfile5.close();
  66. myfile4.close();

Hello i am getting an error message, file cannot be open, quitting...
The code that Vmanes sent in the prevoius post is working in the main function and retrieving in binary...
I have modified the code in the aes main program and its not opening the file cipheretxt.txt to read .... please help me out how to solve this poblem...
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,673
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: EOF problem

 
0
  #10
Feb 26th, 2008
From the code given, I don't see any reason the file open action should be failing. Can you step through the program and after the point at which ciphertext.txt should have been written, verify that it in fact exists?

Once it does open, I think you'll find the decryption will not work right. In all your 4x4 array accesses, you've done them in column major fashion, that is, working down a column, then move to the next column. Reading directly into the the buff array will store in a row major fashion, the natural way that C/C++ work. Your code in lines 43-46 does nothing.

To store from the buff to array a, in the order in which you've previously worked, you need to copy rows to columns.
  1. for ( j=0; j<BC; j++)
  2. for ( i=0; i < 4; i++)
  3. {
  4. a[i][j] = buff[j][i];
  5. }
"We Americans got so tired of being thought of as dumb by the rest of the world that we went to the polls last November and removed all doubt."
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC