Function Reference


_Crypt_EncryptData

Encrypts data using the supplied key

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

Parameters

$vData Data to encrypt/decrypt
$vCryptKey Password or handle to a key if the CALG_USERKEY flag is specified
$iAlgID The algorithm to use
$bFinal [optional] False if this is only a segment of the full data

Return Value

Success: a binary string containing encrypted data.
Failure: -1 and sets the @error flag to non-zero.
@error: 100+ - Cannot create key
20 - Failed to determine buffer
30 - Failed to encrypt data

Remarks

Returns a binary string regardless of input

Related

_Crypt_DecryptData, _Crypt_DeriveKey, _Crypt_EncryptFile

See Also

Search CryptEncrypt 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 <WinAPI.au3>
#include <WindowsConstants.au3>

Global $g_hKey = -1, $g_idInputEdit = -1, $g_idOutputEdit = -1

Example()

Func Example()
    Local $hGUI = GUICreate("Realtime Encryption", 400, 320)
    $g_idInputEdit = GUICtrlCreateEdit("", 0, 0, 400, 150, $ES_WANTRETURN)
    $g_idOutputEdit = GUICtrlCreateEdit("", 0, 150, 400, 150, $ES_READONLY)
    Local $idCombo = GUICtrlCreateCombo("", 0, 300, 100, 20, $CBS_DROPDOWNLIST)
    GUICtrlSetData($idCombo, "3DES|AES (128bit)|AES (192bit)|AES (256bit)|DES|RC2|RC4", "RC4")
    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("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"
                        $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"
                        $iAlgorithm = $CALG_DES

                    Case "RC2"
                        $iAlgorithm = $CALG_RC2

                    Case "RC4"
                        $iAlgorithm = $CALG_RC4

                EndSwitch

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

                Local $sRead = GUICtrlRead($g_idInputEdit)
                If StringStripWS($sRead, $STR_STRIPALL) <> "" Then ; Check there is text available to encrypt.
                    Local $bEncrypted = _Crypt_EncryptData($sRead, $g_hKey, $CALG_USERKEY) ; Encrypt the text with the new cryptographic key.
                    GUICtrlSetData($g_idOutputEdit, $bEncrypted) ; 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_idInputEdit
            Switch _WinAPI_HiWord($wParam)
                Case $EN_CHANGE
                    Local $bEncrypted = _Crypt_EncryptData(GUICtrlRead($g_idInputEdit), $g_hKey, $CALG_USERKEY) ; Encrypt the text with the cryptographic key.
                    GUICtrlSetData($g_idOutputEdit, $bEncrypted) ; 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 $sEncrypted = StringEncrypt(True, 'Encrypt this data.', 'securepassword')

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

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

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

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