Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

The following is the contents of a

Status
Not open for further replies.

nerram

Programmer
Apr 23, 2003
56
IE
The following is the contents of a text file I wish to read:
P$öóŦ :mk ¨=EdQ°Ïºü­z³ÔqSM,Ý
‹RŒÇý${”

It is the output of an encrpyion process. I am trying to read the first line into var1 and the second line into var2. When I try to read the file it only reads the first three characters. I suspect it assumes that the $ means EOF. How could i read the first line into var1 and the second into var2? On a side not if the encryption process yields a \n or some other special character will this cause me problems too?
 
Encrypted data (normally at least) contains all kind of characters.
Thus it doesn't make sense to talk about 'lines'.

Also it doesn't make sense to display the 'characters'. Use a (hex)dump to see/show what's 'inside' the encrypted data.

Read the file in binary mode.

/JOlesen
 
I need to decrypt the contents of the file so I need to get the contents into a variable do I not? The way I wrote the encryption was to put my username on line 1 and my password on line 2. Should I review the way I did this? What would be a better suggestion
 
It doesn't make sense to write binary data to a file opened in text-mode.

How it should /could be done depends amonst other things on the encryption method used.
Some (like DES) will operate on blocks of data : if your data are shorter than a blocksize / multiplum of, you will have to pad your data with 'dummy' data.
To decrypt stuff like that and return only the real data, you will have to know what is dummy data and what's not - ie. you will need some kind of length variable.

You will have to supply more info to get a proper answer.

/JOlesen
 
My suggestion would be to encode the first character on line one as the number of bytes in the user name followed by the bytes containing the data. Post user name, i would follow the same procedure. Read in the first byte, get the lenght and read that much in. Dont bother with next lines. It is not the best approach but it works. You can also save the data to a fixed size struct (no pointers) and write the struct out. Then just read back into the struct.

Matt
 
I think the first hurdle is actually getting whats in the file into a single variable. I can change the way it is encrypted so there are no lines. I am still stuck as it only reads in the first three characters.
 
The reason why you cant read past the first 3 characters is that you are reaching a NULL value (character code 0).

Using strlen() on the data will tell you its length is 3, yet if you call GetFileSize() it will tell you the correct length.

It is best not to look for a line break in encrypted data, because a line break might well be found inside your encrypted username or password. That would mess things up!

The best method for getting the contents of an encrypted file is todo this:

(NB: This code wont compile, its used as a guide only)

BYTE* pData = NULL;
DWORD dwSize = 0;

dwSize = GetFileSize();

pData = (BYTE *)GlobalAlloc(GPTR, dwSize + 1);

ReadFile(pData);

// Now pData contains your whole file, you can look up and down the pieces of data by using [index], pData[0], pData[1] etc


for (DWORD i = 0; i < dwSize; i++)
{
if pData == '\n'
{
// You have found a line break, try to see if the data on this line will decrypt
}
}

GlobalFree(pData);
CloseHandle();



Hope that gives you some pointers, but basically, dont rely on encrypted data being easy to read. There could be NULLs, line breaks, line feeds, back spaces etc. While this may look strange to you, all they are are ASCII values next to each other.

eg,
NULL = ASCII 0 = \0
Line break = ASCII 10 = \n
Line feed = ASCII 13 = \r ( i think, might have got mixed up with line break)

etc
etc

Hope that helps

Skute

&quot;There are 10 types of people in this World, those that understand binary, and those that don't!&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top