CryptProtectData/CryptUnprotectData加解密

#include "stdafx.h"
#include <string>

#include <WTypes.h>

#include <stdio.h>
#include <windows.h>
#include <Wincrypt.h>
#include <sstream>

 

using namespace std;

const char* Hex2ASC( const BYTE* Hex, int Len)
{
    static char ASC[4096 * 2];
    int i; 

    for (i = 0; i < Len; i++)
    {
        ASC[i * 2] = "0123456789ABCDEF"[Hex[i] >> 4];
        ASC[i * 2 + 1] = "0123456789ABCDEF"[Hex[i] & 0x0F];
    }
    ASC[i * 2] = '\0';

    return ASC;
}

std::string Hex2ASC_( const BYTE* Hex, int Len)
{
    std::string str;
    char ASC[3] = {0};
    ASC[2] = '\0';
    int i; 

    for (i = 0; i < Len; i++)
    {
        ASC[0] = "0123456789ABCDEF"[Hex[i] >> 4];
        ASC[1] = "0123456789ABCDEF"[Hex[i] & 0x0F];
        str += ASC;
    }

    return str;
}

int HexStringToByte(LPCSTR lpHex, LPBYTE* ppByte)
{
    std::string straHex = lpHex;
    int nHexLen = straHex.size();
//    assert(nHexLen%2 == 0);
    LPBYTE lpByte = new byte[nHexLen/2];
    int nCount = 0;
    for (int i=0; i<nHexLen/2; i++)
    {
        std::stringstream ss;
        DWORD bb;
        ss << straHex.at(i*2);
        ss << straHex.at(i*2+1);
        ss << '\0';
        ss >> std::hex >> bb;
        lpByte[i] = bb;
        nCount++;
    }
    *ppByte = lpByte;
    return nCount;
}

std::string ProtectData(LPSTR lpszText)
{
    DATA_BLOB DataIn;
    DATA_BLOB DataOut;
    // mstsc.exe中使用的是unicode,所以必须做宽字符转换
    BYTE *pbDataInput =(BYTE *)lpszText;
    DWORD cbDataInput = strlen(lpszText)*sizeof(char);

    DataIn.pbData = pbDataInput;
    DataIn.cbData = cbDataInput;

    std::string strPrt;

    if(CryptProtectData(
        &DataIn,
        L"psw",                                // A description string
        // to be included with the
        // encrypted data.
        NULL,                               // Optional entropy not used.
        NULL,                               // Reserved.
        NULL,                               // Pass NULL for the
        // prompt structure.
        0,
        &DataOut))
    {
        printf("The encryption phase worked.\n");

        strPrt = Hex2ASC(DataOut.pbData, DataOut.cbData);

        LocalFree(DataOut.pbData);

    }
    return strPrt;
}

std::string UnProtectData(LPCSTR lpstr)
{
    DATA_BLOB DataOut;
    DATA_BLOB DataUnp;
    LPBYTE lpByte = NULL;
    int nLen = HexStringToByte(lpstr, &lpByte);
    DataUnp.cbData = nLen;
    DataUnp.pbData = lpByte;
    // mstsc.exe中使用的是unicode,所以必须做宽字符转换
    LPWSTR lpwstr = NULL;
    BOOL bRet = CryptUnprotectData(&DataUnp, &lpwstr, NULL, NULL, NULL, 0, &DataOut);
    LPSTR lpStrOut = new char[DataOut.cbData+1];
    memcpy(lpStrOut, DataOut.pbData, DataOut.cbData);
    lpStrOut[DataOut.cbData] = 0;


    LocalFree(DataOut.pbData); 

    if (lpwstr)
    {
        LocalFree((HLOCAL)lpwstr);
    }
    if (lpByte)
    {
        delete[] lpByte;
    }

    std::string strRet = lpStrOut;
    delete[] lpStrOut;
    return strRet;

}

void main()
{
    std::string strPrt = ProtectData("freedom!!!");
    std::string strUnPrt = UnProtectData(strPrt.c_str());


//#define CONSTSTR "01000000D08C9DDF0115D1118C7A00C04FC297EB01000000B373EAAE3C9F3746B428C797A5742CBF0000000008000000700073007700000003660000A800000010000000F762A0B543ABC4D2E3E1ECB3706875760000000004800000A0000000100000006B5B19F40B51D8FA9BFB88FE2555784D08000000757F1FBD89068ED814000000BEF2822C2A836556E9320BD065870D0242A051F6"
//    std::string strUnPrt = UnProtectData(CONSTSTR);
//    std::string strUnPrt1 = UnProtectData(CONSTSTR);
//    std::string strUnPrt2 = UnProtectData(CONSTSTR);
//    std::string strUnPrt3 = UnProtectData(CONSTSTR);
}


ref:http://msdn.microsoft.com/en-us/library/ms995355.aspx 

posted on 2009-01-21 19:35  阿彪  阅读(2352)  评论(0编辑  收藏  举报

导航