Thread: help implementing wincrypt..

  1. #1
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378

    help implementing wincrypt..

    hey, i'm trying to use wincrypt.h for some local encryption of plain text and for some reason its not working. its probably because i'm doing something wrong, lol.

    does anyone know of a tutorial i could read?

    here's the code i have so far:
    Code:
    void EncryptText(HWND hEdit)
    {
        //-------------------------------------------------------------------
        // Declare and initialize variables.
    
        HCRYPTPROV hCryptProv = 0;        // handle for a cryptographic provider context
        HCRYPTKEY hKey = 0;               // handle for a cryptographic key
        HCRYPTHASH hHash;
        LPCSTR UserName = "MyKeyContainer";  // name of the key container to be used
        DWORD dwDataSize;
        TCHAR *szText;
        //-------------------------------------------------------------------
        // Get the text to be encrypted.
        dwDataSize = GetWindowTextLength(hEdit);
        if (dwDataSize > 0)
        {
            szText = malloc(dwDataSize+1);
            GetWindowText(hEdit, szText, dwDataSize);
        }
        // Attempt to acquire a context and a key
        // container. The context will use the default CSP
        // for the RSA_FULL provider type. DwFlags is set to zero
        // to attempt to open an existing key container.
    
    if(CryptAcquireContext(
       &hCryptProv,               // handle to the CSP
       UserName,                  // container name 
       NULL,                      // use the default provider
       PROV_RSA_FULL,             // provider type
       0))                        // flag values
    {
        MessageBox(NULL, UserName, "Cryptographic context key container", MB_OK);
    }
    else
    { 
    //-------------------------------------------------------------------
    // An error occurred in acquiring the context. This could mean
    // that the key container requested does not exist. In this case,
    // the function can be called again to attempt to create a new key 
    // container. Error codes are defined in Winerror.h.
     if (GetLastError() == NTE_BAD_KEYSET)
     {
       if(CryptAcquireContext(
          &hCryptProv, 
          UserName, 
          NULL, 
          PROV_RSA_FULL, 
          CRYPT_NEWKEYSET)) 
        {
          MessageBox(NULL, "A new key container has been created.", "ok", MB_OK);
        }
        else
        {
          MessageBox(NULL, "Could not create a new key container.", "OOPS", MB_OK);
          exit(1);
        }
     }
      else
      {
          MessageBox(NULL, "A cryptographic service handle could not be "
              "acquired.", "damn", MB_OK);
          exit(1);
       }
      
    } // End of else.
    //-------------------------------------------------------------------
    // A cryptographic context and a key container are available. Perform
    // any functions that require a cryptographic provider handle.
    
    
    //  Create a random session key. 
    
     if(CryptGenKey(
              hCryptProv, 
              KEYLENGTH | CALG_MD5, 
              CRYPT_EXPORTABLE, 
              &hKey))
     {
             MessageBox(NULL, "A session key has been created.", "SWEET", MB_OK);
     } 
     else
     {
              MessageBox(NULL, "Error during CryptGenKey.", "........", MB_OK); 
              exit(1);
     }
    //-------------------------------------------------------------------
    //  The key created can be exported into a key BLOB that can be
    //  written to a file.
    
    //--------------------------------------------------------------------
    // Acquire a hash object handle.
    
    if(CryptCreateHash(
       hCryptProv, 
       CALG_MD5, 
       0, 
       0, 
       &hHash)) 
    {
        printf("An empty hash object has been created. \n");
    }
    else
    {
        printf("Error during CryptBeginHash!\n");
        exit(1);
    }
    
    // Insert code that uses the hash object here.
    
    /* Finally - on to the part where the text
     * is actually encrypted.
     */
     
    if (CryptEncrypt(hKey, hHash, TRUE, 0, szText, &dwDataSize, (DWORD)sizeof(dwDataSize)))
    {
        MessageBox(NULL, szText, "szText", MB_OK);
    }
    else
    {
        if (GetLastError() == ERROR_MORE_DATA)
        {
            MessageBox(NULL, "I got the ERROR_MORE_DATA error.. :(", "........!", MB_OK);
        }
    }
    
    //  ...
    //  When you have finished using the key, free the resource.
    if (!CryptDestroyKey(hKey))
    {
              MessageBox(NULL, "Error during CryptDestroyKey.", "OOPS", MB_OK); 
              exit(1);
    }
    
    
    //--------------------------------------------------------------------
    // After processing, hCryptProv and hHash must be released.
    
    if(hHash) 
       CryptDestroyHash(hHash);
    if(hCryptProv) 
       CryptReleaseContext(hCryptProv,0);
    }
    and that outputs that "Error during CryptGenKey"
    ??
    thanks


    btw, that code is a compilation of the examples provided on the MSDN website for each function except for CryptEncrypt().
    Last edited by willc0de4food; 09-12-2005 at 03:31 PM.
    Registered Linux User #380033. Be counted: http://counter.li.org

  2. #2
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    mm....so basically NO ONE that visits this board knows a THING about encryption. lol

    thats kind of odd...
    yea..i think i find that odd.
    yea...
    Registered Linux User #380033. Be counted: http://counter.li.org

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Did you check to see what GetLastError returns?
    Code:
    else
    {
    char temp[33];
    MessageBox(NULL,"Error during CryptGenKey:",itoa(GetLastError(),temp,10),MB_OK);
    }
    Then just find the system error code on msdn...
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  4. #4
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    it returns -2146893816, lol

    but i found an example on MSDN here and thats returning 0;
    it fails at CryptAcquireContext first and then of course, the rest of the functions fail. but it returning 0 makes me think its returning ERROR_SUCCESS but its not and i'm quite confused. with this code:
    Code:
    if(CryptAcquireContext(
       &hCryptProv, 
       NULL, 
       NULL, 
       PROV_RSA_FULL, 
       0)) 
    {
        MessageBox(NULL, "CryptAcquireContext complete.", "OK", MB_OK);
    }
    else
    {
        if (GetLastError() == ERROR_SUCCESS || GetLastError() == 0)
        {
            MessageBox(NULL, "CryptAcquireContext returned ERROR_SUCCESS.", "?", MB_OK);
        }
        else
        {
             MessageBox(NULL, "Acquisition of context failed.", "ERROR", MB_OK);
             fprintf(fileptr, "Error: CryptAcquireContext: %x\t%s\n", GetLastError(), itoa(GetLastError(),temp,10));
        }
    }
    i get the messagebox "Acquisition of context failed." and then in the error file it says "Error: CryptAcquireContext: 0 0"
    *is confused* (O_o) ?

    lol thanks.
    Registered Linux User #380033. Be counted: http://counter.li.org

  5. #5
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    If you run the example code at that site you posted do you get the error? That example works fine for me...
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  6. #6
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    If i run the example code from that site, this is the output i get..
    Process beginning. Creating a session key.


    An error occurred in running the program.
    Acquisition of context failed.
    Error number 80090016
    .Program terminating.



    Microsoft says that error comes when trying to install IE5 which I'm obviously not trying to do. Here's the page: http://support.microsoft.com/default...;EN-US;q246865
    Only problem is..I don't have the reg key they're talking about. there is no "Cryptography" key in my HKU\.DEFAULT\Software\Microsoft hive.. ?

    but another website says the error code means "Keyset does not exist." this would be true, because it doesn't.

    any idea's why this is happening?
    Last edited by willc0de4food; 09-18-2005 at 09:44 AM.
    Registered Linux User #380033. Be counted: http://counter.li.org

  7. #7
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Try passing CRYPT_NEWKEYSET as the last argument to CryptAcquireContext.
    Quote Originally Posted by MSDN
    CRYPT_NEWKEYSET
    Creates a new key container with the name specified by pszContainer. If pszContainer is NULL, a key container with the default name is created.

  8. #8
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    hm....that worked.

    now..lets see if i can figure out how to make it work with my program.. lol THANKS
    Registered Linux User #380033. Be counted: http://counter.li.org

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Implementing TLVs
    By brooksbp in forum Networking/Device Communication
    Replies: 1
    Last Post: 07-30-2008, 01:41 PM
  2. Implementing shared/exclusive locking (readers/writer) lock
    By ruj.sabya in forum Linux Programming
    Replies: 0
    Last Post: 05-08-2008, 12:06 AM
  3. Implementing of queue using objects?
    By Argo_Jeude in forum C++ Programming
    Replies: 5
    Last Post: 08-07-2007, 11:55 AM
  4. Implementing Mathematical Concepts in Code
    By MisterWonderful in forum Tech Board
    Replies: 6
    Last Post: 03-08-2004, 07:44 AM
  5. I need help with implementing strings???
    By atif in forum C Programming
    Replies: 8
    Last Post: 04-07-2002, 10:04 AM