找回密码
 加入
搜索
查看: 8996|回复: 6

[效率算法] CryptProtectData和CryptUnProtectData两个windows API加密和解密[已解决]

[复制链接]
发表于 2014-7-20 02:19:39 | 显示全部楼层 |阅读模式
本帖最后由 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]);
            count++;
        }

        fclose(fp);

    }
    else
    {
        printf("Encryption error using CryptProtectData.\n");
        exit(1);
    }
}
编译时需要指定连接crypt32.lib库,所以一个最简单的编译命令为
cl rdp.cpp crypt32.lib

看看各位大大,用au3能不能做
 楼主| 发表于 2014-7-20 23:18:30 | 显示全部楼层
貌似没人来回答啊~~ 大神们~   还有P版呢?
发表于 2014-7-22 05:45:06 | 显示全部楼层
#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] = 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 

评分

参与人数 1金钱 +10 收起 理由
nmgwddj + 10 学习了

查看全部评分

 楼主| 发表于 2014-7-23 22:59:31 | 显示全部楼层
回复 3# ceoguang


    太感谢了,果真是高手~
发表于 2016-12-27 17:51:10 | 显示全部楼层
求助,WIN7下好像不行的样子
发表于 2016-12-27 17:58:55 | 显示全部楼层


找到一个,对这个东西也不太懂!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
发表于 2019-6-28 05:24:59 | 显示全部楼层
正想找这个东西,版主太牛逼了,哈哈哈哈哈
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-4-25 03:26 , Processed in 0.080815 second(s), 25 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表