943,699 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 8640
  • C++ RSS
You are currently viewing page 4 of this multi-page discussion thread; Jump to the first page
Feb 28th, 2008
0

Re: EOF problem

Hello,
I am able to locate the error... The code
Quote ...
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...
Reputation Points: 12
Solved Threads: 0
Junior Poster in Training
kartouss is offline Offline
80 posts
since Jan 2008
Feb 28th, 2008
0

Re: EOF problem

Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Mar 1st, 2008
0

Re: EOF problem

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

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

Re: EOF problem

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, 18 views)
Reputation Points: 12
Solved Threads: 0
Junior Poster in Training
kartouss is offline Offline
80 posts
since Jan 2008
Mar 5th, 2008
0

Re: EOF problem

>>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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Mar 6th, 2008
0

Re: EOF problem

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
Reputation Points: 10
Solved Threads: 1
Newbie Poster
sauron84 is offline Offline
6 posts
since Feb 2008
Mar 6th, 2008
0

Re: EOF problem

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

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

Re: EOF problem

To get back to the beginning of the file for reading, after finding size:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Mar 7th, 2008
0

Re: EOF problem



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

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 108
Solved Threads: 7
Posting Whiz in Training
FireNet is offline Offline
256 posts
since May 2004
Mar 10th, 2008
0

Re: EOF problem

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, 14 views)
Reputation Points: 12
Solved Threads: 0
Junior Poster in Training
kartouss is offline Offline
80 posts
since Jan 2008

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