Jump to content

Lost with functions


kudrow
 Share

Recommended Posts

Hello,

First off I feel really stupid because I cannot grasp how to create the function that I want to make. Looking at the examples in the help file just confuse me more. I am trying to simply build a function that will either A: Encrypt a string and return the value or B: Decrypt a string and return the value. I have encryption and decryption working fine I just want to create a function to do the work because I will be doing a lot of encrypting and decrypting in my script. I am working on a TCP register/login/logoff script if anyone is wondering.

Thanks!

Link to comment
Share on other sites

I wish I could delete this topic - I have been beating my head against the wall trying to figure this out but I just realized I need "return" to actually return data.  

#include <Crypt.au3>
$convert=""
$decryptData=""
Func _Unlock()
    $decryptData = _Crypt_DecryptData ($convert, "theKeyUsed", $CALG_AES_256)
    Return $decryptData

    EndFunc

This works when I call _Unlock()

I guess I need to just use "if" statements to declare if I am decrypting or encrypting and return the value. I swear I over think things too much.

Thanks for the help and feedback. All suggestions are welcome.

Link to comment
Share on other sites

Just set a bool variable to see if you want to encript or decrypt.

Saludos

Link to comment
Share on other sites

Just set a bool variable to see if you want to encript or decrypt.

Saludos

You could do that, but I'm a fan of making funcs modular as possible.  I'd have one func do encryption and one do decryption, and depending on how I wanted to handle the "data", perhaps then pass it to a function that would do a condition statement based on a bool value (or other) and pass the data to the appropriate enc/dec func.

Link to comment
Share on other sites

Thank you for the feedback. Can either of you provide me an example of what your talking about? I know a bool is just a switch (true/false) but below is what I created and it does not work.

Func _EncDec($enc=1)
    if $enc==1 Then
        $encrypt = _Crypt_EncryptData($DataToProcess, "TheKeyUsed", $CALG_AES_256)
        Return $encrypt
    Else
        $decryptData = _Crypt_DecryptData ($DataToProcess, "TheKeyUsed", $CALG_AES_256)
    Return $decryptData
    EndIf

    EndFunc

when I call _EncDec() it throws an error and kills the script.

Link to comment
Share on other sites

see example 2 at the help file

Func StringEncrypt($bEncrypt, $sData, $sPassword)
    _Crypt_Startup() ; Start the Crypt library.
    Local $sReturn = ''
    If $bEncrypt Then ; If the flag is set to True then encrypt, otherwise decrypt.
        $sReturn = _Crypt_EncryptData($sData, $sPassword, $CALG_RC4)
    Else
        $sReturn = BinaryToString(_Crypt_DecryptData($sData, $sPassword, $CALG_RC4))
    EndIf
    _Crypt_Shutdown() ; Shutdown the Crypt library.
    Return $sReturn
EndFunc
Link to comment
Share on other sites

I actually found my mistake. I was passing the wrong variable to the function so after the function returned bogus info it made the rest of my script crash. Spider that example is useful once I figured out the concept.

Thank you everyone for the help.

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

×
×
  • Create New...