943,608 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 8639
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Feb 25th, 2008
0

EOF problem

Expand Post »
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
c++ Syntax (Toggle Plain Text)
  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
c++ Syntax (Toggle Plain Text)
  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...
Similar Threads
Reputation Points: 12
Solved Threads: 0
Junior Poster in Training
kartouss is offline Offline
80 posts
since Jan 2008
Feb 25th, 2008
0

Re: EOF problem

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
Reputation Points: 108
Solved Threads: 7
Posting Whiz in Training
FireNet is offline Offline
256 posts
since May 2004
Feb 25th, 2008
0

Re: EOF problem

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.
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Feb 25th, 2008
0

Re: EOF problem

Various subtleties, but 'EOF character' is not a good idiom to maintain.
http://faq.cprogramming.com/cgi-bin/...&id=1043284351
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Feb 25th, 2008
0

Re: EOF problem

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.
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Feb 25th, 2008
0

Re: EOF problem

Click to Expand / Collapse  Quote originally posted by vmanes ...
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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Feb 25th, 2008
0

Re: EOF problem

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.
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Feb 25th, 2008
0

Re: EOF problem

Click to Expand / Collapse  Quote originally posted by FireNet ...
P.S Refer to my reply to your mail.
Click to Expand / Collapse  Quote originally posted by vmanes ...
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.

Click to Expand / Collapse  Quote originally posted by vmanes ...
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.
Moderator
Reputation Points: 3278
Solved Threads: 890
Posting Sage
WaltP is offline Offline
7,717 posts
since May 2006
Feb 26th, 2008
0

Re: EOF problem

c++ Syntax (Toggle Plain Text)
  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...
Reputation Points: 12
Solved Threads: 0
Junior Poster in Training
kartouss is offline Offline
80 posts
since Jan 2008
Feb 26th, 2008
0

Re: EOF problem

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.
C++ Syntax (Toggle Plain Text)
  1. for ( j=0; j<BC; j++)
  2. for ( i=0; i < 4; i++)
  3. {
  4. a[i][j] = buff[j][i];
  5. }
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Inheritance and composition
Next Thread in C++ Forum Timeline: Composition problem "'XXX' is not a base or member"





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC