Re: unexpected termination
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
Re: unexpected termination
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
Re: unexpected termination
Quote:
Originally Posted by
Igeru
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.
Quote:
Originally Posted by
Igeru
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.
Re: unexpected termination
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?
Re: unexpected termination
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:
Code:
{
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:
Code:
AfxGetApp()->PumpMessage()
Re: unexpected termination
{
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?
Re: unexpected termination
Quote:
Originally Posted by
Igeru
{
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:
Code:
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.