0

I have made this code, it's encrypting string and converting it to base64 string (so I can copy it).

void EncryptString(std::string data)
{
DWORD strlen = data.size() * sizeof(char);

unsigned char* buffer = new unsigned char[0];

buffer = (LPBYTE)malloc(strlen);

std::copy(data.begin(), data.end(), buffer);

DWORD lenght = 0;

if (!CryptEncrypt(hKey, 0, TRUE, 0, NULL, &lenght, NULL))
{
    ExitThread(0);
}

buffer = (LPBYTE)malloc(lenght);

if (!CryptEncrypt(hKey, 0, TRUE, 0, buffer, &strlen, lenght))
{
    ExitThread(0);
}

DWORD dlen2 = 0;
if (!CryptBinaryToString(buffer, lenght, CRYPT_STRING_BASE64, NULL, &dlen2))
{
    ExitThread(0);
}

TCHAR* tempo = new TCHAR[dlen2];

if (!CryptBinaryToString(buffer, lenght, CRYPT_STRING_BASE64, tempo, &dlen2))
{
    ExitThread(0);
}

for (int i = 0; i < dlen2; i++) 
{
    printf("%c", tempo[i]);
}
}

I got string like this:

li2b5qyVGbwJw6qAIitPRFI5BbRua2W0hJ1y6YLCKOOOkvQg3i4gZbUd3vaRzKRP F5rHd9HvGTxHX6hOsJah8LfBeftf3ZBrE+F/zcGKQqcENesrxNpm1yLZDtaJdMbZ JcdNcP/JoyVuvS2Gbc0I35chzBCRafwtRLD41aIdNfxnLw9+R4CQQ8BmKUUD8U7d 2wLIZzW+kPSWBLYw2JU69x0H8e2rA1AhJEqnGtypfeOKxTmOzvCzQQFaykp4vLyp t7MoIjArwhNqdpajKfVtu74hUmn2OgLX6FfO8x1bRs61NUnF9f2PniTzI4pyVAjx +MQioBwlro/xLjnOn4vppne6w4cbLzH8JIZC9Zqhn1LFGBE5fAlstlrv3onHfRdm yTvgLxGqFC8RdJ6uEuS10u7EBC/Whq4Ti4XpyET/ILmIHlZmK9mVICE7c95DirTS ejqNQNODmZtqgs4+sJLyFSZV3bkzKyIjrl9K1riAozLFdS8OeeAdbilEFpBXWLy9 5Q1xPkhRasauHMYRkv13pH58zaqcoHF/ThKHg+uPbY8dByfM/9v+igSK/fr88S49 iHam92vIpia+6SbJxilg+00jpMn+ZinLweHPG/kngbENbxFE4m9HbbZC9Vk8NAbG uMiVP8kL0UL5cuKxR771sav1tXQInNuJOKZ+g9qixlg=

When I try to decrypt it, I'm getting string like this: pic What I'm doing wrong?

Decrypting:

void Decrypt()
{
DWORD dlen = 0;
if (!CryptStringToBinary(dataKey, strlen(dataKey), CRYPT_STRING_BASE64, NULL, &dlen, NULL, NULL))
{
    printf("Error while getting size... %d\n", GetLastError());
    exit(0);
}

BYTE* temp;
if (!(temp = (LPBYTE)malloc(dlen)))
{
    printf("Out of memory %d\n", GetLastError());
    exit(0);
}

if (!CryptStringToBinary(dataKey, strlen(dataKey), CRYPT_STRING_BASE64, temp, &dlen, NULL, NULL))
{
    printf("Error while converting to base64 string... %d\n", GetLastError());
    exit(0);
}

if (!CryptDecrypt(hKey, NULL, true, 0, temp, &dlen))
{
    printf("Can't get lenght! %d", GetLastError());
    exit(0);
}

for (DWORD i = 0; i < dlen; i++)
{
    printf("%c", temp[i]);
}
printf("\n");
}
veter0
  • 23
  • 6
  • 1
    What is the type of `TCHAR`? Why are you not using `char`? – paddy Jul 19 '19 at 06:30
  • @paddy more comfortable – veter0 Jul 19 '19 at 06:32
  • 1
    You did not answer my first question. – paddy Jul 19 '19 at 06:34
  • 1
    Unrelated: I was once tormented by the `strlen` function implemented as a a macro and making a variable names `strlen`. It was a painful experience that took several hours to figure out. The error messages were ... they still haunt my dreams. – user4581301 Jul 19 '19 at 06:35
  • It puzzles me, if the question is about why decryption is broken, why the function to _decrypt_ is not shown here. – paddy Jul 19 '19 at 06:37
  • `unsigned char* buffer = new unsigned char[0];` seems to be odd. [Allocating a buffer of 0 length doesn't do much](https://stackoverflow.com/questions/1087042/c-new-int0-will-it-allocate-memory) Consider using `unsigned char* buffer = (LPBYTE)malloc(strlen);` – user4581301 Jul 19 '19 at 06:38
  • @paddy I use tchar instead of char because it's more comfortable for me – veter0 Jul 19 '19 at 06:41
  • @user4581301, it won't help( – veter0 Jul 19 '19 at 06:42
  • 1
    Maybe you're confused about which of my two questions is the first. You've answered my second question twice now. `TCHAR` is typically defined as `wchar_t`. – paddy Jul 19 '19 at 06:42

1 Answers1

0

unsigned char buffer[512]; instead of unsigned char* buffer = new unsigned char[0];

veter0
  • 23
  • 6