CryptProtectData和CryptUnProtectData两个windows API加密和解密[已解决]
本帖最后由 austere 于 2014-7-23 23:25 编辑问题如标题,下面给出C的代码,求高手用au3来写个~~
#include <stdio.h>
#include <windows.h>
#include <Wincrypt.h>
void main()
{
DATA_BLOB DataIn;
DATA_BLOB DataOut;
// mstsc.exe中使用的是unicode,所以必须做宽字符转换
BYTE *pbDataInput =(BYTE *)L"freedom";
DWORD cbDataInput = wcslen(L"freedom")*sizeof(wchar_t);
DataIn.pbData = pbDataInput;
DataIn.cbData = cbDataInput;
FILE *fp;
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");
fp = fopen("password.txt","w");
int count=0;
while ( count <= (int)DataOut.cbData ){
// 因为一个unsigned int 占32位
// 转换成成16进制要占两位
// 所以这里需要用%02
fprintf(fp,"%02X",DataOut.pbData);
count++;
}
fclose(fp);
}
else
{
printf("Encryption error using CryptProtectData.\n");
exit(1);
}
}
编译时需要指定连接crypt32.lib库,所以一个最简单的编译命令为
cl rdp.cpp crypt32.lib
看看各位大大,用au3能不能做 貌似没人来回答啊~~ 大神们~ 还有P版呢?
#include <winapi.au3>
#cs
BOOL WINAPI CryptProtectData(
_In_ DATA_BLOB *pDataIn,
_In_ LPCWSTR szDataDescr,
_In_ DATA_BLOB *pOptionalEntropy,
_In_ PVOID pvReserved,
_In_opt_CRYPTPROTECT_PROMPTSTRUCT *pPromptStruct,
_In_ DWORD dwFlags,
_Out_ DATA_BLOB *pDataOut
);
#ce
Global $tagDATA_BLOB = 'DWORD cbData;PTR pbData'
Global $g_Crypt32Dll = DllOpen('Crypt32.dll')
Local $sOutText = ''
If RDP_Encrypt('freedom','psw',$sOutText) Then MsgBox(64,'Result',$sOutText)
DllClose($g_Crypt32Dll)
Func RDP_Encrypt($szDataInput,$szDataDescr,ByRef $sOut)
Local $nLen = StringLen($szDataInput) * 2
Local $tBuffer = DllStructCreate(StringFormat('wchar[%d]', $nLen+1))
Local $tDATA_BLOB = DllStructCreate($tagDATA_BLOB)
Local $tDataDescr = DllStructCreate(StringFormat('wchar[%d]',2*StringLen($szDataDescr)+1))
Local $tDataOut = DllStructCreate($tagDATA_BLOB)
Local $tBufferOut,$nRet
DllStructSetData($tBuffer, 1, $szDataInput)
DllStructSetData($tDATA_BLOB, 'cbData', $nLen)
DllStructSetData($tDATA_BLOB, 'pbData', DllStructGetPtr($tBuffer))
DllStructSetData($tDataDescr,1,$szDataDescr)
$nRet = DllCall($g_Crypt32Dll, 'BOOL', 'CryptProtectData', 'ptr', DllStructGetPtr($tDATA_BLOB), 'ptr', DllStructGetPtr($tDataDescr), 'ptr', 0, 'ptr', 0, 'ptr', 0, 'DWORD', 0, 'ptr', DllStructGetPtr($tDataOut))
If @error Then
ConsoleWrite(StringFormat('DllCall Error:%d\n', @error))
Return False
EndIf
If $nRet = 0 Then
ConsoleWrite(StringFormat('call CryptProtectData Error:%d\nErrorMessage:%s\n', _WinAPI_GetLastError(),_WinAPI_GetLastErrorMessage()))
Return False
EndIf
$tBufferOut = DllStructCreate(StringFormat('BYTE[%d]',DllStructGetData($tDataOut,'cbData')),DllStructGetData($tDataOut,'pbData'))
$sOut = ''
For $i = 0 To DllStructGetSize($tBufferOut)
$sOut &= StringFormat('%02X',DllStructGetData($tBufferOut,1,$i+1))
Next
_WinAPI_LocalFree(DllStructGetData($tDataOut,'pbData'));When you have finished using the DATA_BLOB structure, free its pbData member by calling the LocalFree function.
Return True
EndFunc
回复 3# ceoguang
太感谢了,果真是高手~ 求助,WIN7下好像不行的样子
找到一个,对这个东西也不太懂! 正想找这个东西,版主太牛逼了,哈哈哈哈哈
页:
[1]