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

Re: EOF problem

 
0
  #31
Feb 28th, 2008
Hello,
I am able to locate the error... The code
while(myfile1.read( (char *)buff, 16 ))
is reading from the file plaintext.txt in chunks of 16 bytes... but there are remaining last 3 bytes left therefore the code is not reading the remaining 3 last bytes for encryption that is why it is not displaying the last 3 bytes when decrypted...
So therefore I need to do a padding so that it can take the last 3 bytes + 13 spaces...
Can anyone help how we can do it...
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,340
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: 236
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: EOF problem

 
0
  #32
Feb 28th, 2008
"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: 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
  #33
Mar 1st, 2008
Hello,
Instead of using another program to grab the pixel values of the image and save it in a file ciphertxt.txt and then using AES to encrypt and decrypt it.... I opened the image directly in binary...
It is encrypting and decrypting the image correctly but it is not displaying the encrypted image
How can we display the encrypted image...

  1. unsigned char buff[4][4];
  2. ifstream myfile1 ("sel.jpg", ios::binary);
  3. ofstream myfile3 ("encrypt.jpg", ios::binary);
  4.  
  5. while(myfile1.read( (char *)buff, 16 ))
  6. {
  7. for ( j=0; j<BC; j++)
  8. for ( i=0; i < 4; i++)
  9. {
  10. a[i][j] = buff[j][i]; // plaintext
  11. }
  12. Encrypt(a, rk);
  13.  
  14. if (myfile3.is_open())
  15. {
  16. for(j=0; j< BC; j ++)
  17. for ( i=0; i<4; i++)
  18. myfile3<<a[i][j];
  19. }
  20. else
  21. cout << "Unable to open file";
  22. }
  23. myfile3.close();
  24. myfile1.close();
  25.  
  26. unsigned char buffer[4][4];
  27. ifstream myfile4;
  28. myfile4.open( "encrypt.jpg", ios::binary);
  29. if( !myfile4 )
  30. {
  31. cout << "error opening file, quitting" << endl;
  32. return -1;
  33. }
  34. ofstream myfile5 ("test.jpg", ios::binary);
  35.  
  36. while(myfile4.read( (char *)buffer, 16 ))
  37. {
  38. for ( j=0; j<BC; j++)
  39. for ( i=0; i < 4; i++)
  40. {
  41. a[i][j] = buffer[j][i]; // plaintext
  42. }
  43. Decrypt(a, rk);
  44. if (myfile5.is_open())
  45. {
  46. for(j=0; j<BC; j ++)
  47. for ( i=0; i<4; i++)
  48. myfile5<<a[i][j];
  49. }
  50. else
  51. cout << "Unable to open file";
  52. }
  53.  
  54. myfile5.close();
  55. myfile4.close();

The image is correctly decrypted but i am unable to display the encrypted image...
How can we display it...
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
  #34
Mar 5th, 2008
Hello,
I am posting the code for encrypting the image as a zip file...
The code is able to encrypt/decrypt the image correctly...
But it is not able to display the encrypted image....
Can anyone help, how we can display the encrypted image...
Attached Files
File Type: zip Image Encryption.zip (132.5 KB, 6 views)
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,348
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1461
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: EOF problem

 
0
  #35
Mar 5th, 2008
>>how we can display the encrypted image
You can't, and that's the whole purpose of entrypting it -- the message has to be decrypted first before you can display it so that it makes sense.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 6
Reputation: sauron84 is an unknown quantity at this point 
Solved Threads: 1
sauron84 sauron84 is offline Offline
Newbie Poster

Re: EOF problem

 
0
  #36
Mar 6th, 2008
I think what he means is that he wants to display the ciphertext as an image. Could the ciphertxt (in binary is it?) be displayed in an image format to show whatever garbage therein?

That would help me too.

Thanks,

Sundeep
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
  #37
Mar 6th, 2008
Hello,
I am writing the code for padding....
The main idea is to get the file size..
If the size is not a multiple of 16 then the space is padded till it is a multiple of 16
The code is:-

  1. long begin, end;
  2. begin = myfile1.tellg();
  3. myfile1.seekg(0, ios::beg);
  4. end = myfile1.tellg();
  5. cout<<end;
  6.  
  7. while(((end)%16)!==0))
  8. {
  9. char pad=" ";
  10. myfile1<<pad;
  11. }

Problem i can't get the file size... it is 0..
If i change
  1. myfile1.seekg(0, ios::end);
I am getting the file size correct but then the code is not able to read from the file in chunks of 16 bytes since it is pointed @ the end of file...
Can anyone help how we can solve this problem...
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
  #38
Mar 7th, 2008
To get back to the beginning of the file for reading, after finding size:
  1. myfile1.seekg( 0, ios::beg );

Your padding code is a bit odd. Do you really want to modify the source file, or simply pad out the data that you're encrypting?
How about something like:
  1. //after reading all the data in, do padding
  2. int pad_num = 16 - (end % 16) ; //find how many to pad
  3. char pad = " ";
  4. for( i = 0; i < pad_num; i++ )
  5. {
  6. arr[r][c] = pad; //assuming you're trying to fill the array out
  7. }

Where I think this will still cause you problems is that when the encrypted file is later decrypted, how do you know that last (up to) 15 bytes might be padding and not part of the original data? To simply include those bytes in the final output might give a corrupted image.

As to displaying the encrypted image - why? You can do a dump of the file contents, byte value by byte value, but that has no meaning. You cannot use an image display program, as almost all image formats include some header data to describe the image format type and image parameters, which is now encrypted.
"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 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
  #39
Mar 7th, 2008


Displaying the image is real easy if it is not encrypted ...

You can use glut + corona to easily display images.
http://www.daniweb.com/forums/thread63827.html#5



Also if you are having problems with leftover bytes at the end of the file smaller than your buffer, use the following bit of code to find the file size

  1. // obtaining file size
  2. #include <iostream>
  3. #include <fstream>
  4. using namespace std;
  5.  
  6. int main () {
  7. long begin,end;
  8. ifstream myfile ("example.txt");
  9. begin = myfile.tellg();
  10. myfile.seekg (0, ios::end);
  11. end = myfile.tellg();
  12. myfile.close();
  13. cout << "size is: " << (end-begin) << " bytes.\n";
  14. return 0;
  15. }
http://www.cplusplus.com/doc/tutorial/files.html

Divide the file size by your buffer size, run the loop with the number you get (the quotient) and read the remaining data using the remainder as the read size.

^^


Also, it possible to display encrypted data, just that it would look like a bunch of random pixels .... The standard format is RGB, so you can create a single color out of 3 bytes of data, 0-255, standard char. Read up an OpenGL tutorial on creating a texture ......
Last edited by FireNet; Mar 7th, 2008 at 1:55 pm.
See what you can, remember what you need

Fourzon | Earn via Coding
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
  #40
Mar 10th, 2008
Hello,
I need to show the encrypted image.. and using a software available freely on the internet i have to draw the histogram of the original and encrypted image...
Therefore to be able to draw the histogram i need to display the encrypted image..
I am sending a paper on aes image encryption... it has displayed the encrypted image and drawn the histogram for both original and encrypted image..
Can anyone help how we can display the encrypted image using either another language, software or whatever provided it has displayed the encrypted image...
Attached Files
File Type: zip v1-1-9.zip (651.3 KB, 2 views)
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