Chiffre les donn�es � l'aide de la cl� fournie
#include <Crypt.au3>
_Crypt_EncryptData ( $vData, $vCryptKey, $iAlgID [, $bFinal = True] )
$vData | Donn�es � chiffrer/d�chiffrer |
$vCryptKey | Mot de passe ou handle d'une cl� si le flag CALG_USERKEY est sp�cifi� |
$iAlgID | L'algorithme � utiliser |
$bFinal | [optionnel] False si c'est seulement un segment de donn�es complet |
Succ�s: | Retourne une cha�ne binaire contenant les donn�es chiffr�es. |
�chec: | Retourne -1 et d�finit @error <> 0. |
@error: | 50 - Impossible de d�terminer la m�moire tampon 60 - Impossible de chiffrer les donn�es 10 to 30 - Ne peut cr�er la cl� 80 - Impossible de r�cup�rer $CALG_USERKEY AlgID 1000+ Erreur dans _Crypt_Startup() |
Retourne une cha�ne binaire quel que soit l'entr�e
Si $vData contient des caract�res non ANSI il doit �tre pass� comme StringToBinary(..., $SB_UTF8). Voir Exemple 3.
_Crypt_DecryptData, _Crypt_DeriveKey, _Crypt_EncryptFile
Recherchez CryptEncrypt dans la biblioth�que MSDN.
#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() ; Pour optimiser les performances, d�marre la librarie crypt. Local $iAlgorithm = $CALG_RC4 $g_hKey = _Crypt_DeriveKey(StringToBinary("CryptPassword"), $iAlgorithm) ; D�clare un mot de passe et l'algorithme pour cr�er une cl� cryptographique. Local $iMsg = GUIGetMsg() While $iMsg <> $GUI_EVENT_CLOSE If $iMsg = $idCombo Then ; Teste si la combobox est s�lectionn�e et r�cup�re l'algorithme correct. Switch GUICtrlRead($idCombo) ; Lit la s�lection de la combobox. 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) ; D�truit la cl� cryptographique. $g_hKey = _Crypt_DeriveKey(StringToBinary("CryptPassword"), $iAlgorithm) ; Re-d�clare un mot de passe et l'algorithme pour cr�er une nouvelle cl� cryptographique. Local $sRead = GUICtrlRead($g_idInputEdit) If StringStripWS($sRead, $STR_STRIPALL) <> "" Then ; Teste s'il y a du texte disponible � chiffrer. Local $dEncrypted = _Crypt_EncryptData($sRead, $g_hKey, $CALG_USERKEY) ; Chiffre le texte avec la nouvelle cl� cryptographique. GUICtrlSetData($g_idOutputEdit, $dEncrypted) ; D�finit la bo�te de sortie avec le texte chiffr�. Local $dDecrypted = _Crypt_DecryptData($dEncrypted, $g_hKey, $CALG_USERKEY) ; D�chiffre le texte avec la nouvelle cl� cryptographique. GUICtrlSetData($g_idOutputDeCrypted, BinaryToString($dDecrypted)) ; D�finit la bo�te de sortie avec le texte chiffr�. EndIf EndIf $iMsg = GUIGetMsg() WEnd GUIDelete($hGUI) ; Supprime la GUI et tous ses contr�les. _Crypt_DestroyKey($g_hKey) ; D�truit la cl� cryptographique. _Crypt_Shutdown() ; Arr�te la librairie crypt. EndFunc ;==>Example Func WM_COMMAND($hWnd, $iMsg, $wParam, $lParam) #forceref $hWnd, $iMsg, $lParam If _WinAPI_LoWord($wParam) = $g_idInputEdit Then If _WinAPI_HiWord($wParam) = $EN_CHANGE Then Local $dEncrypted = _Crypt_EncryptData(GUICtrlRead($g_idInputEdit), $g_hKey, $CALG_USERKEY) ; Chiffre le texte avec la cl� cryptographique. GUICtrlSetData($g_idOutputEdit, $dEncrypted) ; D�finit la bo�te de sortie avec le texte chiffr�. Local $dDecrypted = _Crypt_DecryptData($dEncrypted, $g_hKey, $CALG_USERKEY) ; D�chiffre le texte avec la nouvelle cl� cryptographique. GUICtrlSetData($g_idOutputDeCrypted, BinaryToString($dDecrypted)) ; D�finit la bo�te de sortie avec le texte chiffr��. EndIf EndIf EndFunc ;==>WM_COMMAND
#include <Crypt.au3> #include <MsgBoxConstants.au3> Example() Func Example() ; Chiffre du texte en utilisant un mot de passe g�n�rique. Local $dEncrypted = StringEncrypt(True, 'Encrypt this data.', 'securepassword') ; Affiche le texte chiffr�. MsgBox($MB_SYSTEMMODAL, 'Chiffr�', $dEncrypted) ; D�chiffre le texte chiffr� en utilisant le mot de passe g�n�rique. Local $sDecrypted = StringEncrypt(False, $dEncrypted, 'securepassword') ; Affiche le texte d�chiffr�. MsgBox($MB_SYSTEMMODAL, 'D�chiffr�', $sDecrypted) EndFunc ;==>Example Func StringEncrypt($bEncrypt, $sData, $sPassword) _Crypt_Startup() ; D�marre la librairie Crypt. Local $vReturn = '' If $bEncrypt Then ; Si le flag est d�fini � True alors chiffre, sinon d�chiffre. $vReturn = _Crypt_EncryptData($sData, $sPassword, $CALG_RC4) Else $vReturn = BinaryToString(_Crypt_DecryptData($sData, $sPassword, $CALG_RC4)) EndIf _Crypt_Shutdown() ; Arr�te la librairie Crypt. Return $vReturn EndFunc ;==>StringEncrypt
; Exemple pour traiter une cha�ne non ANSI #include <Crypt.au3> #include <MsgBoxConstants.au3> #include <StringConstants.au3> Local $sPlaintext = "Hello! ជំរាបសួរ! All�! Привет! 您好!مرحبا! હેલો! שלום! こんにちは!" Local $dPlaintextUTF8 = StringToBinary($sPlaintext, $SB_UTF8) ; Convertit en cha�ne Binary en convertissant les caract�res Unicode en UTF8 ;~ $dPlaintextUTF8 = $sPlaintext ; Si d�comment�, montrera pourquoi la conversion UTF8 est n�cessaire Local $iAlgorithm = $CALG_3DES Local $g_hKey = _Crypt_DeriveKey("CryptPassword", $iAlgorithm) Local $dEncrypted = _Crypt_EncryptData($dPlaintextUTF8, $g_hKey, $CALG_USERKEY) ; Chiffre le texte avec la nouvelle cl� cryptographique. Local $dDecrypted = _Crypt_DecryptData($dEncrypted, $g_hKey, $CALG_USERKEY) ; D�chiffre les donn�es en utilisant la cha�ne de mot de passe g�n�rique. La valeur retourn�e est une cha�ne binaire. Local $sDecrypted = BinaryToString($dDecrypted, $SB_UTF8) ; Convertit la cha�ne binaire en utilisant BinaryToString pour afficher les donn�es initiales encrypt�es. If $sPlaintext = $sDecrypted Then MsgBox($MB_SYSTEMMODAL, "Donn�es d�chiffr�es", $sDecrypted) Else MsgBox($MB_SYSTEMMODAL, "Donn�es d�chiffr�es mauvaises", $sPlaintext & @CRLF & "-->" & @CRLF & $sDecrypted) EndIf