Encrypts data using the supplied key
#include <Crypt.au3>
_Crypt_EncryptData ( $vData, $vCryptKey, $iAlgID [, $bFinal = True] )
$vData | Data to encrypt/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. |
Success: | a binary string containing encrypted data. |
Failure: | sets the @error flag to non-zero. |
@error: | 50 - Failed to determine buffer 60 - Failed to encrypt data 10 to 30 - Cannot create key 80 - Failed to retrieve $CALG_USERKEY AlgID 1000+ _Crypt_Startup() failed |
Returns a binary string regardless of input.
If $vData contains non ANSI chars it must be passed as StringToBinary(..., $SB_UTF8). See Example 3.
_Crypt_DecryptData, _Crypt_DeriveKey, _Crypt_EncryptFile
Search CryptEncrypt in MSDN Library.
#include <ComboConstants.au3>
#include <Crypt.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <StringConstants.au3>
#include <WinAPIConv.au3>
#include <WindowsConstants.au3>
Global $g_hKey = -1, $g_idInputEdit = -1, $g_idOutputEdit = -1, $g_idOutputDeCrypted = -1
Example()
Func Example()
Local $hGUI = GUICreate("Realtime (En/DE)cryption", 400, 470)
$g_idInputEdit = GUICtrlCreateEdit("", 0, 0, 400, 150, $ES_WANTRETURN)
$g_idOutputEdit = GUICtrlCreateEdit("", 0, 150, 400, 150, $ES_READONLY)
$g_idOutputDeCrypted = 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_idInputEdit)
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_idOutputEdit, $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_idOutputDeCrypted, 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_idInputEdit
Switch _WinAPI_HiWord($wParam)
Case $EN_CHANGE
Local $dEncrypted = _Crypt_EncryptData(GUICtrlRead($g_idInputEdit), $g_hKey, $CALG_USERKEY) ; Encrypt the text with the cryptographic key.
GUICtrlSetData($g_idOutputEdit, $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_idOutputDeCrypted, BinaryToString($dDecrypted)) ; Set the output box with the encrypted text.
EndSwitch
EndSwitch
EndFunc ;==>WM_COMMAND
#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
#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