Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Crypto API Question

Crypto API Question

Scheduled Pinned Locked Moved C / C++ / MFC
questioncryptographyjsonperformancehelp
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Drakesal
    wrote on last edited by
    #1

    I am crazying with a problem with the following function:

    CStringA EncryptDecryptString(CStringA szString, BOOL bEncrypt)
    {
    HCRYPTPROV hCryptProv = NULL;
    LPCWSTR keyContainer = _T("MyKeyContainer");

       if(CryptAcquireContext(&hCryptProv,keyContainer,NULL,PROV\_RSA\_FULL,0))
             if (GetLastError() == NTE\_BAD\_KEYSET)
                CryptAcquireContext(&hCryptProv,keyContainer,NULL,PROV\_RSA\_FULL,CRYPT\_NEWKEYSET);
    
       HCRYPTHASH hHash = NULL;
       TCHAR szPassword\[11\]         = \_T("Password");
    
       if(CryptCreateHash(hCryptProv,CALG\_MD5,0,0,&hHash))
    {
             if(!CryptHashData(hHash,(BYTE \*)szPassword,wcslen(szPassword),0))
             {
                    // reset hash object to NULL
                    CryptDestroyHash(hHash);
                    hHash = NULL;       
             }
    }
    
       HCRYPTKEY hKey = NULL;
    
       CryptDeriveKey(hCryptProv,CALG\_RC4,hHash,0x00800000,&hKey);
    
       DWORD length= szString.GetLength() + 1;
       BYTE \* cipherBlock= (BYTE \*)malloc(length);
       memset(cipherBlock, 0, length);
       memcpy(cipherBlock, szString, length -1);
    
       if (bEncrypt)
             CryptEncrypt(hKey,0,TRUE,0,cipherBlock,&length,length);
       else
             CryptDecrypt(hKey,0,TRUE,0,cipherBlock,&length);
    
       CStringA szOutString(cipherBlock);
    
       if(cipherBlock) 
             free(cipherBlock); 
       if(hKey)
             CryptDestroyKey(hKey);
       if(hHash) 
          CryptDestroyHash(hHash);
       if(hCryptProv) 
          CryptReleaseContext(hCryptProv,0);
    
       return szOutString;
    

    }

    It's all ok with function, but sometimes when string is very very long during the Decryption i have only a part of string becouse the allocated memeory is not enought. How can i alloc the correct amount of memory during the decryption phase?

    L 1 Reply Last reply
    0
    • D Drakesal

      I am crazying with a problem with the following function:

      CStringA EncryptDecryptString(CStringA szString, BOOL bEncrypt)
      {
      HCRYPTPROV hCryptProv = NULL;
      LPCWSTR keyContainer = _T("MyKeyContainer");

         if(CryptAcquireContext(&hCryptProv,keyContainer,NULL,PROV\_RSA\_FULL,0))
               if (GetLastError() == NTE\_BAD\_KEYSET)
                  CryptAcquireContext(&hCryptProv,keyContainer,NULL,PROV\_RSA\_FULL,CRYPT\_NEWKEYSET);
      
         HCRYPTHASH hHash = NULL;
         TCHAR szPassword\[11\]         = \_T("Password");
      
         if(CryptCreateHash(hCryptProv,CALG\_MD5,0,0,&hHash))
      {
               if(!CryptHashData(hHash,(BYTE \*)szPassword,wcslen(szPassword),0))
               {
                      // reset hash object to NULL
                      CryptDestroyHash(hHash);
                      hHash = NULL;       
               }
      }
      
         HCRYPTKEY hKey = NULL;
      
         CryptDeriveKey(hCryptProv,CALG\_RC4,hHash,0x00800000,&hKey);
      
         DWORD length= szString.GetLength() + 1;
         BYTE \* cipherBlock= (BYTE \*)malloc(length);
         memset(cipherBlock, 0, length);
         memcpy(cipherBlock, szString, length -1);
      
         if (bEncrypt)
               CryptEncrypt(hKey,0,TRUE,0,cipherBlock,&length,length);
         else
               CryptDecrypt(hKey,0,TRUE,0,cipherBlock,&length);
      
         CStringA szOutString(cipherBlock);
      
         if(cipherBlock) 
               free(cipherBlock); 
         if(hKey)
               CryptDestroyKey(hKey);
         if(hHash) 
            CryptDestroyHash(hHash);
         if(hCryptProv) 
            CryptReleaseContext(hCryptProv,0);
      
         return szOutString;
      

      }

      It's all ok with function, but sometimes when string is very very long during the Decryption i have only a part of string becouse the allocated memeory is not enought. How can i alloc the correct amount of memory during the decryption phase?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      See the comments at http://msdn.microsoft.com/en-gb/library/windows/desktop/aa379913(v=vs.85).aspx[^].

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups