3#include <winpr/print.h>
4#include <winpr/crypto.h>
8static const char* SECRET_PASSWORD_TEST =
"MySecretPassword123!";
10int TestCryptoProtectMemory(
int argc,
char* argv[])
13 UINT32 cbPlainText = 0;
14 UINT32 cbCipherText = 0;
15 const char* pPlainText =
nullptr;
16 BYTE* pCipherText =
nullptr;
21 pPlainText = SECRET_PASSWORD_TEST;
22 cbPlainText = strlen(pPlainText) + 1;
23 cbCipherText = cbPlainText +
24 (CRYPTPROTECTMEMORY_BLOCK_SIZE - (cbPlainText % CRYPTPROTECTMEMORY_BLOCK_SIZE));
25 printf(
"cbPlainText: %" PRIu32
" cbCipherText: %" PRIu32
"\n", cbPlainText, cbCipherText);
26 pCipherText = (BYTE*)malloc(cbCipherText);
29 printf(
"Unable to allocate memory\n");
32 CopyMemory(pCipherText, pPlainText, cbPlainText);
33 ZeroMemory(&pCipherText[cbPlainText], (cbCipherText - cbPlainText));
34 if (!winpr_InitializeSSL(WINPR_SSL_INIT_DEFAULT))
37 if (!CryptProtectMemory(pCipherText, cbCipherText, CRYPTPROTECTMEMORY_SAME_PROCESS))
39 printf(
"CryptProtectMemory failure\n");
43 printf(
"PlainText: %s (cbPlainText = %" PRIu32
", cbCipherText = %" PRIu32
")\n", pPlainText,
44 cbPlainText, cbCipherText);
45 winpr_HexDump(
"crypto.test", WLOG_DEBUG, pCipherText, cbCipherText);
47 if (!CryptUnprotectMemory(pCipherText, cbCipherText, CRYPTPROTECTMEMORY_SAME_PROCESS))
49 printf(
"CryptUnprotectMemory failure\n");
53 printf(
"Decrypted CipherText: %s\n", pCipherText);
54 SecureZeroMemory(pCipherText, cbCipherText);