How can I get byte pointers to give CString values ?
-
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¸"Ç^1iM€ÌygUjŽæ#Eì}ÔKËëü’²ôâ  äÃr[X>¾U Since, the file contained h ¤ zÛ³Wp <ø®lŒoWP¸"Ç^1iM€ÌygUjŽæ#Eì}ÔKËëü’²ôâ  äÃr[X>¾U string, I was expecting pbBlob to point to the above content ie. h ¤ zÛ³Wp <ø®lŒoWP¸"Ç^1iM€Ì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 ?
-
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¸"Ç^1iM€ÌygUjŽæ#Eì}ÔKËëü’²ôâ  äÃr[X>¾U Since, the file contained h ¤ zÛ³Wp <ø®lŒoWP¸"Ç^1iM€ÌygUjŽæ#Eì}ÔKËëü’²ôâ  äÃr[X>¾U string, I was expecting pbBlob to point to the above content ie. h ¤ zÛ³Wp <ø®lŒoWP¸"Ç^1iM€Ì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 ?
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.
-
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.
Enable dispaly unicode strings settings in the IDE.
-
Enable dispaly unicode strings settings in the IDE.
-
yes it is available.
-
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¸"Ç^1iM€ÌygUjŽæ#Eì}ÔKËëü’²ôâ  äÃr[X>¾U Since, the file contained h ¤ zÛ³Wp <ø®lŒoWP¸"Ç^1iM€ÌygUjŽæ#Eì}ÔKËëü’²ôâ  äÃr[X>¾U string, I was expecting pbBlob to point to the above content ie. h ¤ zÛ³Wp <ø®lŒoWP¸"Ç^1iM€Ì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 ?
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.