Jump to content

Recommended Posts

Posted

Hi 🙂

For some unknown reason, the same code behaves differently if I run it multiple times.

Currently this is the most secure software ever as it seems like I must have the correct value for $myPASS and a lot of luck too 😁

The relevant part of the code:

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

global $special='[@#$%^&+=]'

ConsoleWrite(@CRLF &@CRLF &'@@ Debug(' & @ScriptLineNumber & ') : TEST1' & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
Local $StringToEncrypt="LA.Alalalala123123@"
global $myPASS="LA.Alalalala123123@"
$test=ENCRYPTit($StringToEncrypt,$myPASS)
$test=DECRYPTit($test, $myPASS)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $test = ' & $test & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console

ConsoleWrite(@CRLF &@CRLF &'@@ Debug(' & @ScriptLineNumber & ') : TEST2' & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
$StringToEncrypt="AA.Alalalala123123@"
$myPASS="LA.Alalalala123123@"
$test=ENCRYPTit($StringToEncrypt,$myPASS)
$test=DECRYPTit($test, $myPASS)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $test = ' & $test & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console

Func ENCRYPTit($StringToEncrypt,$myPASS)
    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : ENCRYPT' & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $myPASS = ' & $myPASS & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $StringToEncrypt = ' & $StringToEncrypt & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
    Local $hKey = _Crypt_DeriveKey(Binary(LOGIN($myPASS)), $CALG_AES_256,$CALG_SHA_512) ; Declare a password string and algorithm to create a cryptographic key.
    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : Binary($hKey) = ' & Binary($hKey) & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
    local $encrypted = _Crypt_EncryptData($StringToEncrypt, Binary($hKey), $CALG_AES_256)
    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : binary($encrypted) = ' & binary($encrypted) & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $encrypted = ' & $encrypted & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
    _Crypt_DestroyKey($hKey) ; Destroy the cryptographic key.
    return Binary($encrypted)
EndFunc


Func DECRYPTit($StringToDecrypt, $myPASS)
    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : DECRYPT' & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $myPASS = ' & $myPASS & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $StringToDecrypt = ' & $StringToDecrypt & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
    Local $hKey = _Crypt_DeriveKey(Binary(LOGIN($myPASS)), $CALG_AES_256, $CALG_SHA_512) ; Declare a password string and algorithm to create a cryptographic key.
    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : Binary($hKey) = ' & Binary($hKey) & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
    Local $decrypted = _Crypt_DecryptData($StringToDecrypt, Binary($hKey), $CALG_AES_256)
    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : binary($decrypted) = ' & Binary($decrypted) & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $decrypted = ' & $decrypted & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
    _Crypt_DestroyKey($hKey) ; Destroy the cryptographic key.
    Return BinaryToString(binary($decrypted))
EndFunc   ;==>DECRYPT

 

Results when it fails:

@@ Debug(6) : TEST1
>Error code: 0
@@ Debug(21) : ENCRYPT
>Error code: 0
@@ Debug(22) : $myPASS = LA.Alalalala123123@
>Error code: 0
@@ Debug(23) : $StringToEncrypt = LA.Alalalala123123@
>Error code: 0
@@ Debug(25) : Binary($hKey) = 0xC0E709E4EC010000
>Error code: 0
@@ Debug(27) : binary($encrypted) = 0x13756D1C3845579E5CAE2065B087E3661165445612CFE4A40E18372BCA170DB1
>Error code: 0
@@ Debug(28) : $encrypted = 0x13756D1C3845579E5CAE2065B087E3661165445612CFE4A40E18372BCA170DB1
>Error code: 0
@@ Debug(35) : DECRYPT
>Error code: 0
@@ Debug(36) : $myPASS = LA.Alalalala123123@
>Error code: 0
@@ Debug(37) : $StringToDecrypt = 0x13756D1C3845579E5CAE2065B087E3661165445612CFE4A40E18372BCA170DB1
>Error code: 0
@@ Debug(39) : Binary($hKey) = 0xC0E709E4EC010000
>Error code: 0
@@ Debug(41) : binary($decrypted) = 0x4C412E416C616C616C616C6131323331323340
>Error code: 0
@@ Debug(42) : $decrypted = 0x4C412E416C616C616C616C6131323331323340
>Error code: 0
@@ Debug(11) : $test = LA.Alalalala123123@
>Error code: 0


@@ Debug(13) : TEST2
>Error code: 0
@@ Debug(21) : ENCRYPT
>Error code: 0
@@ Debug(22) : $myPASS = LA.Alalalala123123@
>Error code: 0
@@ Debug(23) : $StringToEncrypt = AA.Alalalala123123@
>Error code: 0
@@ Debug(25) : Binary($hKey) = 0xC0E709E4EC010000
>Error code: 0
@@ Debug(27) : binary($encrypted) = 0x0F2E2D69E210E72B7BEC05CF7E628A7D2D872D8F38B578AD046891BCBB8B044B
>Error code: 0
@@ Debug(28) : $encrypted = 0x0F2E2D69E210E72B7BEC05CF7E628A7D2D872D8F38B578AD046891BCBB8B044B
>Error code: 0
@@ Debug(35) : DECRYPT
>Error code: 0
@@ Debug(36) : $myPASS = LA.Alalalala123123@
>Error code: 0
@@ Debug(37) : $StringToDecrypt = 0x0F2E2D69E210E72B7BEC05CF7E628A7D2D872D8F38B578AD046891BCBB8B044B
>Error code: 0
@@ Debug(39) : Binary($hKey) = 0x40F209E4EC010000
>Error code: 0
@@ Debug(41) : binary($decrypted) = 0xFFFFFFFF
>Error code: 0
@@ Debug(42) : $decrypted = -1
>Error code: 70
@@ Debug(18) : $test = ÿÿÿÿ
>Error code: 0
+>19:17:42 AutoIt3.exe ended.rc:0
+>19:17:43 AutoIt3Wrapper Finished.
>Exit code: 0    Time: 4.54

Another different failed try without any change in the code:

@@ Debug(6) : TEST1
>Error code: 0
@@ Debug(21) : ENCRYPT
>Error code: 0
@@ Debug(22) : $myPASS = LA.Alalalala123123@
>Error code: 0
@@ Debug(23) : $StringToEncrypt = LA.Alalalala123123@
>Error code: 0
@@ Debug(25) : Binary($hKey) = 0xA0E3B88EAA010000
>Error code: 0
@@ Debug(27) : binary($encrypted) = 0x8717DBB452965C093FD48B7B14C9DA4803A8D60A9189DBE97228503B3D098653
>Error code: 0
@@ Debug(28) : $encrypted = 0x8717DBB452965C093FD48B7B14C9DA4803A8D60A9189DBE97228503B3D098653
>Error code: 0
@@ Debug(35) : DECRYPT
>Error code: 0
@@ Debug(36) : $myPASS = LA.Alalalala123123@
>Error code: 0
@@ Debug(37) : $StringToDecrypt = 0x8717DBB452965C093FD48B7B14C9DA4803A8D60A9189DBE97228503B3D098653
>Error code: 0
@@ Debug(39) : Binary($hKey) = 0x20F0B88EAA010000
>Error code: 0
@@ Debug(41) : binary($decrypted) = 0xFFFFFFFF
>Error code: 0
@@ Debug(42) : $decrypted = -1
>Error code: 70
@@ Debug(11) : $test = ÿÿÿÿ
>Error code: 0


@@ Debug(13) : TEST2
>Error code: 0
@@ Debug(21) : ENCRYPT
>Error code: 0
@@ Debug(22) : $myPASS = LA.Alalalala123123@
>Error code: 0
@@ Debug(23) : $StringToEncrypt = AA.Alalalala123123@
>Error code: 0
@@ Debug(25) : Binary($hKey) = 0x20E1B88EAA010000
>Error code: 0
@@ Debug(27) : binary($encrypted) = 0xC993721623667138C0A2C523163694C08D35BDB55682AC3979AFF804CF914592
>Error code: 0
@@ Debug(28) : $encrypted = 0xC993721623667138C0A2C523163694C08D35BDB55682AC3979AFF804CF914592
>Error code: 0
@@ Debug(35) : DECRYPT
>Error code: 0
@@ Debug(36) : $myPASS = LA.Alalalala123123@
>Error code: 0
@@ Debug(37) : $StringToDecrypt = 0xC993721623667138C0A2C523163694C08D35BDB55682AC3979AFF804CF914592
>Error code: 0
@@ Debug(39) : Binary($hKey) = 0xA0DEB88EAA010000
>Error code: 0
@@ Debug(41) : binary($decrypted) = 0xFFFFFFFF
>Error code: 0
@@ Debug(42) : $decrypted = -1
>Error code: 70
@@ Debug(18) : $test = ÿÿÿÿ
>Error code: 0
+>19:25:53 AutoIt3.exe ended.rc:0
+>19:25:53 AutoIt3Wrapper Finished.
>Exit code: 0    Time: 3.022

 

Another try and different result again!

@@ Debug(6) : TEST1
>Error code: 0
@@ Debug(21) : ENCRYPT
>Error code: 0
@@ Debug(22) : $myPASS = LA.Alalalala123123@
>Error code: 0
@@ Debug(23) : $StringToEncrypt = LA.Alalalala123123@
>Error code: 0
@@ Debug(25) : Binary($hKey) = 0xC0EBD63F03020000
>Error code: 0
@@ Debug(27) : binary($encrypted) = 0x6916F4099537D771E17B8C970134D14EF369DEFA5937BBD5A995425023730D37
>Error code: 0
@@ Debug(28) : $encrypted = 0x6916F4099537D771E17B8C970134D14EF369DEFA5937BBD5A995425023730D37
>Error code: 0
@@ Debug(35) : DECRYPT
>Error code: 0
@@ Debug(36) : $myPASS = LA.Alalalala123123@
>Error code: 0
@@ Debug(37) : $StringToDecrypt = 0x6916F4099537D771E17B8C970134D14EF369DEFA5937BBD5A995425023730D37
>Error code: 0
@@ Debug(39) : Binary($hKey) = 0x40EDD63F03020000
>Error code: 0
@@ Debug(41) : binary($decrypted) = 0xFFFFFFFF
>Error code: 0
@@ Debug(42) : $decrypted = -1
>Error code: 70
@@ Debug(11) : $test = ÿÿÿÿ
>Error code: 0


@@ Debug(13) : TEST2
>Error code: 0
@@ Debug(21) : ENCRYPT
>Error code: 0
@@ Debug(22) : $myPASS = LA.Alalalala123123@
>Error code: 0
@@ Debug(23) : $StringToEncrypt = AA.Alalalala123123@
>Error code: 0
@@ Debug(25) : Binary($hKey) = 0xC0EBD63F03020000
>Error code: 0
@@ Debug(27) : binary($encrypted) = 0xB1FD73FE621D58CFE20D18386424EF788D4ED0528B9A95937C5FCABEE5BFB0F2
>Error code: 0
@@ Debug(28) : $encrypted = 0xB1FD73FE621D58CFE20D18386424EF788D4ED0528B9A95937C5FCABEE5BFB0F2
>Error code: 0
@@ Debug(35) : DECRYPT
>Error code: 0
@@ Debug(36) : $myPASS = LA.Alalalala123123@
>Error code: 0
@@ Debug(37) : $StringToDecrypt = 0xB1FD73FE621D58CFE20D18386424EF788D4ED0528B9A95937C5FCABEE5BFB0F2
>Error code: 0
@@ Debug(39) : Binary($hKey) = 0xC0EBD63F03020000
>Error code: 0
@@ Debug(41) : binary($decrypted) = 0x41412E416C616C616C616C6131323331323340
>Error code: 0
@@ Debug(42) : $decrypted = 0x41412E416C616C616C616C6131323331323340
>Error code: 0
@@ Debug(18) : $test = AA.Alalalala123123@
>Error code: 0
+>19:29:06 AutoIt3.exe ended.rc:0
+>19:29:07 AutoIt3Wrapper Finished.
>Exit code: 0    Time: 2.95

 

 

Then magically, after being insistent by running the same code again, it works:

@@ Debug(6) : TEST1
>Error code: 0
@@ Debug(21) : ENCRYPT
>Error code: 0
@@ Debug(22) : $myPASS = LA.Alalalala123123@
>Error code: 0
@@ Debug(23) : $StringToEncrypt = LA.Alalalala123123@
>Error code: 0
@@ Debug(25) : Binary($hKey) = 0x1095A7F58D010000
>Error code: 0
@@ Debug(27) : binary($encrypted) = 0x5DBA6E0DD474E2C394E9ADDD688C45333ABD5CD125510849B8CD60575015C146
>Error code: 0
@@ Debug(28) : $encrypted = 0x5DBA6E0DD474E2C394E9ADDD688C45333ABD5CD125510849B8CD60575015C146
>Error code: 0
@@ Debug(35) : DECRYPT
>Error code: 0
@@ Debug(36) : $myPASS = LA.Alalalala123123@
>Error code: 0
@@ Debug(37) : $StringToDecrypt = 0x5DBA6E0DD474E2C394E9ADDD688C45333ABD5CD125510849B8CD60575015C146
>Error code: 0
@@ Debug(39) : Binary($hKey) = 0x1095A7F58D010000
>Error code: 0
@@ Debug(41) : binary($decrypted) = 0x4C412E416C616C616C616C6131323331323340
>Error code: 0
@@ Debug(42) : $decrypted = 0x4C412E416C616C616C616C6131323331323340
>Error code: 0
@@ Debug(11) : $test = LA.Alalalala123123@
>Error code: 0


@@ Debug(13) : TEST2
>Error code: 0
@@ Debug(21) : ENCRYPT
>Error code: 0
@@ Debug(22) : $myPASS = LA.Alalalala123123@
>Error code: 0
@@ Debug(23) : $StringToEncrypt = AA.Alalalala123123@
>Error code: 0
@@ Debug(25) : Binary($hKey) = 0x1095A7F58D010000
>Error code: 0
@@ Debug(27) : binary($encrypted) = 0xAA6F0C080AB9D2582FFDB980409286CF25DA2CAA2D62995C2EEE0F82205FBDEF
>Error code: 0
@@ Debug(28) : $encrypted = 0xAA6F0C080AB9D2582FFDB980409286CF25DA2CAA2D62995C2EEE0F82205FBDEF
>Error code: 0
@@ Debug(35) : DECRYPT
>Error code: 0
@@ Debug(36) : $myPASS = LA.Alalalala123123@
>Error code: 0
@@ Debug(37) : $StringToDecrypt = 0xAA6F0C080AB9D2582FFDB980409286CF25DA2CAA2D62995C2EEE0F82205FBDEF
>Error code: 0
@@ Debug(39) : Binary($hKey) = 0x1095A7F58D010000
>Error code: 0
@@ Debug(41) : binary($decrypted) = 0x41412E416C616C616C616C6131323331323340
>Error code: 0
@@ Debug(42) : $decrypted = 0x41412E416C616C616C616C6131323331323340
>Error code: 0
@@ Debug(18) : $test = AA.Alalalala123123@
>Error code: 0
+>19:32:26 AutoIt3.exe ended.rc:0
+>19:32:26 AutoIt3Wrapper Finished.
>Exit code: 0    Time: 3.397

 

Posted

Two issues I see:
    No need to pass $hKey to the encrypt and decrypt functions as a binary
    You should use $CALG_USERKEY instead of $CALG_AES_256
    
 

;local $encrypted = _Crypt_EncryptData($StringToEncrypt, Binary($hKey), $CALG_AES_256)
local $encrypted = _Crypt_EncryptData($StringToEncrypt, $hKey, $CALG_USERKEY)



;Local $decrypted = _Crypt_DecryptData($StringToDecrypt, Binary($hKey), $CALG_AES_256)
Local $decrypted = _Crypt_DecryptData($StringToDecrypt, $hKey, $CALG_USERKEY)

 

Posted
13 hours ago, Nine said:

Maybe the problem comes from the LOGIN function ?  Since you did not provide it, hard to say.

Thank you for your answers, but this is not the case. If you look into the outputs, the login function is returning consistent values in all the tests.

Posted
10 hours ago, spudw2k said:

Two issues I see:
    No need to pass $hKey to the encrypt and decrypt functions as a binary
    You should use $CALG_USERKEY instead of $CALG_AES_256
    
 

;local $encrypted = _Crypt_EncryptData($StringToEncrypt, Binary($hKey), $CALG_AES_256)
local $encrypted = _Crypt_EncryptData($StringToEncrypt, $hKey, $CALG_USERKEY)



;Local $decrypted = _Crypt_DecryptData($StringToDecrypt, Binary($hKey), $CALG_AES_256)
Local $decrypted = _Crypt_DecryptData($StringToDecrypt, $hKey, $CALG_USERKEY)

 

Thank you for your answer! It has resolved the issue.

I just did not understand why I am forced to use $CALG_USERKEY instead of $CALG_AES_256.

I thought there is where I choose which algorithm I want to use for encrypting/decrypting my stuff. If I can't choose it, why the help file for both the _Crypt_EncryptData and _Crypt_DecryptData tells that $iAlgID is "The algorithm to use. See _Crypt_DeriveKey()."?

Also, do you know what is the practical effect of using $CALG_USERKEY?

Posted (edited)

If you look inside the Crypt.au3 UDF, you can see that if $CALG_USERKEY is not specified, then the _Crypt_DeriveKey function is called anyways inside the _Crypt_EncryptData function with the key value and the algorithm chosen.

You can think of it this way; the _Crypt_DeriveKey function embeds the encryption algorithm into the $hKey handle (HCRYPTKEY data type). So whether you generate (derive) a key on your own, or leave it to _Crypt_EncryptData to handle it for you, the algorithm is embedded in the HCRYPTKEY data type.

 

The advantage of deriving a key on your own is that you can specify which hash algorithm you want to use. If you rely on having _Crypt_EncryptData handle the key derive function for you, it uses a default hash algo (MD5).

The advantage of not deriving the key on your own is the key is automatically destroyed at the end of the _Crypt_EncryptData function, and you don't have to do it on your own.

Edited by spudw2k

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
  • Recently Browsing   0 members

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