Click to See Complete Forum and Search --> : unexpected termination


Igeru
July 26th, 2009, 09:23 AM
Hi everybody,

I have a program that encrypts a file ,by reading it 8-bit at a time, manipulating them and puting them into a temp file, when this is done, it copies the temp to the original file.

Now I've got three problems, at least...

First is how can I read a char, manipulate it and putting it back instead of the old char (that would surely save time)? I didn't figure out a way using-
" fstream File (File, ios::out | ios::in | ios::binary) " (thought the then in+out would solve this).

Second is how can I detect an unexpected termination of the program (as in THE PROGRAM IS NOT RESPONDING, crtl+alt+del and so on....)? how can I roll back the changes that were made on the file if he was not fully encrypted / decrypted?

Third is what could I do for my program not to get stuck somewhere on the way when it's handeling "big files" (200mb and up)?


Thanks

egawtry
July 26th, 2009, 03:30 PM
Igeru,

1. Just for reference, a byte level encryption like that is not very secure. Unless you are just trying to keep non-hackers out.

2. For that type of I/O, don't use C++ streaming, open with standard file io using open, fopen, or CreateFile. Then you can use seek, fseek, or SetFilePointer to back up a byte and write the new value. If you want to support larger files, use the CreateFile().

3. You may want to take a look at the Windows CryptAPI.

-Erik

Igeru
July 26th, 2009, 04:05 PM
1. Yaeh I know of block cipher, just gave an example, but thanks.

2. why shouldn't I use c++ streaming?
by the way, my program opens and deals with the file perfectly, It's just get stuck if I open another program or I move the main window while crypting.. why is that?

3. what is Windows CryptAPI? Can I manipulat it as I want? I use a uniqe cryption system


THANKs

egawtry
July 26th, 2009, 04:24 PM
2. why shouldn't I use c++ streaming?
by the way, my program opens and deals with the file perfectly, It's just get stuck if I open another program or I move the main window while crypting.. why is that?

You probably have a tight loop reading the file without a message loop. It probably doesn't stop, it just looks like it stops.

Try to find the c++ streaming equivalant of fseek(). Then you can read, back up, write your new byte, then read the next.


3. what is Windows CryptAPI? Can I manipulat it as I want? I use a uniqe cryption system


The Windows CryptAPI is the interface into SSL. If you are using your own algorithm, don't worry about it.

Igeru
July 26th, 2009, 04:28 PM
I see, the loop reading and manipulating the file is in a function and wile it is running the messege loop doesn't run.. how can I resolve this? inserting the whole encryption loop somehow to the messege loop?? threading? is there another way?

egawtry
July 26th, 2009, 08:20 PM
For long functions, there are two ways: put it on its own thread, or add a message loop pump. The first way is the best, but it is more complicated programming. The latter is easy, but slows down your algorithm.

Add this in your loop:


{
MSG msg;
if( PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE) )
{
if( msg.message == WM_QUIT ) break; // or return if necessary
PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}



or if you are using MFC:

AfxGetApp()->PumpMessage()

Igeru
July 31st, 2009, 02:35 AM
{
MSG msg;
if( PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE) )
{
if( msg.message == WM_QUIT ) break; // or return if necessary
PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

this does nothing. or I didnt really understand what to do with that..
help?

egawtry
July 31st, 2009, 10:21 AM
{
MSG msg;
if( PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE) )
{
if( msg.message == WM_QUIT ) break; // or return if necessary
PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

this does nothing. or I didnt really understand what to do with that..
help?

If you put that in your loop, it should allow the display to update. If the display is still freezing, then you definately have a bug in your algorithm.

Here is a simple way to do what you are saying:



HANDLE hFile = CreateFile(_T("MyFile.txt"), GENERIC_READ|GENERIC_WRITE, FILE_SHARE_NONE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if( !hFile || (hFile==INVALID_HANDLE_VALUE) )
{
DWORD dwError = GetLastError();
TCHAR tszBuf[256];
_stprintf_s(tszBuf, _T("Error: %d"), dwError);
MessageBox(NULL, tszBuf, NULL, MB_OK|MB_ICONEXCLAMATION);
}
else
{
DWORD dwRead, dwWrite;
BYTE byteBuffer[256];
while( ReadFile(hFile, byteBuffer, sizeof(byteBuffer), &dwRead, NULL) )
{
if( dwRead == 0 ) break;
for(DWORD i = 0; i < dwRead; ++i)
byteBuffer[i] = DoMyEncrypt(byteBuffer[i]);
SetFilePointer(hFile, -dwRead, NULL, FILE_CURRENT);
WriteFile(hFile, byteBuffer, dwRead, &dwWrite, NULL);
}
CloseHandle(hFile);
}


If you want to keep the screen updating, add the message dispatch just before the for() loop.