Re: CryptDeriveKey seems to create different keys with the same initial values?

From: Rauno Uusitalo (rauno.uusitalo_at_no_spam.fi)
Date: 09/18/03


Date: Thu, 18 Sep 2003 13:48:00 +0300

Stupid me! Just forget to copy the data to be encrypted into it's place.

                    if (CryptEncrypt(hKey, NULL, TRUE, 0, NULL, pBufSize, 0))
                        CryptEncrypt(hKey, NULL, TRUE, 0, reinterpret_cast<BYTE*>(malloc(*pBufSize)), pBufSize, *pBufSize);
should be

                    if (CryptEncrypt(hKey, NULL, TRUE, 0, NULL, pBufSize, 0))
                    {
                        *ppBuf = malloc(*pBufSize);
                                  CopyMemory(*ppBuf, szString, size);
                                  CryptEncrypt(hKey, NULL, TRUE, 0, reinterpret_cast<BYTE*>(*ppBuf), pBufSize, *pBufSize);
                              }

Sorry about the trouble,
Rauno

  "Rauno Uusitalo" <rauno.uusitalo@no_spam.fi> wrote in message news:OyPGARcfDHA.392@TK2MSFTNGP12.phx.gbl...
  Hi there!

  Hope somebody could point out what am I doing wrong. Purpose is to create functions to encrypt and decrypt strings by a given password. To me it seems like the CryptDeriveKey-calls are generating different keys with the same passwords resulting unability to decrypt the string. Please help!

  Best Regards,
  Rauno Uusitalo
    #include <wincrypt.h>
    #include <malloc.h>
    #include <stdio.h>
     
    void EncryptStr(LPCTSTR szString,
                           LPCTSTR szPassword,
                           LPVOID* ppBuf,
                           DWORD* pBufSize)
    {
        HCRYPTPROV hProv = NULL;
        HCRYPTHASH hHash = NULL;
        HCRYPTKEY hKey = NULL;
        LPTSTR szEncrypted = NULL;
     
        *ppBuf = NULL;
        *pBufSize = 0;
     
        if (!CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, CRYPT_SILENT))
            CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, CRYPT_NEWKEYSET | CRYPT_SILENT);
     
        if (NULL != hProv)
        {
            if (CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash))
            {
                if (CryptHashData(hHash, reinterpret_cast<BYTE*>(const_cast<LPTSTR>(szPassword)), lstrlen(szPassword) * sizeof *szPassword, 0))
                    if (CryptDeriveKey(hProv, CALG_RC4, hHash, 0, &hKey))
                    {
                        DWORD size = (lstrlen(szString) + 1) * sizeof *szString;
     
                        *pBufSize = size;
     
                        if (CryptEncrypt(hKey, NULL, TRUE, 0, NULL, pBufSize, 0))
                            CryptEncrypt(hKey, NULL, TRUE, 0, reinterpret_cast<BYTE*>(malloc(*pBufSize)), pBufSize, *pBufSize);
     
                        CryptDestroyKey(hKey);
                    }
     
                CryptDestroyHash(hHash);
            }
     
        CryptReleaseContext(hProv, 0);
        }
    }
     
    LPTSTR DecryptStr(LPVOID pBuf,
                                  DWORD bufSize,
                                  LPCTSTR szPassword)
    {
        HCRYPTPROV hProv = NULL;
        HCRYPTHASH hHash = NULL;
        HCRYPTKEY hKey = NULL;
        BYTE* pDecrypted = NULL;
     
        if (!CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, CRYPT_SILENT))
            CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, CRYPT_NEWKEYSET | CRYPT_SILENT);
     
        if (NULL != hProv)
        {
            if (CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash))
            {
                if (CryptHashData(hHash, reinterpret_cast<BYTE*>(const_cast<LPTSTR>(szPassword)), lstrlen(szPassword) * sizeof *szPassword, 0))
                    if (CryptDeriveKey(hProv, CALG_RC4, hHash, 0, &hKey))
                    {
                        pDecrypted = reinterpret_cast<BYTE*>(malloc(bufSize));
                        CryptDecrypt(hKey, NULL, TRUE, 0, pDecrypted, &bufSize);
                        CryptDestroyKey(hKey);
                    }
     
                CryptDestroyHash(hHash);
            }
     
        CryptReleaseContext(hProv, 0);
        }
     
    return reinterpret_cast<LPTSTR>(pDecrypted);
    }
     
    int _tmain(int argc, _TCHAR* argv[])
    {
        LPCTSTR szPlainText = _T("ABC");
        LPCTSTR szPassword = _T("Password");
        LPVOID pBuf = NULL;
        DWORD bufSize = 0;
     
        EncryptStr(szPlainText, szPassword, &pBuf, &bufSize);
     
        LPTSTR szDecrypt = DecryptStr(pBuf, bufSize, szPassword);
        
        _tprintf(_T("%s\n"), szDecrypt);
        
        free(szDecrypt);
        free(pBuf);
    }



Relevant Pages

  • unsubscribe
    ... > Sorry for the trouble. ... > Best Regards ... > working so finally I turned to you gurus. ... > cat source_file ...
    (SunManagers)
  • SUMMARY: sed help
    ... Well I got response from other forum just after I posted here. ... Sorry for the trouble. ... Best Regards ... working so finally I turned to you gurus. ...
    (SunManagers)
  • Re: Implementing CANopen => Help
    ... Hocommercial having some trouble ... > understanding how to actually start. ... > I've been reading about the object dictionary quite a bit, ... -- with best regards / mit freundlichen Grüßen ...
    (comp.arch.embedded)
  • Re: CANNOT get an ElTorito Disk
    ... each time I made the image file (with NERO, ... the device was recognized as El-Torito device? ... Can be a trouble the german OS? ... >> Best regards, ...
    (microsoft.public.windowsxp.embedded)
  • Re: Windows CE 5.0
    ... > text box any characters it receives from a PC. ... > However when i have CE 5.0 running on the device, the app displays in hex ... > Kind Regards, ... I haven't had any trouble with the serial drivers in CE 5.0. ...
    (microsoft.public.windowsce.platbuilder)