954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

EOF problem

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

char d;
	ifstream myfile1 ("myfile.txt");
	ofstream myfile3 ("Ciphertext.txt");
	
	myfile1.get(d);  //priming read

  while(!myfile1.eof())
{
	  for ( j=0; j<BC; j++)
	  for ( i=0; i < 4; i++)        
	  {
		  a[i][j] = d; // plaintext
		  //cout<<a[i][j]; // to chk how many plaintext is copied
		  myfile1.get(d);  //reads first element of next block
	  } 

		  Encrypt(a, rk);
		  counter1();
		
	  if (myfile3.is_open())
	  {
		  for(j=0; j< BC; j ++)
			  for ( i=0; i<4; i++)
				  myfile3<<a[i][j];
	  }   
	  else        
		cout << "Unable to open file";
  }	 
   	myfile3.close();
	myfile1.close();


Code to open Ciphertext.txt and save decrypted data to text.txt file

char buffer[4][4];
	char dxc;
	ifstream myfile4 ("Ciphertext.txt");
	ofstream myfile5 ("Text.txt");

	myfile4.get(dxc);  //priming read

  while(!myfile4.eof())
  {
	  for ( j=0; j<BC; j++)
	  for ( i=0; i < 4; i++)        
	  {
		  buffer[i][j]=dxc;
		  //a[i][j] = dxc; // plaintext
		  //cout<<a[i][j]; // to chk how many plaintext is copied
		  myfile4.get(dxc);  //reads first element of next block
	  }

	  for ( j=0; j<BC; j++)
	  for ( i=0; i < 4; i++)        
	  {
		  a[i][j] = buffer[i][j];

	  }
		  Decrypt(a, rk);
		  counter2();

		if (myfile5.is_open())
		{
		    for(j=0; j<BC; j ++)
			for ( i=0; i<4; i++)
			myfile5<<a[i][j];
		}   
		  else        
			  cout << "Unable to open file";
  }			 
  	myfile5.close();
	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...

kartouss
Junior Poster in Training
80 posts since Jan 2008
Reputation Points: 12
Solved Threads: 0
 

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 :D

FireNet
Posting Whiz in Training
258 posts since May 2004
Reputation Points: 108
Solved Threads: 7
 

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.

#include<iostream>
#include <fstream>
using namespace std;

int main( )
{
   fstream f;
   unsigned char buff[16];
   int i, j;

   f.open( "ciphertext.txt", ios::binary | ios::in );
   if( !f )
   {
      cout << "error opening file, quitting" << endl;
      return -1;
   }


   for( j = 0; j < 10; j++ ) //we just want to see a sample of the file
   {
      f.read( (char *)buff, 16 );
      for( i = 0; i < 16; i++ )
         cout << int ( buff[i] ) << " " ; //display ASCII values
      cout << endl;
   }


   return 0;
}

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:

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.

vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
 

Various subtleties, but 'EOF character' is not a good idiom to maintain.
http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1048865140&id=1043284351

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 
Various subtleties, but 'EOF character' is not a good idiom to maintain. http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1048865140&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.

vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
 

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.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

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.

vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
 
P.S Refer to my reply to your mail.
As I'd said privately....


No one should be answering private requests for help. That's because no one should beasking for private help. Help is what these forums are for. Keep all questions and answers in the forums, please.

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.


AndDave 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.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 
char d;
	ifstream myfile1 ("myfile.txt");
	ofstream myfile3  ("ciphertext.txt");

	myfile1.get(d);  //priming read

  while(!myfile1.eof())
{
	  for ( j=0; j<BC; j++)
	  for ( i=0; i < 4; i++)        
	  {
		  a[i][j] = d; // plaintext
		  //cout<<a[i][j]; // to chk how many plaintext is copied
		  myfile1.get(d);  //reads first element of next block
	 }
		  Encrypt(a, rk);
		  counter1();
	
	  if (myfile3.is_open())
	  {
		  for(j=0; j< BC; j ++)
			  for ( i=0; i<4; i++)
				  myfile3<<a[i][j];
	  }   
	  else        
		cout << "Unable to open file";
  }  
   	myfile3.close();
	myfile1.close();
	  
	unsigned char buff[4][4];
	fstream myfile4;
	myfile4.open( "ciphertext.txt", ios::binary | ios::in );
	if( !myfile4 )
	{
      cout << "error opening file, quitting" << endl;
      return -1;
	}
	ofstream myfile5 ("Text.txt");	
 
	  while(myfile4.read( (char *)buff, 16 ))
	  {
                    for( i = 0; i < 4; i++ )
		  for( j = 0; j < 4; j++ )
                               buff[i][j];

	  for ( j=0; j<BC; j++)
	  for ( i=0; i < 4; i++)        
	  {
		  a[i][j] = buff[i][j];

	  }	
		  Decrypt(a, rk);
		  counter2();

	if (myfile5.is_open())
		{
		    for(j=0; j<BC; j ++)
			for ( i=0; i<4; i++)
			myfile5<<a[i][j];
		}   
		  else        
			  cout << "Unable to open file";
  }		 
  	myfile5.close();
	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...

kartouss
Junior Poster in Training
80 posts since Jan 2008
Reputation Points: 12
Solved Threads: 0
 

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.

for ( j=0; j<BC; j++)
     for ( i=0; i < 4; i++)
    {
          a[i][j] = buff[j][i];
     }
vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
 

Hello,
I am forwarding the codes in a zip file...I tried to debug the code in the main function so as to open the file ciphertext.txt in binary but still i am getting error messages...
The file cannot be opened...

error opening file, quitting

Please help me out how to solve this problem....

Attachments AES.zip (115.56KB)
kartouss
Junior Poster in Training
80 posts since Jan 2008
Reputation Points: 12
Solved Threads: 0
 

Zoinks! The ugly stepchild C/C++ nether-language circa 1995!

When I make necessary tweaks to compile, I got this.
128

--- Advanced Encryption Standard ---


Enter the length of Key(128, 192 or 256 only):

*********** AES Encryption using 128 bits **********

Key : 1111111111111111

----- AES Encryption Time for 1 Operation -----

Key Expansion Time : 0.000010 seconds.

AddRoundKey Time : 0.103023 seconds.

SubBytes Time : 0.083792 seconds.

ShiftRow Time : 0.097528 seconds.

MixColunms Time : 0.135990 seconds.

Encrytion Time : 0.420333 seconds.

Number of plaintext blocks of 16 bytes created: 4917

*********** AES Decryption using 128 bits **********

Key : 1111111111111111

----- Average AES Decryption Time for 1 Operation -----

Key Expansion Time : 0.000010 seconds.

AddRoundKey Time : 0.093787 seconds.

Inverse SubBytes Time : 0.096546 seconds.

Inverse ShiftRow Time : 0.096546 seconds.

Inverse MixColunms Time : 0.173782 seconds.

Decrytion Time : 0.460661 seconds.

Number of plaintext blocks of 16 bytes created: 4937


End of the program

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

Which software are you using to test the codes...
I am using visual c++ 6.0 on windows Xp..
Still getting error messages....
Have u made any modifications in the code... to run it..

kartouss
Junior Poster in Training
80 posts since Jan 2008
Reputation Points: 12
Solved Threads: 0
 

I was minimally invasive, choosing merely to #include instead of the nonstandard #include. And I looked the other way on a few warnings.

I built with Code::Blocks' gcc. MSVC6 is one to ditch when you can find anything better, and you can. For free. Very easily. Even to another MS product. I'd recommend doing so with no reservations.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

Thanks Dave Sinkula... Thats works...
But now i have my text decrypted in binary, I want the files plaintext.txt and text.txt the same
That is how to convert the binary encrypted text in char....
Also the program counts the number of 16 bytes block being created for encryption and decryption...But the number of encrypted and decrypted blocks created is not the same..
The number of decrypted blocks is more than the encrypted one for 128/192/256 keys..
Can anyone help me out...

kartouss
Junior Poster in Training
80 posts since Jan 2008
Reputation Points: 12
Solved Threads: 0
 

Plaintext or ciphertext, open both as binary and process them thusly.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

Yes, i have opened both as binary for encryption and decryption

fstream myfile1;
	myfile1.open( "plaintext.txt", ios::binary | ios::in );
fstream myfile4;
	myfile4.open( "ciphertext.txt", ios::binary | ios::in );


Still the files plaintext.txt and cipheretxt.txt not the same...

kartouss
Junior Poster in Training
80 posts since Jan 2008
Reputation Points: 12
Solved Threads: 0
 

Well I haven't really been digging too deep, but I'm just not a fan of the eof() loop control -- as I'm sure you may have gathered. I prefer to instead look for a successful read before continuing with data that may or may not have been read. Maybe such an approach can be fruitful.

Another thing I see in glancing is the input of a plain char . When reading a bag of bytes as bytes, I always prefer unsigned char . This may not be a magic bullet either.

Perhaps later on I may have time to dwell if you haven't killed your bugs yet.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

Hello i think the data has been corrupted that is why I am getting the text in encrypted form now... How can i solve this problem.... I have open the file in binary for decryption...
Is there another way to solve this problem instead of using files...

kartouss
Junior Poster in Training
80 posts since Jan 2008
Reputation Points: 12
Solved Threads: 0
 

Ok so 1 method to solve this is to skip or delete the EOF character when reading the plaintext.txt before encryption...
How can this be done...
Please help me out how we can solve this problem...

kartouss
Junior Poster in Training
80 posts since Jan 2008
Reputation Points: 12
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You