返回> 网站首页
使用API函数CryptProtectData和CryptUnprotectData进行字符串加解密
yoours2010-05-17 10:58:42
简介一边听听音乐,一边写写文章。
#include <stdio.h>
#include <windows.h>
#include <Wincrypt.h>
#include <fstream>
#include <string>
#include <algorithm>
#include <list>
using namespace std;
void HandleError(char *s)
{
fprintf(stderr, "An error occurred in running the program. \n");
fprintf(stderr, "%s\n",s);
fprintf(stderr, "Error number %d.\n", GetLastError());
fprintf(stderr, "Program terminating. \n");
//exit(1);
return;
}
void main()
{
DATA_BLOB DataIn1;
DATA_BLOB DataOut1;
// mstsc.exe中使用的是unicode,所以必须做宽字符转换
BYTE *pbDataInput1 =(BYTE *)L"freedom";
DWORD cbDataInput1 = wcslen(L"freedom")*sizeof(wchar_t);
DataIn1.pbData = pbDataInput1;
DataIn1.cbData = cbDataInput1;
FILE *fp;
if(CryptProtectData(
&DataIn1,
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,
&DataOut1))
{
printf("The encryption phase worked.\n");
fp = fopen("password.txt","w");
int count=0;
while ( count <= (int)DataOut1.cbData ){
// 因为一个unsigned int 占32位
// 转换成成16进制要占两位
// 所以这里需要用%02
fprintf(fp,"%02X",DataOut1.pbData[count]);
count++;
}
fclose(fp);
}else{
printf("Encryption error using CryptProtectData.\n");
exit(1);
}
ifstream in("password.txt");
list<string> lst;
char buf[1024];
while(in)
{
in.getline(buf,1024);
lst.push_back(buf);
}
list<string>::iterator iter, iterBegin;
string strPwd;
iterBegin = lst.begin();
strPwd = *iterBegin;
iterBegin++;
for(iter = iterBegin; iter != lst.end(); iter++)
{
strPwd += *iter;
}
DATA_BLOB DataOut;
DATA_BLOB DataVerify;
LPWSTR pDescrOut = NULL;
BYTE *pbDataOutput;
DWORD cbDataOutput;
int data;
// Put encrypted password string here
//char *szEncodedPwd = new char[strlen(strPwd.c_str()) + 1];
//strcpy(szEncodedPwd, strPwd.c_str());
char szEncodedPwd[] = "51:b:01000000D08C9DDF0115D1118C7A00C04FC297EB010000005A95766D6AED2744AA27C4F51CCF71C70000000008000000700073007700000003660000A800000010000000738B7545A4E609D5E627D2340668C2230000000004800000A000000010000000980CFB0F5DF7B89D57AE2DA3708DE0C6080200001F933C9215C8548A2C591A0D3C659D53FE45BCB4AE770E4B0369D4EC6D9804B6C094FB7181632C3E41E35350E742FEA14308FD87D9BAA190B0253DA92CEFC2439DCEAEAEBA108E7030DBC0ACF973E28C058FB180086A21DD809CF37F463AF7BE2B0088880F706FCB6D1722368959A4F8A885257A06DBD153B735376FB0267218F1771668A660EA81188EDDDB650A314091233B99EC2FF6361BEDEC532603A86882AEBBC597836DADF40E882A2C8EC16FAB2018E73E40187BF913C307EEF9CA4A6664BD39AAD163348D67A75EB12460A1EA8840E416BAA80D5724C4F17A6CB71621F3841B1AF1B2AC40C9E4725847B7487B22A7E42542E321B84F0561FF73A1DD6B199E1DBAFB12881ABB5332B33B956AE431F4E20A61DA94175A7F1597FDF1C205E7975C8FD8CEAB78B18528EB57CB93D518880E62797E3B2226673B43B36D2BA118235F4D265344E07D195AC0B07398DB5776E524D7FEC121DFCAEBABA578D9ABA1F5A3E6DEC3DF72FC64D4516F2B2BEFEDCAFD4E21C5C53BD79F908D11E4BAC4608E2E1A3F7A0100AE324DE31EC4CC6E7B69CD768483A93C6B31F84CCFD4B2507991FCF0EC3CB45F68DBE179038574664F0FE5FE2CA9C3E790C3EFA32984DEDC2D563F23EEF4B03F2C1A34B01BDAE05FFB854821B6EFEEA0CF1EAF9AAA769D24862EFC5E3D24DD475736FDC6B70E0F9ABB21AF8B80C36D8D3F473D4AEC52B71E08F9F980014F3C14000000D2542DC2A708785AF43A4DB07FD5DB36628176D60";
// String conversion
cbDataOutput = ( (strlen(szEncodedPwd)) / 2 );
pbDataOutput = (BYTE *)malloc( cbDataOutput + 1 );
if (pbDataOutput == NULL) HandleError("Not enough memory.");
for (unsigned int i=0; i<(cbDataOutput); i++) {
sscanf(&(szEncodedPwd[i+i]), "%02x", &data);
pbDataOutput = data;
}
DataOut.pbData = pbDataOutput;
DataOut.cbData = cbDataOutput;
// Decryption
if (CryptUnprotectData(
&DataOut, // [in] Input data
&pDescrOut, // (Optional) [out] Description string
NULL, // (Optional) [in] Entropy (not used by MS)
NULL, // Reserved
NULL, // (Optional) PromptStruct
0, // Flags
&DataVerify)) // [out] Output data
{
wprintf(L"The decrypted data is: %s\n", (WCHAR *)DataVerify.pbData);
printf("The description of the data was: %S\n", pDescrOut);
}else{
HandleError("解密错误!");
}
LocalFree(pDescrOut);
free(DataOut.pbData);
LocalFree(DataVerify.pbData);
//delete [] szEncodedPwd;
system("pause");
}
#include <windows.h>
#include <Wincrypt.h>
#include <fstream>
#include <string>
#include <algorithm>
#include <list>
using namespace std;
void HandleError(char *s)
{
fprintf(stderr, "An error occurred in running the program. \n");
fprintf(stderr, "%s\n",s);
fprintf(stderr, "Error number %d.\n", GetLastError());
fprintf(stderr, "Program terminating. \n");
//exit(1);
return;
}
void main()
{
DATA_BLOB DataIn1;
DATA_BLOB DataOut1;
// mstsc.exe中使用的是unicode,所以必须做宽字符转换
BYTE *pbDataInput1 =(BYTE *)L"freedom";
DWORD cbDataInput1 = wcslen(L"freedom")*sizeof(wchar_t);
DataIn1.pbData = pbDataInput1;
DataIn1.cbData = cbDataInput1;
FILE *fp;
if(CryptProtectData(
&DataIn1,
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,
&DataOut1))
{
printf("The encryption phase worked.\n");
fp = fopen("password.txt","w");
int count=0;
while ( count <= (int)DataOut1.cbData ){
// 因为一个unsigned int 占32位
// 转换成成16进制要占两位
// 所以这里需要用%02
fprintf(fp,"%02X",DataOut1.pbData[count]);
count++;
}
fclose(fp);
}else{
printf("Encryption error using CryptProtectData.\n");
exit(1);
}
ifstream in("password.txt");
list<string> lst;
char buf[1024];
while(in)
{
in.getline(buf,1024);
lst.push_back(buf);
}
list<string>::iterator iter, iterBegin;
string strPwd;
iterBegin = lst.begin();
strPwd = *iterBegin;
iterBegin++;
for(iter = iterBegin; iter != lst.end(); iter++)
{
strPwd += *iter;
}
DATA_BLOB DataOut;
DATA_BLOB DataVerify;
LPWSTR pDescrOut = NULL;
BYTE *pbDataOutput;
DWORD cbDataOutput;
int data;
// Put encrypted password string here
//char *szEncodedPwd = new char[strlen(strPwd.c_str()) + 1];
//strcpy(szEncodedPwd, strPwd.c_str());
char szEncodedPwd[] = "51:b:01000000D08C9DDF0115D1118C7A00C04FC297EB010000005A95766D6AED2744AA27C4F51CCF71C70000000008000000700073007700000003660000A800000010000000738B7545A4E609D5E627D2340668C2230000000004800000A000000010000000980CFB0F5DF7B89D57AE2DA3708DE0C6080200001F933C9215C8548A2C591A0D3C659D53FE45BCB4AE770E4B0369D4EC6D9804B6C094FB7181632C3E41E35350E742FEA14308FD87D9BAA190B0253DA92CEFC2439DCEAEAEBA108E7030DBC0ACF973E28C058FB180086A21DD809CF37F463AF7BE2B0088880F706FCB6D1722368959A4F8A885257A06DBD153B735376FB0267218F1771668A660EA81188EDDDB650A314091233B99EC2FF6361BEDEC532603A86882AEBBC597836DADF40E882A2C8EC16FAB2018E73E40187BF913C307EEF9CA4A6664BD39AAD163348D67A75EB12460A1EA8840E416BAA80D5724C4F17A6CB71621F3841B1AF1B2AC40C9E4725847B7487B22A7E42542E321B84F0561FF73A1DD6B199E1DBAFB12881ABB5332B33B956AE431F4E20A61DA94175A7F1597FDF1C205E7975C8FD8CEAB78B18528EB57CB93D518880E62797E3B2226673B43B36D2BA118235F4D265344E07D195AC0B07398DB5776E524D7FEC121DFCAEBABA578D9ABA1F5A3E6DEC3DF72FC64D4516F2B2BEFEDCAFD4E21C5C53BD79F908D11E4BAC4608E2E1A3F7A0100AE324DE31EC4CC6E7B69CD768483A93C6B31F84CCFD4B2507991FCF0EC3CB45F68DBE179038574664F0FE5FE2CA9C3E790C3EFA32984DEDC2D563F23EEF4B03F2C1A34B01BDAE05FFB854821B6EFEEA0CF1EAF9AAA769D24862EFC5E3D24DD475736FDC6B70E0F9ABB21AF8B80C36D8D3F473D4AEC52B71E08F9F980014F3C14000000D2542DC2A708785AF43A4DB07FD5DB36628176D60";
// String conversion
cbDataOutput = ( (strlen(szEncodedPwd)) / 2 );
pbDataOutput = (BYTE *)malloc( cbDataOutput + 1 );
if (pbDataOutput == NULL) HandleError("Not enough memory.");
for (unsigned int i=0; i<(cbDataOutput); i++) {
sscanf(&(szEncodedPwd[i+i]), "%02x", &data);
pbDataOutput
}
DataOut.pbData = pbDataOutput;
DataOut.cbData = cbDataOutput;
// Decryption
if (CryptUnprotectData(
&DataOut, // [in] Input data
&pDescrOut, // (Optional) [out] Description string
NULL, // (Optional) [in] Entropy (not used by MS)
NULL, // Reserved
NULL, // (Optional) PromptStruct
0, // Flags
&DataVerify)) // [out] Output data
{
wprintf(L"The decrypted data is: %s\n", (WCHAR *)DataVerify.pbData);
printf("The description of the data was: %S\n", pDescrOut);
}else{
HandleError("解密错误!");
}
LocalFree(pDescrOut);
free(DataOut.pbData);
LocalFree(DataVerify.pbData);
//delete [] szEncodedPwd;
system("pause");
}
文章评论
1512人参与,0条评论