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