DPAPI函数是CryptoAPI中少有的简单易用的加密函数,调用过程简单,其调用接口几乎不涉及密码学概念。Win32 DPAPI有4个函数,它们分别是CryptProtectData、CryptUnProtectData、CryptProtectMemory和CryptUnProtectMemory。

CryptProtectData和CryptUnProtectData有一个用户提示描述结构,应用程序可以在这里设定呈现给用户的提示对话框的出现时机和内容。代码示例如下:

//-------------------------------------------------------------------
//  初始化提示对话框结构.

ZeroMemory(&PromptStruct, sizeof(PromptStruct));
PromptStruct.cbSize = sizeof(PromptStruct);
PromptStruct.dwPromptFlags = CRYPTPROTECT_PROMPT_ON_PROTECT;
PromptStruct.szPrompt = L"这是一个用户提示。";

//  实施数据加密保护
if(CryptProtectData(
     &DataIn,
     L"This is the description string.", // 说明 
     NULL,                               // 可选附加扰码,本例不用
     NULL,                               // 保留
     &PromptStruct,                      // 传入前面准备好的提示对话框结构
     0,
     &DataOut))
{
     printf("数据加密保护完成.\n");
}
else
{
     printf("数据加密保护失败.\n");
}


//---------------------------------------------------------------------------
// 进行数据解密还原操作
if (CryptUnprotectData(
        &DataOut,
        &pDescrOut,
        NULL,                 // 附加扰码,必须与加密时所用的一致。
        NULL,                 // Reserved
        &PromptStruct,        // 提示对话框描述结构
        0,
        &DataVerify))
{
     printf("解密出来的数据是: %s\n", DataVerify.pbData);
     printf("数据的描述是: %S\n",pDescrOut);

}
else
{
    printf("解密错!");
}
posted on 2015-10-15 21:05  FUMIOT  阅读(1548)  评论(0编辑  收藏  举报