CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    May 2009
    Posts
    35

    unexpected termination

    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:ut | 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

  2. #2
    Join Date
    Oct 2005
    Location
    Minnesota, U.S.A.
    Posts
    680

    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

  3. #3
    Join Date
    May 2009
    Posts
    35

    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

  4. #4
    Join Date
    Oct 2005
    Location
    Minnesota, U.S.A.
    Posts
    680

    Re: unexpected termination

    Quote Originally Posted by Igeru View Post
    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 View Post
    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.

  5. #5
    Join Date
    May 2009
    Posts
    35

    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?

  6. #6
    Join Date
    Oct 2005
    Location
    Minnesota, U.S.A.
    Posts
    680

    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()

  7. #7
    Join Date
    May 2009
    Posts
    35

    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?

  8. #8
    Join Date
    Oct 2005
    Location
    Minnesota, U.S.A.
    Posts
    680

    Re: unexpected termination

    Quote Originally Posted by Igeru View Post
    {
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured