| | |
EOF problem
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
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
Code to open Ciphertext.txt and save decrypted data to text.txt file
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...
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)
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
c++ Syntax (Toggle Plain Text)
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...
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
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
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.
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:
For this to work right, you must ensure your encoding process creates a file that is a multiple of 16 bytes in size.
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)
#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; }
For reading the full file, control the loop as:
C++ Syntax (Toggle Plain Text)
while( f.read( (char *)buff, 16 ) )
"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.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Various subtleties, but 'EOF character' is not a good idiom to maintain.
http://faq.cprogramming.com/cgi-bin/...&id=1043284351
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
•
•
•
•
Various subtleties, but 'EOF character' is not a good idiom to maintain.
http://faq.cprogramming.com/cgi-bin/...&id=1043284351
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.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
•
•
•
•
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.
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
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.
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.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
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.
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.
•
•
•
•
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.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
c++ Syntax (Toggle Plain Text)
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...
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.
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)
for ( j=0; j<BC; j++) for ( i=0; i < 4; i++) { a[i][j] = buff[j][i]; }
"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.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
![]() |
Similar Threads
- read to end of line problem (C)
- amigious problem with fstream (C++)
- No idea how to do this problem... (C++)
- problem reading text file to struct (C++)
- File processing problem (C++)
- Stack Queue Fstream (C++)
- fread problem? (C)
Other Threads in the C++ Forum
- Previous Thread: Inheritance and composition
- Next Thread: Composition problem "'XXX' is not a base or member"
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






