Jump to content

Decrypt data


MrVietA2
 Share

Recommended Posts

Helllo everyone,

I'm trying to make a program with two functions : Decrypt and encrypt data. I use the example in Help file and I edit something, but it doesn't work:

#include <Crypt.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>

Global $hKey = -1, $iInputEdit = -1, $iOutputEdit = -1
Global $snum = InputBox("::","1 = Decrypt" & @CRLF & "2 = Encrypt","")
If @error Then Exit

GUICreate("Realtime Encryption", 400, 320)
$iInputEdit = GUICtrlCreateEdit("", 0, 0, 400, 150)
$iOutputEdit = GUICtrlCreateEdit("", 0, 150, 400, 150)

If $snum = 1 Then ; Decrypt
    GUICtrlSetStyle($iInputEdit, $ES_READONLY)
ElseIf $snum = 2 Then ; Encrypt
    GUICtrlSetStyle($iOutputEdit, $ES_READONLY)
EndIf

Local $iCombo = GUICtrlCreateCombo("", 0, 300, 100, 20, $CBS_DROPDOWNLIST)
GUICtrlSetData(-1, "3DES|AES (128bit)|AES (192bit)|AES (256bit)|DES|RC2|RC4", "RC4")
GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
GUISetState(@SW_SHOW)

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

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

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit

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

                Case "AES (128bit)"
                    If @OSVersion = "WIN_2000" Then
                        MsgBox(16, "Error", "Sorry, this algorithm is not available on Windows 2000.") ; Show an error if the system is Windows 2000.
                        ContinueLoop
                    EndIf
                    $bAlgorithm = $CALG_AES_128

                Case "AES (192bit)"
                    If @OSVersion = "WIN_2000" Then
                        MsgBox(16, "Error", "Sorry, this algorithm is not available on Windows 2000.")
                        ContinueLoop
                    EndIf
                    $bAlgorithm = $CALG_AES_192

                Case "AES (256bit)"
                    If @OSVersion = "WIN_2000" Then
                        MsgBox(16, "Error", "Sorry, this algorithm is not available on Windows 2000.")
                        ContinueLoop
                    EndIf
                    $bAlgorithm = $CALG_AES_256

                Case "DES"
                    $bAlgorithm = $CALG_DES

                Case "RC2"
                    $bAlgorithm = $CALG_RC2

                Case "RC4"
                    $bAlgorithm = $CALG_RC4

            EndSwitch
        If $snum = 2 Then
            _Crypt_DestroyKey($hKey) ; Destroy the cryptographic key.
            $hKey = _Crypt_DeriveKey("CryptPassword", $bAlgorithm) ; Re-declare a password string and algorithm to create a new cryptographic key.

            Local $sRead = GUICtrlRead($iInputEdit)
            If StringStripWS($sRead, 8) <> "" Then ; Check there is text available to encrypt.
                Local $bEncrypted = _Crypt_EncryptData($sRead, $hKey, $CALG_USERKEY) ; Encrypt the text with the new cryptographic key.
                GUICtrlSetData($iOutputEdit, $bEncrypted) ; Set the output box with the encrypted text.
            EndIf
        Else
            Local $sRead = GUICtrlRead($iOutputEdit)
            If StringStripWS($sRead, 8) <> "" Then
                Local $bEncrypted = _Crypt_DecryptData($sRead, $hKey, $CALG_USERKEY)
                GUICtrlSetData($iInputEdit, $bEncrypted)
            EndIf
        EndIf
    EndSwitch
WEnd

_Crypt_DestroyKey($hKey) ; Destroy the cryptographic key.
_Crypt_Shutdown() ; Shutdown the crypt library.

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

    Switch _WinAPI_LoWord($wParam)
        Case $iInputEdit
            Switch _WinAPI_HiWord($wParam)
                Case $EN_CHANGE
                    If $snum = 2 Then
                        Local $bEncrypted = _Crypt_EncryptData(GUICtrlRead($iInputEdit), $hKey, $CALG_USERKEY) ; Encrypt the text with the cryptographic key.
                        GUICtrlSetData($iOutputEdit, $bEncrypted) ; Set the output box with the encrypted text.
                    Else
                        Local $bEncrypted = _Crypt_DecryptData(GUICtrlRead($iOutputEdit), $hKey, $CALG_USERKEY)
                        GUICtrlSetData($iInputEdit, $bEncrypted)
                    EndIf
            EndSwitch
    EndSwitch
EndFunc   ;==>WM_COMMAND

Please help me solve this problem, I'm extremely grateful.

P/s : Sorry for my bad english

Link to comment
Share on other sites

you must use BinaryToString() on the return value of _Crypt_DecryptData() - see the remarks in the help file.

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...