Programming/C

[Win API] CryptProtectData example

Tribal 2018. 8. 6. 17:01



Source Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#pragma comment(lib, "crypt32.lib")
 
#include <stdio.h>
#include <Windows.h>
#include <wincrypt.h>
 
void hexdump(unsigned char *p, unsigned int len)
{
    unsigned char *line = p;
    unsigned int i, thisline, offset = 0;
 
    while (offset < len)
    {
        printf("%04x ", offset);
        thisline = len - offset;
        if (thisline > 16)
            thisline = 16;
 
        for (i = 0; i < thisline; i++)
            printf("%02x ", line[i]);
 
        for (; i < 16; i++)
            printf("   ");
 
        for (i = 0; i < thisline; i++)
            printf("%c", (line[i] >= 0x20 && line[i] < 0x7f) ? line[i] : '.');
 
        printf("\n");
        offset += thisline;
        line += thisline;
    }
}
 
int main(void) {
    unsigned char *cDataIn = (unsigned char*)"Hello World!";
    DATA_BLOB dDataIn, dDataOut, dDataVerify;
    LPWSTR pDescrOut = NULL;
 
    dDataIn.cbData = strlen(cDataIn) + 1;
    dDataIn.pbData = cDataIn;
 
    CryptProtectData(&dDataIn, L"Tribal"NULL00, CRYPTPROTECT_LOCAL_MACHINE, &dDataOut);
    CryptProtectData(&dDataIn, L"Tribal"NULL000&dDataOut);
    CryptUnprotectData(&dDataOut, &pDescrOut, NULLNULLNULL, CRYPTPROTECT_UI_FORBIDDEN, &dDataVerify);
 
    hexdump(dDataOut.pbData, dDataOut.cbData);
    hexdump(dDataVerify.pbData, dDataVerify.cbData);
 
    return 0;
}
cs



Result