Jump to content

[Fixed] _Crypt_DeriveKey Not working as Intended


Recommended Posts

Big thanks to CrypticKiwi for helping me with this issue. Click HERE to go to his solution.

 

It was working earlier today but now all of a sudden it doesn't work. I literally have no idea why it won't work. It should look like 0x30783030453638434130 but it will only generate keys that looks like 0x016A0C08 now. (Hint: It's noticibly shorter)

#RequireAdmin
#include "crypt.au3"
#include "Date.au3"
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <MsgBoxConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 154, 121, 192, 124)
$Label1 = GUICtrlCreateLabel("What would you like to do?", 8, 8, 141, 17)
$Button1 = GUICtrlCreateButton("Encrypt", 8, 40, 137, 25)
$Button2 = GUICtrlCreateButton("Decrypt", 8, 80, 137, 25)
#EndRegion ### END Koda GUI section ###
Global $key_encrypted = _Crypt_DeriveKey("1WSUJpOq1ca2H9DMRhs14iy3fI04IBFp", $CALG_RC4);Does not generate a working key. What's going on?
Global $date, $input, $data, $result, $key
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1;I assume works as Intended.

            GUISetState(@SW_HIDE)
            $date = ""
            $date = _DateDiff("s", "1970/01/01 00:00:00", _NowCalc())

            $data = ""
            $data = _Crypt_EncryptData($date, $key_encrypted, $CALG_RC4)

            $input = ""
            $input = InputBox("Crypter", "Enter the data to be encrypted.", $data)

            $result = ""
            $result = _Crypt_EncryptData($input, $key_encrypted, $CALG_RC4)

            
            InputBox("Crypter", "Your Data Encrypted is", $result)
            InputBox("Crypter", "Your Key Encrypted is", $key_encrypted)

            GUISetState(@SW_SHOW)
        Case $Button2;Works as Intended, as it decrypts previously successful encrypted variables just fine.
            GUISetState(@SW_HIDE)

            $input = ""
            $input = InputBox("Crypter", "Enter the data to be decrypted.")
            

            $key = ""
            $key = InputBox("Crypter", "Enter the key to decrypt the data.")
            

            $result = ""
            $result = BinaryToString(_Crypt_DecryptData($input, $key, $CALG_RC4))
            

            InputBox("Crypter", "Your Data Decrypted is", $result)

            GUISetState(@SW_SHOW)
    EndSwitch
WEnd

 

Edited by BetaLeaf
Fixed, Thanks CrypticKiwi

 

 

Link to comment
Share on other sites

  • Moderators

BetaLeaf,

The return you get from the _Crypt_DeriveKey function is a pointer to the generated key (check with VarGetType) - and as such I would expect it to look like "0x016A0C08" as that the normal format for that datatype. Why do you think it should be so much longer?

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

BetaLeaf,

The return you get from the _Crypt_DeriveKey function is a pointer to the generated key (check with VarGetType) - and as such I would expect it to look like "0x016A0C08" as that the normal format for that datatype. Why do you think it should be so much longer?

M23

When I ran _Crypt_DeriveKey earlier that day, it gave me a much longer code that worked. This shorter code fails to properly decrypt the data I give it using _Crypt_DecryptData. Just to be clear, this script is just suppose to generate a key on boot and encrypt some data, with a function to check and make sure it can decrypt fine.  Im using Windows 10 btw.

For example I encrypted the word "test". It gave me the EncryptedData 0xBB34681A
and the key 0x026FBD20. Trying to decrypt it turns "test" into "[¿TÁ"

Now you mentioned it is unexpected that the key is longer. Idk why you say that. This encrypted data contains my email, and it decrypts fine. 

Encrypted Data 0x5127F1C0426C24FCDE778CA6FA5B586F7F73

Key 0x30783030453638434130

Edited by BetaLeaf
example.

 

 

Link to comment
Share on other sites

  • Moderators

BetaLeaf,

The function works fine for me:

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

Example()

Func Example()
    Local $aStringsToEncrypt[5] = ["AutoIt", "SciTE", "Crypt", ".au3", "42"]
    Local $aEncryptedStrings[5], $aDecryptedStrings[5]

    Local $sEnCrypted = "", $sDeCrypted = ""

    Local $hKey = _Crypt_DeriveKey("CryptPassword", $CALG_RC4) ; Declare a password string and algorithm to create a cryptographic key.
MsgBox($MB_SYSTEMMODAL, "Key Pointer", $hKey & @CRLF & @CRLF & VarGetType($hKey))


    For $i = 0 To UBound($aStringsToEncrypt) - 1
        $aEncryptedStrings[$i] = _Crypt_EncryptData($aStringsToEncrypt[$i], $hKey, $CALG_USERKEY)
        $sEnCrypted &= $aStringsToEncrypt[$i] & @TAB & " = " & $aEncryptedStrings[$i] & @CRLF ; Encrypt the text with the cryptographic key.
        $aDecryptedStrings[$i] = _Crypt_DecryptData($aEncryptedStrings[$i], $hKey, $CALG_USERKEY)
        $sDeCrypted &= $aEncryptedStrings[$i] & @TAB & " = " & BinaryToString($aDecryptedStrings[$i]) & @CRLF ; Encrypt the text with the cryptographic key.
    Next

    MsgBox($MB_SYSTEMMODAL, "Encrypted data", $sEnCrypted & @CRLF & @CRLF & $sDeCrypted)

    _Crypt_DestroyKey($hKey) ; Destroy the cryptographic key.
EndFunc   ;==>Example

Now you mentioned it is unexpected that the key is longer. Idk why you say that

I say that because the returned value is a pointer and I would expect a pointer (on my 32 bit machine) to have the format 0x########. Were you perhaps running the script on a 64-bit machine when you got the longer return value?

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

I tried running in both 32 bit and 64 bit. The longer pointer seems to come from the 64 bit exe. I never build 64 bit exe so I have no idea why it did this. Also, I still cannot decrypt my test data with the code I posted in the OP

 

 

Link to comment
Share on other sites

  • Moderators

BetaLeaf,

The longer pointer seems to come from the 64 bit exe

Exactly - pointers are sized according to the "bitness" of OS on which they run. So that is one mystery solved.

As to the failure to decrypt, please post the code (see here how to do it) that you are using to en/decrypt and we can see what might be the problem.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Got it, it has to be  Number($Key) while recieving decryption input from inputbox

#RequireAdmin
#include "crypt.au3"
#include "Date.au3"
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <MsgBoxConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 154, 121, 192, 124)
$Label1 = GUICtrlCreateLabel("What would you like to do?", 8, 8, 141, 17)
$Button1 = GUICtrlCreateButton("Encrypt", 8, 40, 137, 25)
$Button2 = GUICtrlCreateButton("Decrypt", 8, 80, 137, 25)
#EndRegion ### END Koda GUI section ###
Global $key_encrypted = _Crypt_DeriveKey("1WSUJpOq1ca2H9DMRhs14iy3fI04IBFp", $CALG_RC4);Does not generate a working key. What's going on?
Global $date, $input, $data, $result, $key
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1;I assume works as Intended.

            GUISetState(@SW_HIDE)
            $date = ""
            $date = _DateDiff("s", "1970/01/01 00:00:00", _NowCalc())

            $data = ""
            $data = _Crypt_EncryptData($date, $key_encrypted, $CALG_RC4)

            $input = ""
            $input = InputBox("Crypter", "Enter the data to be encrypted.", $data)

            $result = ""
            $result = _Crypt_EncryptData($input, $key_encrypted, $CALG_RC4)


            InputBox("Crypter", "Your Data Encrypted is", $result)
            InputBox("Crypter", "Your Key Encrypted is", $key_encrypted)

            GUISetState(@SW_SHOW)
        Case $Button2;Works as Intended, as it decrypts previously successful encrypted variables just fine.
            GUISetState(@SW_HIDE)

            $input = ""
            $input = InputBox("Crypter", "Enter the data to be decrypted.")


            $key = ""
            $key = InputBox("Crypter", "Enter the key to decrypt the data.")


            $result = ""
            $result = BinaryToString(_Crypt_DecryptData($input, Number($key), $CALG_RC4))


            InputBox("Crypter", "Your Data Decrypted is", $result)

            GUISetState(@SW_SHOW)
    EndSwitch
WEnd

 

 

Edited by CrypticKiwi
Link to comment
Share on other sites

You're trying to encrypt the data with the derived key, but instead you're using the key as the password, which is wrong. Then you're expecting the user to remember the password used to decrypt it. If you want to use DeriveKey, then use $CALG_USERKEY as the algorithm for the encryption and decryption and not RC4.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Got it, it has to be  Number($Key) while recieving decryption input from inputbox

#RequireAdmin
#include "crypt.au3"
#include "Date.au3"
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <MsgBoxConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 154, 121, 192, 124)
$Label1 = GUICtrlCreateLabel("What would you like to do?", 8, 8, 141, 17)
$Button1 = GUICtrlCreateButton("Encrypt", 8, 40, 137, 25)
$Button2 = GUICtrlCreateButton("Decrypt", 8, 80, 137, 25)
#EndRegion ### END Koda GUI section ###
Global $key_encrypted = _Crypt_DeriveKey("1WSUJpOq1ca2H9DMRhs14iy3fI04IBFp", $CALG_RC4);Does not generate a working key. What's going on?
Global $date, $input, $data, $result, $key
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1;I assume works as Intended.

            GUISetState(@SW_HIDE)
            $date = ""
            $date = _DateDiff("s", "1970/01/01 00:00:00", _NowCalc())

            $data = ""
            $data = _Crypt_EncryptData($date, $key_encrypted, $CALG_RC4)

            $input = ""
            $input = InputBox("Crypter", "Enter the data to be encrypted.", $data)

            $result = ""
            $result = _Crypt_EncryptData($input, $key_encrypted, $CALG_RC4)


            InputBox("Crypter", "Your Data Encrypted is", $result)
            InputBox("Crypter", "Your Key Encrypted is", $key_encrypted)

            GUISetState(@SW_SHOW)
        Case $Button2;Works as Intended, as it decrypts previously successful encrypted variables just fine.
            GUISetState(@SW_HIDE)

            $input = ""
            $input = InputBox("Crypter", "Enter the data to be decrypted.")


            $key = ""
            $key = InputBox("Crypter", "Enter the key to decrypt the data.")


            $result = ""
            $result = BinaryToString(_Crypt_DecryptData($input, Number($key), $CALG_RC4))


            InputBox("Crypter", "Your Data Decrypted is", $result)

            GUISetState(@SW_SHOW)
    EndSwitch
WEnd

 

 

This works, Thank you very much CrypticKiwi

You're trying to encrypt the data with the derived key, but instead you're using the key as the password, which is wrong. Then you're expecting the user to remember the password used to decrypt it. If you want to use DeriveKey, then use $CALG_USERKEY as the algorithm for the encryption and decryption and not RC4.

Could you show me an example of how it needs to be use to work properly so I may learn from it? I copy pasted the example from the documentation and expanded it to my needs. I thought I did it correctly.

 

 

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...