# Function Reference

# _Crypt_DecryptData

Decrypts data using the supplied key

#include <Crypt.au3>
_Crypt_DecryptData ( $vData, $vCryptKey, $iAlgID [, $bFinal = True] )

## Parameters

$vData | Data to decrypt. See remark.
$vCryptKey | Password or handle to a key if the $CALG_USERKEY flag is specified.
$iAlgID | The algorithm to use. See _Crypt_DeriveKey().
$bFinal | [optional] False if this is only a segment of the full data.

## Return Value

Success: | a binary string containing decrypted data.
Failure: | sets the @error flag to non-zero.
@error: | 70 - Failed to decrypt data 10 to 30 - Cannot create key 80 - Failed to retrieve $CALG_USERKEY AlgID 1000+ _Crypt_Startup() failed

## Remarks

The decrypted data is always returned as a binary string even if the encrypted data is in fact a string (cast with BinaryToString()).
If $vData encrypted data has been called with a Binary containing UTF8 chars, BinaryToString(..., $SB_UTF8) must be called to get back a perfect UNICODE string. See Example 3.

## Related

_Crypt_DeriveKey, _Crypt_EncryptData

## See Also

Search CryptDecrypt in MSDN Library.

## Example

### Example 1

    #include <ComboConstants.au3>
    #include <Crypt.au3>
    #include <EditConstants.au3>
    #include <GUIConstantsEx.au3>
    #include <MsgBoxConstants.au3>
    #include <StringConstants.au3>
    #include <WinAPIConv.au3>
    #include <WindowsNotifsConstants.au3>

    Global $g_hKey = -1, $g_idEdt_Input = -1, $g_idEdt_Output = -1, $g_idEdt_OutputDeCrypted = -1

    Example()

    Func Example()
        Local $hGUI = GUICreate("Realtime (En/DE)cryption", 400, 470)
        $g_idEdt_Input = GUICtrlCreateEdit("", 0, 0, 400, 150, $ES_WANTRETURN)
        $g_idEdt_Output = GUICtrlCreateEdit("", 0, 150, 400, 150, $ES_READONLY)
        $g_idEdt_OutputDeCrypted = GUICtrlCreateEdit("", 0, 300, 400, 150, $ES_READONLY)
        Local $idCombo = GUICtrlCreateCombo("", 0, 450, 100, 20, $CBS_DROPDOWNLIST)
        GUICtrlSetData($idCombo, "3DES (168bit)|AES (128bit)|AES (192bit)|AES (256bit)|DES (56bit)|RC2 (128bit)|RC4 (128bit)", "RC4 (128bit)")
        GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
        GUISetState(@SW_SHOW, $hGUI)

        _Crypt_Startup() ; To optimize performance start the crypt library.

        Local $iAlgorithm = $CALG_RC4
        $g_hKey = _Crypt_DeriveKey(StringToBinary("CryptPassword"), $iAlgorithm) ; Declare a password string and algorithm to create a cryptographic key.

        While 1
            Switch GUIGetMsg()
                Case $GUI_EVENT_CLOSE
                    Exit

                Case $idCombo ; Check when the combobox is selected and retrieve the correct algorithm.
                    Switch GUICtrlRead($idCombo) ; Read the combobox selection.
                        Case "3DES (168bit)"
                            $iAlgorithm = $CALG_3DES

                        Case "AES (128bit)"
                            $iAlgorithm = $CALG_AES_128

                        Case "AES (192bit)"
                            $iAlgorithm = $CALG_AES_192

                        Case "AES (256bit)"
                            $iAlgorithm = $CALG_AES_256

                        Case "DES (56bit)"
                            $iAlgorithm = $CALG_DES

                        Case "RC2 (128bit)"
                            $iAlgorithm = $CALG_RC2

                        Case "RC4 (128bit)"
                            $iAlgorithm = $CALG_RC4

                    EndSwitch

                    _Crypt_DestroyKey($g_hKey) ; Destroy the cryptographic key.
                    $g_hKey = _Crypt_DeriveKey(StringToBinary("CryptPassword"), $iAlgorithm) ; Re-declare a password string and algorithm to create a new cryptographic key.

                    Local $sRead = GUICtrlRead($g_idEdt_Input)
                    If StringStripWS($sRead, $STR_STRIPALL) <> "" Then ; Check there is text available to encrypt.
                        Local $dEncrypted = _Crypt_EncryptData($sRead, $g_hKey, $CALG_USERKEY) ; Encrypt the text with the new cryptographic key.
                        GUICtrlSetData($g_idEdt_Output, $dEncrypted) ; Set the output box with the encrypted text.
                        Local $dDecrypted = _Crypt_DecryptData($dEncrypted, $g_hKey, $CALG_USERKEY) ; Decrypt the text with the new cryptographic key.
                        GUICtrlSetData($g_idEdt_OutputDeCrypted, BinaryToString($dDecrypted)) ; Set the output box with the encrypted text.
                    EndIf
            EndSwitch
        WEnd

        GUIDelete($hGUI) ; Delete the previous GUI and all controls.
        _Crypt_DestroyKey($g_hKey) ; Destroy the cryptographic key.
        _Crypt_Shutdown() ; Shutdown the crypt library.
    EndFunc   ;==>Example

    Func WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)
        #forceref $hWnd, $iMsg, $lParam

        Switch _WinAPI_LoWord($wParam)
            Case $g_idEdt_Input
                Switch _WinAPI_HiWord($wParam)
                    Case $EN_CHANGE
                        Local $dEncrypted = _Crypt_EncryptData(GUICtrlRead($g_idEdt_Input), $g_hKey, $CALG_USERKEY) ; Encrypt the text with the cryptographic key.
                        GUICtrlSetData($g_idEdt_Output, $dEncrypted) ; Set the output box with the encrypted text.
                        Local $dDecrypted = _Crypt_DecryptData($dEncrypted, $g_hKey, $CALG_USERKEY) ; Decrypt the text with the new cryptographic key.
                        GUICtrlSetData($g_idEdt_OutputDeCrypted, BinaryToString($dDecrypted)) ; Set the output box with the encrypted text.
                EndSwitch
        EndSwitch
    EndFunc   ;==>WM_COMMAND

### Example 2

    #include <Crypt.au3>
    #include <MsgBoxConstants.au3>

    Example()

    Func Example()
        ; Encrypt text using a generic password.
        Local $dEncrypted = StringEncrypt(True, 'Encrypt this data.', 'securepassword')

        ; Display the encrypted text.
        MsgBox($MB_SYSTEMMODAL, 'Encrypted', $dEncrypted)

        ; Decrypt the encrypted text using the generic password.
        Local $sDecrypted = StringEncrypt(False, $dEncrypted, 'securepassword')

        ; Display the decrypted text.
        MsgBox($MB_SYSTEMMODAL, 'Decrypted', $sDecrypted)
    EndFunc   ;==>Example

    Func StringEncrypt($bEncrypt, $sData, $sPassword)
        _Crypt_Startup() ; Start the Crypt library.
        Local $vReturn = ''
        If $bEncrypt Then ; If the flag is set to True then encrypt, otherwise decrypt.
            $vReturn = _Crypt_EncryptData($sData, $sPassword, $CALG_RC4)
        Else
            $vReturn = BinaryToString(_Crypt_DecryptData($sData, $sPassword, $CALG_RC4))
        EndIf
        _Crypt_Shutdown() ; Shutdown the Crypt library.
        Return $vReturn
    EndFunc   ;==>StringEncrypt

### Example 3 for Handling non ANSI String

    #include <Crypt.au3>
    #include <MsgBoxConstants.au3>
    #include <StringConstants.au3>

    Local $sPlaintext = "Hello! ជំរាបសួរ! Allô! Привет! 您好！مرحبا! હેલો! שלום! こんにちは！"

    Local $dPlaintextUTF8 = StringToBinary($sPlaintext, $SB_UTF8) ; Convert to Binary string converting Unicode char as UTF8
    ;~ $dPlaintextUTF8 = $sPlaintext ; If Uncommented willshow why UTF8 conversion is needed

    Local $iAlgorithm = $CALG_3DES
    Local $hKey = _Crypt_DeriveKey("CryptPassword", $iAlgorithm)

    Local $dEncrypted = _Crypt_EncryptData($dPlaintextUTF8, $hKey, $CALG_USERKEY) ; Encrypt the text with the new cryptographic key.

    Local $dDecrypted = _Crypt_DecryptData($dEncrypted, $hKey, $CALG_USERKEY) ; Decrypt the data using the generic password string. The return value is a binary string.

    Local $sDecrypted = BinaryToString($dDecrypted, $SB_UTF8) ; Convert the binary string using BinaryToString to display the initial data we encrypted.

    If $sPlaintext = $sDecrypted Then
        MsgBox($MB_SYSTEMMODAL, "Decrypted data", $sDecrypted)
    Else
        MsgBox($MB_SYSTEMMODAL, "BAD Decrypted data", $sPlaintext & @CRLF & "-->" & @CRLF & $sDecrypted)
    EndIf