我编写了此代码,它是对字符串进行加密并将其转换为base64字符串(以便我可以复制它)。
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]);
}
}
我有这样的字符串:
li2b5qyVGbwJw6qAIitPRFI5BbRua2W0hJ1y6YLCKOOOkvQg3i4gZbUd3vaRzKRP F5rHd9HvGTxHX6hOsJah8LfBeftf3ZBrE + F / zcGKQqcENesrxNpm1yLZDtaJdMbZ JcdNcP / JoyVuvS2Gbc0I35chzBCRafwtRLD41aIdNfxnLw9 + R4CQQ8BmKUUD8U7d 2wLIZzW + kPSWBLYw2JU69x0H8e2rA1AhJEqnGtypfeOKxTmOzvCzQQFaykp4vLyp t7MoIjArwhNqdpajKfVtu74hUmn2OgLX6FfO8x1bRs61NUnF9f2PniTzI4pyVAjx + MQioBwlro / xLjnOn4vppne6w4cbLzH8JIZC9Zqhn1LFGBE5fAlstlrv3onHfRdm yTvgLxGqFC8RdJ6uEuS10u7EBC / Whq4Ti4XpyET / ILmIHlZmK9TVICE ejqNQNODmZtqgs4 + sJLyFSZV3bkzKyIjrl9K1riAozLFdS8OeeAdbilEFpBXWLy9 5Q1xPkhRasauHMYRkv13pH58zaqcoHF / ThKHg + uPbY8dByfM / 9v + igSK / fr88S49 iHam92vIpia + 6SbJxilg + 00jpMn + ZinLweHPG / kngbENbxFE4m9HbbZC9Vk8NAbG uMiVP8kL0UL5cuKxR771sav1tXQInNuJOKZ + g9qixlg =
解密:
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");
}
答案 0 :(得分:0)
unsigned char buffer[512];
而不是unsigned char* buffer = new unsigned char[0];