10 Years of Service
66%
[HIDE-THANKS]
[/HIDE-THANKS]
Code:
>#include
;When this flag is set, it associates the data encrypted with the current computer instead of with an individual user.
;Any user on the computer on which CryptProtectData is called can use CryptUnprotectData to decrypt the data.
Global Const $CRYPTPROTECT_LOCAL_MACHINE = 0x4
;This flag is used for remote situations where presenting a user interface (UI) is not an option. When this flag is set
;and a UI is specified for either the protect or unprotect operation, the operation fails and GetLastError returns the ERROR_PASSWORD_RESTRICTION code.
Global Const $CRYPTPROTECT_UI_FORBIDDEN = 0x1
;This flag generates an audit on protect and unprotect operations.
Global Const $CRYPTPROTECT_LOCAL_AUDIT = 0x10
Global Const $CRYPTPROTECT_VERIFY_PROTECTION = 0x40
;This flag is used to provide the prompt for the protect phase.
Global Const $CRYPTPROTECT_PROMPT_ON_PROTECT = 0x2
;This flag can be combined with CRYPTPROTECT_PROMPT_ON_PROTECT to enforce the UI (user interface) policy of the caller.
;When CryptUnprotectData is called, the dwPromptFlags specified in the CryptProtectData call are enforced.
Global Const $CRYPTPROTECT_PROMPT_ON_UNPROTECT = 0x1
Global Const $ERROR_INVALID_DATA = 13
Global Const $tagDATA_BLOB = "DWORD cbData;ptr pbData;"
Global Const $tagCRYPTPROTECT_PROMPTSTRUCT = "DWORD cbSize;DWORD dwPromptFlags;HWND hwndApp;ptr szPrompt;"
Global $hDLL_CryptProtect = DllOpen("crypt32.dll")
Global $sString2Hide = "This is a test string to protect!"
Global $bData, $sData, $sDesc = ""
$bData = _CryptProtectData($sString2Hide, "Some information")
ConsoleWrite("Error protecting: " & @error & " - " & @extended & @LF)
ConsoleWrite("Protected data: " & $bData & @LF)
$sData = _CryptUnprotectData($bData, $sDesc)
ConsoleWrite("Error unprotecting: " & @error & " - " & @extended & @LF)
ConsoleWrite("Unprotected string: " & $sData & @LF)
ConsoleWrite("Unprotected description: " & $sDesc & @LF)
ConsoleWrite(@LF & @LF)
$bData = _CryptProtectData($sString2Hide, "Some other information", "pass")
ConsoleWrite("Error protecting: " & @error & " - " & @extended & @LF)
ConsoleWrite("Protected data: " & $bData & @LF)
$sData = _CryptUnprotectData($bData, $sDesc, "")
ConsoleWrite("Error unprotecting: " & @error & " - " & @extended & @LF)
ConsoleWrite("Unprotected string: " & $sData & @LF)
ConsoleWrite("Unprotected description: " & $sDesc & @LF)
ConsoleWrite(@LF & @LF)
$bData = _CryptProtectData($sString2Hide, "Some other information", "pwd")
ConsoleWrite("Error protecting: " & @error & " - " & @extended & @LF)
ConsoleWrite("Protected data: " & $bData & @LF)
$sData = _CryptUnprotectData($bData, $sDesc, "pwd")
ConsoleWrite("Error unprotecting: " & @error & " - " & @extended & @LF)
ConsoleWrite("Unprotected string: " & $sData & @LF)
ConsoleWrite("Unprotected description: " & $sDesc & @LF)
ConsoleWrite(@LF & @LF)
Global $sPromptString = "Data protection will be done"
Global $tPromptString = DllStructCreate("wchar szPrompt[256]")
DllStructSetData($tPromptString, "szPrompt", $sPromptString)
Global $tPrompt = DllStructCreate($tagCRYPTPROTECT_PROMPTSTRUCT)
DllStructSetData($tPrompt, "cbSize", DllStructGetSize($tPrompt))
DllStructSetData($tPrompt, "dwPromptFlags", BitOR($CRYPTPROTECT_PROMPT_ON_PROTECT, $CRYPTPROTECT_PROMPT_ON_UNPROTECT))
DllStructSetData($tPrompt, "szPrompt", DllStructGetPtr($tPromptString))
$bData = _CryptProtectData($sString2Hide, "Protection example with Gui", "pwd", 0, DllStructGetPtr($tPrompt))
ConsoleWrite("Error protecting: " & @error & " - " & @extended & @LF)
ConsoleWrite("Protected data: " & $bData & @LF)
$sPromptString = "Data unprotection will be done"
DllStructSetData($tPromptString, "szPrompt", $sPromptString)
$sData = _CryptUnprotectData($bData, $sDesc, "pwd", 0, DllStructGetPtr($tPrompt))
ConsoleWrite("Error unprotecting: " & @error & " - " & @extended & @LF)
ConsoleWrite("Unprotected string: " & $sData & @LF)
ConsoleWrite("Unprotected description: " & $sDesc & @LF)
DllClose($hDLL_CryptProtect)
;http://msdn.microsoft.com/en-us/library/aa380261(v=vs.85).aspx
Func _CryptProtectData($sString, $sDesc = "", $sPwd = "", $iFlag = 0, $pPrompt = 0)
;funkey 2014.08.11th
Local $aRet, $iError, $tEntropy, $tDesc, $pEntropy = 0, $pDesc = 0
Local $tDataIn = _DataToBlob($sString)
If $sPwd <> "" Then
$tEntropy = _DataToBlob($sPwd)
$pEntropy = DllStructGetPtr($tEntropy)
EndIf
If $sDesc <> "" Then
$tDesc = DllStructCreate("wchar desc[" & StringLen($sDesc) + 1 & "]")
DllStructSetData($tDesc, "desc", $sDesc)
$pDesc = DllStructGetPtr($tDesc)
EndIf
Local $tDataBuf = DllStructCreate($tagDATA_BLOB)
$aRet = DllCall($hDLL_CryptProtect, "BOOL", "CryptProtectData", "struct*", $tDataIn, "ptr", $pDesc, "ptr", $pEntropy, "ptr", 0, "ptr", $pPrompt, "DWORD", $iFlag, "struct*", $tDataBuf)
$iError = @error
_WinAPI_LocalFree(DllStructGetData($tDataIn, "pbData"))
If $sPwd <> "" Then _WinAPI_LocalFree(DllStructGetData($tEntropy, "pbData"))
If $iError Then Return SetError(1, 0, "")
If $aRet[0] = 0 Then Return SetError(2, _WinAPI_GetLastError(), "")
Local $tDataOut = DllStructCreate("byte data[" & DllStructGetData($tDataBuf, "cbData") & "]", DllStructGetData($tDataBuf, "pbData"))
Local $bData = DllStructGetData($tDataOut, "data")
_WinAPI_LocalFree(DllStructGetData($tDataBuf, "pbData"))
Return $bData
EndFunc ;==>_CryptProtectData
;http://msdn.microsoft.com/en-us/library/aa380882(v=vs.85).aspx
Func _CryptUnprotectData($bData, ByRef $sDesc, $sPwd = "", $iFlag = 0, $pPrompt = 0)
;funkey 2014.08.11th
Local $aRet, $iError, $tEntropy, $pEntropy = 0
Local $tDataIn = _DataToBlob($bData)
$sDesc = ""
If $sPwd <> "" Then
$tEntropy = _DataToBlob($sPwd)
$pEntropy = DllStructGetPtr($tEntropy)
EndIf
Local $tDataBuf = DllStructCreate($tagDATA_BLOB)
Local $tDesc = DllStructCreate("ptr desc")
Local $pDesc = DllStructGetPtr($tDesc)
$aRet = DllCall($hDLL_CryptProtect, "BOOL", "CryptUnprotectData", "struct*", $tDataIn, "ptr*", $pDesc, "ptr", $pEntropy, "ptr", 0, "ptr", $pPrompt, "DWORD", $iFlag, "struct*", $tDataBuf)
$iError = @error
_WinAPI_LocalFree(DllStructGetData($tDataIn, "pbData"))
If $sPwd <> "" Then _WinAPI_LocalFree(DllStructGetData($tEntropy, "pbData"))
If $iError Then Return SetError(1, 0, "")
If $aRet[0] = 0 Then Return SetError(2, _WinAPI_GetLastError(), "")
Local $tDataOut = DllStructCreate("char data[" & DllStructGetData($tDataBuf, "cbData") & "]", DllStructGetData($tDataBuf, "pbData"))
Local $sData = DllStructGetData($tDataOut, "data")
Local $aLen = DllCall("msvcrt.dll", "UINT:cdecl", "wcslen", "ptr", $aRet[2])
Local $tDesc = DllStructCreate("wchar desc[" & $aLen[0] + 1 & "]", $aRet[2])
$sDesc = DllStructGetData($tDesc, "desc")
_WinAPI_LocalFree($aRet[2])
_WinAPI_LocalFree(DllStructGetData($tDataBuf, "pbData"))
Return $sData
EndFunc ;==>_CryptUnprotectData
;Creates a DATA_BLOB structure where the function stores the decrypted data.
;When you have finished using the DATA_BLOB structure, free its pbData member by calling the _WinAPI_LocalFree function.
Func _DataToBlob($data)
;funkey 2014.08.11th
Local $iLen, $tDataIn, $tData, $aMem
Local Const $LMEM_ZEROINIT = 0x40
Select
Case IsString($data)
$iLen = StringLen($data)
Case IsBinary($data)
$iLen = BinaryLen($data)
Case Else
Return SetError(1, 0, 0)
EndSelect
$tDataIn = DllStructCreate($tagDATA_BLOB)
$aMem = DllCall("Kernel32.dll", "handle", "LocalAlloc", "UINT", $LMEM_ZEROINIT, "UINT", $iLen)
$tData = DllStructCreate("byte[" & $iLen & "]", $aMem[0])
DllStructSetData($tData, 1, $data)
DllStructSetData($tDataIn, "cbData", $iLen)
DllStructSetData($tDataIn, "pbData", DllStructGetPtr($tData))
Return $tDataIn
EndFunc ;==>_DataToBlob