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. How can I get byte pointers to give CString values ?

How can I get byte pointers to give CString values ?

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestiondebugging
6 Posts 3 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.
  • S Offline
    S Offline
    SherTeks
    wrote on last edited by
    #1

    I have this code snippet which writes an encrypted value of a string into a file (KEY_FILE -- #defined as C:\xyz.txt) ;

    BYTE *pbBlob;
    DWORD cbBlob;

    :
    :
    :
    

    if (!CryptExportKey(hSessionKey, hKey, SIMPLEBLOB, 0, NULL, &cbBlob))
    {
    dwResult = GetLastError();
    MessageBox("Error CryptExportKey() failed.", "Information", MB_OK);
    return;
    }
    if (!CryptExportKey(hSessionKey, hKey, SIMPLEBLOB, 0, NULL, &cbBlob))
    {
    dwResult = GetLastError();
    MessageBox("Error CryptExportKey() failed.", "Information", MB_OK);
    return;
    }
    if ((pbBlob = (BYTE *) LocalAlloc(LMEM_ZEROINIT,cbBlob)) == NULL)
    {
    dwResult = ERROR_NOT_ENOUGH_MEMORY;
    MessageBox("Error LocalAlloc() failed.", "Information", MB_OK);
    return;
    }
    if (!CryptExportKey(hSessionKey, hKey, SIMPLEBLOB, 0, pbBlob, &cbBlob))
    {
    dwResult = GetLastError();
    MessageBox("Error CryptExportKey() failed.", "Information", MB_OK);
    return;
    }

    if(hSessionKey != 0)
    CryptDestroyKey(hSessionKey);

    hSessionKey = 0;

    FILE *fp = fopen(KEY_FILE, "w+b");
    if (fp) {
    fwrite(pbBlob, 1, cbBlob, fp);
    fclose(fp);
    }

    I find a junk value written by the above code in "C:\xyz.txt" which is like :  h ¤ zÛ³Wp <ø®lŒoWP¸"Ç^1i M€ÌygUjŽæ#Eì}ÔKËëü’²ô â  äÃr[X>¾U Since, the file contained  h ¤ zÛ³Wp <ø®lŒoWP¸"Ç^1i M€ÌygUjŽæ#Eì}ÔKËëü’²ô â  äÃr[X>¾U string, I was expecting pbBlob to point to the above content ie.  h ¤ zÛ³Wp <ø®lŒoWP¸"Ç^1i M€ÌygUjŽæ#Eì}ÔKËëü’²ô â  äÃr[X>¾U But when I debug, the following line fwrite(pbBlob, 1, cbBlob, fp) shows me the value of pbBlob = "||" and cbBlob = 76 What is the problem here. How can I get the junk value in a CString ? Will some kind of casting help ?

    S S 2 Replies Last reply
    0
    • S SherTeks

      I have this code snippet which writes an encrypted value of a string into a file (KEY_FILE -- #defined as C:\xyz.txt) ;

      BYTE *pbBlob;
      DWORD cbBlob;

      :
      :
      :
      

      if (!CryptExportKey(hSessionKey, hKey, SIMPLEBLOB, 0, NULL, &cbBlob))
      {
      dwResult = GetLastError();
      MessageBox("Error CryptExportKey() failed.", "Information", MB_OK);
      return;
      }
      if (!CryptExportKey(hSessionKey, hKey, SIMPLEBLOB, 0, NULL, &cbBlob))
      {
      dwResult = GetLastError();
      MessageBox("Error CryptExportKey() failed.", "Information", MB_OK);
      return;
      }
      if ((pbBlob = (BYTE *) LocalAlloc(LMEM_ZEROINIT,cbBlob)) == NULL)
      {
      dwResult = ERROR_NOT_ENOUGH_MEMORY;
      MessageBox("Error LocalAlloc() failed.", "Information", MB_OK);
      return;
      }
      if (!CryptExportKey(hSessionKey, hKey, SIMPLEBLOB, 0, pbBlob, &cbBlob))
      {
      dwResult = GetLastError();
      MessageBox("Error CryptExportKey() failed.", "Information", MB_OK);
      return;
      }

      if(hSessionKey != 0)
      CryptDestroyKey(hSessionKey);

      hSessionKey = 0;

      FILE *fp = fopen(KEY_FILE, "w+b");
      if (fp) {
      fwrite(pbBlob, 1, cbBlob, fp);
      fclose(fp);
      }

      I find a junk value written by the above code in "C:\xyz.txt" which is like :  h ¤ zÛ³Wp <ø®lŒoWP¸"Ç^1i M€ÌygUjŽæ#Eì}ÔKËëü’²ô â  äÃr[X>¾U Since, the file contained  h ¤ zÛ³Wp <ø®lŒoWP¸"Ç^1i M€ÌygUjŽæ#Eì}ÔKËëü’²ô â  äÃr[X>¾U string, I was expecting pbBlob to point to the above content ie.  h ¤ zÛ³Wp <ø®lŒoWP¸"Ç^1i M€ÌygUjŽæ#Eì}ÔKËëü’²ô â  äÃr[X>¾U But when I debug, the following line fwrite(pbBlob, 1, cbBlob, fp) shows me the value of pbBlob = "||" and cbBlob = 76 What is the problem here. How can I get the junk value in a CString ? Will some kind of casting help ?

      S Offline
      S Offline
      SandipG
      wrote on last edited by
      #2

      Go to memory address pointed by pbBlob and have a look at the contents. Watch window will not show you full contents. and dont compare it with the contents of notepad. Open the file in VC++ IDE it will show you all hex as well as text data then compare it.

      Regards, Sandip.

      R 1 Reply Last reply
      0
      • S SandipG

        Go to memory address pointed by pbBlob and have a look at the contents. Watch window will not show you full contents. and dont compare it with the contents of notepad. Open the file in VC++ IDE it will show you all hex as well as text data then compare it.

        Regards, Sandip.

        R Offline
        R Offline
        RockyJames
        wrote on last edited by
        #3

        Enable dispaly unicode strings settings in the IDE.

        S 1 Reply Last reply
        0
        • R RockyJames

          Enable dispaly unicode strings settings in the IDE.

          S Offline
          S Offline
          SandipG
          wrote on last edited by
          #4

          Is this option avaialbe in VC++6.0. Thanks a lot.

          Regards, Sandip.

          R 1 Reply Last reply
          0
          • S SandipG

            Is this option avaialbe in VC++6.0. Thanks a lot.

            Regards, Sandip.

            R Offline
            R Offline
            RockyJames
            wrote on last edited by
            #5

            yes it is available.

            1 Reply Last reply
            0
            • S SherTeks

              I have this code snippet which writes an encrypted value of a string into a file (KEY_FILE -- #defined as C:\xyz.txt) ;

              BYTE *pbBlob;
              DWORD cbBlob;

              :
              :
              :
              

              if (!CryptExportKey(hSessionKey, hKey, SIMPLEBLOB, 0, NULL, &cbBlob))
              {
              dwResult = GetLastError();
              MessageBox("Error CryptExportKey() failed.", "Information", MB_OK);
              return;
              }
              if (!CryptExportKey(hSessionKey, hKey, SIMPLEBLOB, 0, NULL, &cbBlob))
              {
              dwResult = GetLastError();
              MessageBox("Error CryptExportKey() failed.", "Information", MB_OK);
              return;
              }
              if ((pbBlob = (BYTE *) LocalAlloc(LMEM_ZEROINIT,cbBlob)) == NULL)
              {
              dwResult = ERROR_NOT_ENOUGH_MEMORY;
              MessageBox("Error LocalAlloc() failed.", "Information", MB_OK);
              return;
              }
              if (!CryptExportKey(hSessionKey, hKey, SIMPLEBLOB, 0, pbBlob, &cbBlob))
              {
              dwResult = GetLastError();
              MessageBox("Error CryptExportKey() failed.", "Information", MB_OK);
              return;
              }

              if(hSessionKey != 0)
              CryptDestroyKey(hSessionKey);

              hSessionKey = 0;

              FILE *fp = fopen(KEY_FILE, "w+b");
              if (fp) {
              fwrite(pbBlob, 1, cbBlob, fp);
              fclose(fp);
              }

              I find a junk value written by the above code in "C:\xyz.txt" which is like :  h ¤ zÛ³Wp <ø®lŒoWP¸"Ç^1i M€ÌygUjŽæ#Eì}ÔKËëü’²ô â  äÃr[X>¾U Since, the file contained  h ¤ zÛ³Wp <ø®lŒoWP¸"Ç^1i M€ÌygUjŽæ#Eì}ÔKËëü’²ô â  äÃr[X>¾U string, I was expecting pbBlob to point to the above content ie.  h ¤ zÛ³Wp <ø®lŒoWP¸"Ç^1i M€ÌygUjŽæ#Eì}ÔKËëü’²ô â  äÃr[X>¾U But when I debug, the following line fwrite(pbBlob, 1, cbBlob, fp) shows me the value of pbBlob = "||" and cbBlob = 76 What is the problem here. How can I get the junk value in a CString ? Will some kind of casting help ?

              S Offline
              S Offline
              SherTeks
              wrote on last edited by
              #6

              The following code snippet worked out for me in getting the junk value (stored in the file) into a CString value.

              	CString	csEncryptedString(\_T(""));
              	char \*pEncryptedString = new char;
              
              	for (INT i = 0; i < (int)cbBlob; i++)
              	{
              		if (pbBlob\[i\] == '\\0')
              		{
              			pEncryptedString\[i\] = '-';
              		}
              		else
              		{
              			pEncryptedString\[i\] = pbBlob\[i\];
              		}
              	}
              	pEncryptedString\[cbBlob + 1\] = '\\0';
              
              	csEncryptedString = pEncryptedString;
              

              Thanks all for your various suggestions and support. Any other efficient code is always welcome.

              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