`
helpbs
  • 浏览: 1165714 次
文章分类
社区版块
存档分类
最新评论

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);

}

分享到:
评论

相关推荐

    易语言rdp加解密

    易语言rdp加解密源码,rdp加解密,RDP加密,RDP解密,Ansi转Unicode,指针到文本_Unicode版,Unicode转Ansi,CryptProtectData,CryptUnprotectData,lstrcpyn_字节,LocalFree,sprintf,sscanf,lstrcpyn_文本,GlobalAlloc,...

    C++解密Chrome80版本数据库的方法示例代码

    DPAPI是Windows系统级对数据进行加解密的一种接口无需自实现加解密代码微软已经提供了经过验证的高质量加解密算法提供了用户态的接口对密钥的推导存储数据加解密实现透明并提供较高的安全保证 DPAPI提供了两个用户态...

    易语言主机远程连接

    易语言主机远程连接源码,主机远程连接,加密_解密_rc4,取格文本,读入列表,取Crypt密码,十到十六,编码_Ansi转Unicode,刷新列表,保存列表,检测延迟,API_MultiByteToWideChar,CryptProtectData,加载皮肤,socket,inet_...

    Remote Desktop Connection Manager v2.83

    Passwords are stored securely by encrypting with either CryptProtectData using the (locally) logged on user's authority or an X509 certificate. User with OS versions prior to Win7/Vista will need to...

    ChromeBackup2:ChromeBackup2

    然后使用Windows DPAPI(“ CryptProtectData”)功能对其进行加密。 对于此加密密钥,它将在开头插入签名“ DPAPI”以进行识别。 最后,此密钥使用Base64编码,并存储在“用户数据”文件夹上方的“本地状

    易语言3389客户资料添加

    3389客户资料添加,获取配置,删除客户资料分组,执法机关删除客户数据,超级列表框数据展示,从新配置,配置丢失模版,修改客户机器,配置登录模版,取Crypt密码,十到十六,编码_Ansi转Unicode,CryptProtectData,wvsprintf,...

Global site tag (gtag.js) - Google Analytics