| | |
Help with reading data from a file
![]() |
•
•
Join Date: Jun 2008
Posts: 16
Reputation:
Solved Threads: 0
Hi there,
I want to read 1 byte of information, in the form of an array of unsigned chars [1], from a file. I then want to use this piece of data to act as a secret key for a message authentication code I will be performing. The key will be a 20 byte array of unsigned chars [20].
At the moment I have this as my key:
memset( key, 0x0b ,20);
It is basically 0b repeated x 20 to make up a 20 byte key:
0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b
But instead I want to store they key in a file and read it from there rather than hard code it in.
Here is what I am doing at the moment. I have saved '0x0b' in a .dat file, that is the files only content.
But it says that an unsigned char etc can not be used as an integer in the 2nd command of the memset. Although it worked fine when '0x0b' was hard coded into it. The code is opening and getting some data ok from the file, but I just need it converted to an array of unsigned chars ok, and for it to be accepted as my key.
Basically I want to replace 0x0b with the variable, sequence, and retrieve the value of 'sequence' from a file, instead of having it hard coded in. And its value should be a 20 byte array of unsigned chars: ie = 0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b
Any help with this would be great,
Thanks.
I want to read 1 byte of information, in the form of an array of unsigned chars [1], from a file. I then want to use this piece of data to act as a secret key for a message authentication code I will be performing. The key will be a 20 byte array of unsigned chars [20].
At the moment I have this as my key:
memset( key, 0x0b ,20);
It is basically 0b repeated x 20 to make up a 20 byte key:
0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b
But instead I want to store they key in a file and read it from there rather than hard code it in.
Here is what I am doing at the moment. I have saved '0x0b' in a .dat file, that is the files only content.
C++ Syntax (Toggle Plain Text)
std::fstream myFile("/home/adam/Documents/cryptopp/mackeystring.dat",std::ios::app); myFile.open("/home/adam/Documents/cryptopp/mackeystring.dat"); if (! myFile) // Always test file open { std::cout << "Error opening output file\n"; return -1; } //char *key; std::string keystring; getline(myFile,keystring); unsigned char sequence [1]; memcpy(sequence,&keystring,1); myFile.close(); memset( key, sequence ,20);
But it says that an unsigned char etc can not be used as an integer in the 2nd command of the memset. Although it worked fine when '0x0b' was hard coded into it. The code is opening and getting some data ok from the file, but I just need it converted to an array of unsigned chars ok, and for it to be accepted as my key.
Basically I want to replace 0x0b with the variable, sequence, and retrieve the value of 'sequence' from a file, instead of having it hard coded in. And its value should be a 20 byte array of unsigned chars: ie = 0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b
Any help with this would be great,
Thanks.
1. You have a binary file. Don't use getline to get data from it - getline gets a line from a text file; use read():
The 1st arg of read() must be char* pointer, so use cast op if you want to read into unsigned char array.
2. Have a look at this (incorrect) code fragment:
You never get the 1st byte of keystring contents! Right construct:
An address (pointer to) of std::string object is not the same thing as this string data address. It's an address of a string object header. So you get a byte from this header but it's absolutely uninteresting result for you...
C++ Syntax (Toggle Plain Text)
unsigned char key[20]; fstream f(".....",ios::binary); // No need to open file explicitly // when the filename is specified in the constructor. if (f.is_open()) // or simply: if (f) { if (f.read((char*)key,sizeof key)) { // you get this data! } }
2. Have a look at this (incorrect) code fragment:
cpp Syntax (Toggle Plain Text)
std::string keystring; ... unsigned char sequence [1]; memcpy(sequence,&keystring,1);
cpp Syntax (Toggle Plain Text)
memcpy(sequence,keystring.data(),1);
•
•
Join Date: Jun 2008
Posts: 16
Reputation:
Solved Threads: 0
Many thanks for the reply, I will just read the whole key in one go now so have changed the .dat file to read 0x0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b for the 20 byte key.
Where should I use the op cast to convert between an unsigned char and char*? And if you could type out for me what that op cast command should look like that would be great.
Was also slightly confused about the 2nd if statement, whether that was just to test that the type it is reading is correct, or whether it is actually doing some sort of reading itself.
Where you put the comment of //you get this data!
I have put the line: myFile.read (key,20);
Which, as expected, gives conversion errors as it not type char*
So would I need to change 'key' to be a variable of char* and then do the opcast on it to get my 20 byte unsigned char array called key?
Where should I use the op cast to convert between an unsigned char and char*? And if you could type out for me what that op cast command should look like that would be great.
Was also slightly confused about the 2nd if statement, whether that was just to test that the type it is reading is correct, or whether it is actually doing some sort of reading itself.
Where you put the comment of //you get this data!
I have put the line: myFile.read (key,20);
Which, as expected, gives conversion errors as it not type char*
So would I need to change 'key' to be a variable of char* and then do the opcast on it to get my 20 byte unsigned char array called key?
Last edited by adamj2; Aug 3rd, 2008 at 8:34 am.
I don't understand why you don't understand...
Look at my code snippet. Substitute your stream variable (myFile) instead of mine one (f).
The stream.read() returns a reference to the stream. You can use a stream class object reference in conditional expression directly: it's true if a previous operation was successful. In actual fact there are a chain of casting here, but what does in matter to us?..
I hope, you feel at home not only in copy/paste environment...
Look at my code snippet. Substitute your stream variable (myFile) instead of mine one (f).
cpp Syntax (Toggle Plain Text)
if (myFile.read((char*)key,20)) { // Well, you gOt your key! It's here, in key unsigned char array now! // (char*)key - it's a proper cast here! .... don't worry, process your key as unsigned char if you wish...
I hope, you feel at home not only in copy/paste environment...
•
•
Join Date: Jun 2008
Posts: 16
Reputation:
Solved Threads: 0
Thanks,
I have now got it reading ok from the file into the array of unsigned chars.
The only problem is that it is interpreting the key different than it should be.
The content of my file is:
0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b
When I print out the contents of the unsigned char array it produces:
30 62 30 62 30 62 30 62 30 62 30 62 30 62 30 62 30 62 30 62
So I maybe it is storing each '0' and each 'b' seperately as 1 byte in each slot of the unsigned char array?
Where as my key is basically 0x0b repeated 20 times. So 0b is 1 byte. How do I get it to read 2 characters as being 1 byte, rather than each character being a byte? Cheers.
I have now got it reading ok from the file into the array of unsigned chars.
The only problem is that it is interpreting the key different than it should be.
The content of my file is:
0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b
When I print out the contents of the unsigned char array it produces:
30 62 30 62 30 62 30 62 30 62 30 62 30 62 30 62 30 62 30 62
So I maybe it is storing each '0' and each 'b' seperately as 1 byte in each slot of the unsigned char array?
Where as my key is basically 0x0b repeated 20 times. So 0b is 1 byte. How do I get it to read 2 characters as being 1 byte, rather than each character being a byte? Cheers.
•
•
Join Date: Jun 2008
Posts: 16
Reputation:
Solved Threads: 0
•
•
•
•
Please, post your current code now (especially key writing part). You wrote a textual representation of repeated "0b" pair (exactly 30 62 in ASCII) instead of packed byte '0x0b').
0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b
NB: the key, as exactly given by the test vector that I am using, using hmac-sha from rfc2202 is:
0x0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b
C++ Syntax (Toggle Plain Text)
unsigned char key[20]; fstream myFile ("/home/adam/Documents/cryptopp/mackeystring.dat", ios::binary); myFile.open("/home/adam/Documents/cryptopp/mackeystring.dat"); if (myFile) { myFile.read((char*)key,sizeof key); } else printf("error opening file"); myFile.close(); printf("\nFILE CONTENTS: \n"); for (int i=0; i<20; i++) printf(" %02x ", key[i]); //full content of key= 30 62 30 62 30 62 30 62 30 62 30 62 30 62 30 62 30 62 30 62 //this is where the 20 byte key is used: HMAC<SHA1> hmac( key, sizeof(key ) ); ... ...
Previously I used: memset (key, 0x0b, 20);
to get my key, and if gave the correct result for my hmac-sha calculation, but have taken this out to retrieve it from the file instead.
So could either have the block of 0x0b stored in the file, and then put that 1 byte unsigned char into the memset, or just have the full key written to the file, which is what I have done now. Because when I try to replace 0x0b in the memset, with a unsigned char variable, it says invalid conversion between unsigned char and int for the memset expected values.
When I put the 0x in my ,dat file, both the 0 and the x are being read as a characters individually (ie 30 78)so took it out.
It works when I use the test vector with the string value of Jefe being used as the key in the file. As each character is one byte. But when I have the 0b pairs in, it does not give the correct output when I use that value for my calculation using the key.
Thanks again.
Last edited by adamj2; Aug 3rd, 2008 at 4:42 pm.
Wait a bit
: there are two different cases (let's use C++ notation):
1. mackeystring.dat contains TEXT data "0b0b... and so on 0b" (if so why it has .dat extension?)
2. mackeystring.dat contains BINARY data "\x0b\x0b...\x0b" (if so you can't print these 30 60 pairs).
In case#1 you must convert this data in binary form after reading (pack two byte values into a single byte). It's a simple limbering-up for beginner...
In case#2 you may place the file contents in the key array directly. No problems...
Happy end of the thread of this story?..
: there are two different cases (let's use C++ notation):1. mackeystring.dat contains TEXT data "0b0b... and so on 0b" (if so why it has .dat extension?)
2. mackeystring.dat contains BINARY data "\x0b\x0b...\x0b" (if so you can't print these 30 60 pairs).
In case#1 you must convert this data in binary form after reading (pack two byte values into a single byte). It's a simple limbering-up for beginner...
In case#2 you may place the file contents in the key array directly. No problems...
Happy end of the thread of this story?..
•
•
Join Date: Jun 2008
Posts: 16
Reputation:
Solved Threads: 0
So in case#2 do you mean that the contents of my file should be:
\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b ?
I have done that and it treats each '\', 'x', '0' and 'b' as a seperate character and gives a 2 character representation of each. So basically the is up to 80 odd characters needing one byte of storage....when I just want each slot of my array to have the content of '0x0b'.
Previously, I just used memset(key, 0x0b, 20)
Which when printed using printf("%02x, key[i]) etc.. in a for loop,
GIves: 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b
So what I am doing now is not giving me the same contents of my previous key array. What am I doing wrong?! Is it the code itself, or the way I am storing the data in the file?!
\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b ?
I have done that and it treats each '\', 'x', '0' and 'b' as a seperate character and gives a 2 character representation of each. So basically the is up to 80 odd characters needing one byte of storage....when I just want each slot of my array to have the content of '0x0b'.
Previously, I just used memset(key, 0x0b, 20)
Which when printed using printf("%02x, key[i]) etc.. in a for loop,
GIves: 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b
So what I am doing now is not giving me the same contents of my previous key array. What am I doing wrong?! Is it the code itself, or the way I am storing the data in the file?!
•
•
Join Date: Jun 2008
Posts: 16
Reputation:
Solved Threads: 0
Just made a quick experiemental change and if I create an integer variable and give it the value of 0x0b
and then do:
memset(key, integer, 20);
it works.
So maybe I should be trying to read the content of 0x0b from the file and saving it as an integer? But the the same problem as it treating each character individually may arise rather than a block?
and then do:
memset(key, integer, 20);
it works.
So maybe I should be trying to read the content of 0x0b from the file and saving it as an integer? But the the same problem as it treating each character individually may arise rather than a block?
![]() |
Similar Threads
- Reading data file in C (C)
- No documentation on reading in int from file (Python)
- read data file in C (C)
- reading data from file to vector (C++)
- Reading an input file as a class memeber function (C++)
- Perl/CGI (Reading Data) Part II (Computer Science)
- reading txt file into array (C++)
- Reading from external data file (C++)
Other Threads in the C++ Forum
- Previous Thread: Fairly new to C++: Need help with Strings and Dynamic Array Allocation
- Next Thread: 64k text segment error
Views: 1431 | Replies: 10
| Thread Tools | Search this Thread |
Tag cloud for C++
6 algorithm array arrays assignment beginner binary borland browser c++ c/c++ calculator char class classes code command compile compiler constructor conversion convert count delete desktop dll dynamic encryption error file files fstream function functions game givemetehcodez graph gui homework http i/o iamthwee ifstream input int java lazy lib library linker list loop looping loops math matrix memory newbie news number object objects opengl output path pointer pointers problem program programming project random read reading recursion recursive reference simple sort sorting spoonfeeding string strings struct student studio template templates text time tree variable vc++ vector video visual win32 window windows winsock






