2014-02-01 20:52:04 +04:00
|
|
|
|
|
|
|
#include <winpr/crt.h>
|
|
|
|
#include <winpr/print.h>
|
|
|
|
#include <winpr/crypto.h>
|
2014-08-18 19:22:22 +04:00
|
|
|
#include <winpr/wlog.h>
|
2014-02-01 20:52:04 +04:00
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
static const char* SECRET_PASSWORD_TEST = "MySecretPassword123!";
|
2014-02-01 20:52:04 +04:00
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
int TestCryptoProtectMemory(int argc, char* argv[])
|
2014-02-01 20:52:04 +04:00
|
|
|
{
|
|
|
|
int cbPlainText;
|
|
|
|
int cbCipherText;
|
2014-08-18 21:34:47 +04:00
|
|
|
char* pPlainText;
|
|
|
|
BYTE* pCipherText;
|
|
|
|
pPlainText = (char*) SECRET_PASSWORD_TEST;
|
2014-02-01 20:52:04 +04:00
|
|
|
cbPlainText = strlen(pPlainText) + 1;
|
|
|
|
cbCipherText = cbPlainText + (CRYPTPROTECTMEMORY_BLOCK_SIZE - (cbPlainText % CRYPTPROTECTMEMORY_BLOCK_SIZE));
|
|
|
|
printf("cbPlainText: %d cbCipherText: %d\n", cbPlainText, cbCipherText);
|
2014-08-18 21:34:47 +04:00
|
|
|
pCipherText = (BYTE*) malloc(cbCipherText);
|
2015-04-03 17:21:01 +03:00
|
|
|
if (!pCipherText)
|
|
|
|
{
|
|
|
|
printf("Unable to allocate memory\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2014-02-01 20:52:04 +04:00
|
|
|
CopyMemory(pCipherText, pPlainText, cbPlainText);
|
|
|
|
ZeroMemory(&pCipherText[cbPlainText], (cbCipherText - cbPlainText));
|
|
|
|
|
|
|
|
if (!CryptProtectMemory(pCipherText, cbCipherText, CRYPTPROTECTMEMORY_SAME_PROCESS))
|
|
|
|
{
|
|
|
|
printf("CryptProtectMemory failure\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("PlainText: %s (cbPlainText = %d, cbCipherText = %d)\n", pPlainText, cbPlainText, cbCipherText);
|
2014-08-18 19:22:22 +04:00
|
|
|
winpr_HexDump("crypto.test", WLOG_DEBUG, pCipherText, cbCipherText);
|
2014-02-01 20:52:04 +04:00
|
|
|
|
|
|
|
if (!CryptUnprotectMemory(pCipherText, cbCipherText, CRYPTPROTECTMEMORY_SAME_PROCESS))
|
|
|
|
{
|
|
|
|
printf("CryptUnprotectMemory failure\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Decrypted CipherText: %s\n", pCipherText);
|
|
|
|
SecureZeroMemory(pCipherText, cbCipherText);
|
|
|
|
free(pCipherText);
|
|
|
|
return 0;
|
|
|
|
}
|