首页 > Windows开发 > 详细

Win32 DPAPI加密编程

时间:2015-10-15 22:09:12      阅读:406      评论:0      收藏:0      [点我收藏+]

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("解密错!");
}

Win32 DPAPI加密编程

原文:http://www.cnblogs.com/kodefun/p/4883671.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!