Jump to content

_StringEncrypt is returning ""


Recommended Posts

  • Developers

Will it mean something more than just replacing CHR with Hex...? :) I'd be willing to give it a try. Lemme see what I can do before I go out.

It will won't it :)

It will need more then that since it will not create an intermediate character string but a string of hex values.

This will require digging into the current logic and convert it in such a way that the same output value is achieved but avoiding to use the chr() func.

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Replies 208
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Posted Images

It will need more then that since it will not create an intermediate character string but a string of hex values.

This will require digging into the current logic and convert it in such a way that the same output value is achieved but avoiding to use the chr() func.

I had a quick look, and got about as lost as a little kid in Heathrow Airpot. I'll try it again later.
Link to comment
Share on other sites

What i should do now ..?

Sit back and wait ?

Reconsider my previous recommendation of using RC4() to get a true binary encryption... if you must have the encrypted value as a string, write a short function to take the long binary returned and convert it to string hex by BinaryLen/BinaryMid/Hex functions.

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

  • Developers

What i should do now ..?

Sit back and wait ?

Don't agree with Gary's answer. I think you should work out the solution and submit it to us so we can update it .. :)

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Don't agree with Gary's answer. I think you should work out the solution and submit it to us so we can update it .. :)

I can't reproduce the OP's symptoms because it seems to involve having code pages loaded that I don't have. This version of __StringEncrypt() {note double underbar in function name} guts the native AutoIt encryption scheme from the original _StringEncrypt() and makes calls to SkinnyWhiteGuy's RC4() instead. It is essentially a wrapper for RC4() that allows you to get strings in/out, consistent with the behavior of the original _StringEncrypt(). In addition, it allows multipass encryption by appending a pseudo-random 32bits to the encrypted string and passing it through again.

Need CrazeStar1074 to test with his environment:

$sString = "This is a text string"
ConsoleWrite("Debug: $sString = " & $sString & @LF)
$sKey = "Key phrase"
ConsoleWrite("Debug: $sKey = " & $sKey & @LF)
For $z = 1 To 4
    ConsoleWrite("Debug: Encryption level = " & $z & @LF)
    $sEncrypted = __StringEncrypt(1, $sString, $sKey, $z)
    ConsoleWrite("Debug: $sEncrypted = " & $sEncrypted & @LF)
    $sDecrypted = __StringEncrypt(0, $sEncrypted, $sKey, $z)
    ConsoleWrite("Debug: $sDecrypted = " & $sDecrypted & @LF)
Next

;===============================================================================
;
; Function Name:    __StringEncrypt()
; Description:      RC4 Based string encryption
; Parameter(s):     $i_Encrypt - 1 to encrypt, 0 to decrypt
;                   $s_EncryptText - string to encrypt
;                   $s_EncryptPassword - string to use as an encryption password
;                   $i_EncryptLevel - integer to use as number of times to encrypt string
; Requirement(s):   None
; Return Value(s):  On Success - Returns the string encrypted (blank) times with (blank) password
;                   On Failure - Returns a blank string and sets @error = 1
; Author(s):        Wes Wolfe-Wolvereness <Weswolf at aol dot com>
;                   Modified by PsaltyDS to use binary RC4 functionality from SkinnyWhiteGuy
;
;===============================================================================
;
Func __StringEncrypt($i_Encrypt, $s_EncryptText, $s_EncryptPassword, $i_EncryptLevel = 1)
    Local $RET, $sRET = "", $iBinLen, $iHexWords
    
    ; Sanity check of parameters
    If $i_Encrypt <> 0 And $i_Encrypt <> 1 Then
        SetError(1)
        Return ''
    ElseIf $s_EncryptText = '' Or $s_EncryptPassword = '' Then
        SetError(1)
        Return ''
    EndIf
    If Number($i_EncryptLevel) <= 0 Or Int($i_EncryptLevel) <> $i_EncryptLevel Then $i_EncryptLevel = 1
    
    ; Encrypt/Decrypt
    If $i_Encrypt Then
        ; Encrypt selected
        $RET = $s_EncryptText
        For $n = 1 To $i_EncryptLevel
            If $n > 1 Then $RET = Binary(Random(0, 2 ^ 31 - 1, 1)) & $RET & Binary(Random(0, 2 ^ 31 - 1, 1)) ; prepend/append random 32bits
            $RET = rc4($s_EncryptPassword, $RET) ; returns binary
        Next
       
        ; Convert to hex string
        $iBinLen = BinaryLen($RET)
        $iHexWords = Int($iBinLen / 4)
        If Mod($iBinLen, 4) Then $iHexWords += 1
        For $n = 1 To $iHexWords
            $sRET &= Hex(BinaryMid($RET, 1 + (4 * ($n - 1)), 4))
        Next
        $RET = $sRET
    Else
        ; Decrypt selected
        ; Convert input string to primary binary
        $RET = Binary("0x" & $s_EncryptText) ; Convert string to binary
       
        ; Additional passes, if required
        For $n = 1 To $i_EncryptLevel
            If $n > 1 Then
                $iBinLen = BinaryLen($RET)
                $RET = BinaryMid($RET, 5, $iBinLen - 8) ; strip random 32bits from both ends
            EndIf
            $RET = rc4($s_EncryptPassword, $RET)
        Next
        $RET = BinaryToString($RET)
    EndIf
    
    ; Return result
    Return $RET
EndFunc   ;==>__StringEncrypt

; -------------------------------------------------------
; Function:  rc4
; Purpose:  An encryption/decryption RC4 implementation in AutoIt
; Syntax:  rc4($key, $value)
;   Where:  $key = encrypt/decrypt key
;       $value = value to be encrypted/decrypted
; On success returns encrypted/decrypted version of $value
; Author:  SkinnyWhiteGuy on the AutoIt forums at www.autoitscript.com/forum
; Notes:  The same function encrypts and decrypts $value.
; -------------------------------------------------------
Func rc4($key, $value)
    Local $S[256], $i, $j, $c, $t, $x, $y, $output
    Local $keyLength = BinaryLen($key), $valLength = BinaryLen($value)
    For $i = 0 To 255
        $S[$i] = $i
    Next
    For $i = 0 To 255
        $j = Mod($j + $S[$i] + Dec(StringTrimLeft(BinaryMid($key, Mod($i, $keyLength) + 1, 1), 2)), 256)
        $t = $S[$i]
        $S[$i] = $S[$j]
        $S[$j] = $t
    Next
    For $i = 1 To $valLength
        $x = Mod($x + 1, 256)
        $y = Mod($S[$x] + $y, 256)
        $t = $S[$x]
        $S[$x] = $S[$y]
        $S[$y] = $t
        $j = Mod($S[$x] + $S[$y], 256)
        $c = BitXOR(Dec(StringTrimLeft(BinaryMid($value, $i, 1), 2)), $S[$j])
        $output = Binary($output) & Binary('0x' & Hex($c, 2))
    Next
    Return $output
EndFunc   ;==>rc4

My results are:

>Running:(3.2.10.0):C:\Program Files\AutoIt3\autoit3.exe "C:\Program Files\AutoIt3\Scripts\Test_1.au3"  
Debug: $sString = This is a text string
Debug: $sKey = Key phrase
Debug: Encryption level = 1
Debug: $sEncrypted = 13BFA0643B2D2B75A2FA41A98B0B363748DB0EC8C5
Debug: $sDecrypted = This is a text string
Debug: Encryption level = 2
Debug: $sEncrypted = AC829D3708FBF831F8F71EB9518557EDB7A25191EAB226AEB91B64799E
Debug: $sDecrypted = This is a text string
Debug: Encryption level = 3
Debug: $sEncrypted = 00DF6B69F6A61452CB21CDFD0B8808FD6D2C304B15CB79F796C76BA468DBFF4502927AE867
Debug: $sDecrypted = This is a text string
Debug: Encryption level = 4
Debug: $sEncrypted = AA29DB5DBF32CA0EEC589CE5385EDBB937216F5BCF45182D69BE34FD472FE0D6CECD55011C273C41AB6EF2E201
Debug: $sDecrypted = This is a text string
+>12:34:30 AutoIT3.exe ended.rc:0

:)

Edit: Updated to a new implementation of the multi-pass functionality. Now it adds a random 32bits to BOTH ends for each pass greater than 1. This means you get a different result each time it is run for higher numbers of passes, and don't see the same pattern just shifted down.

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Thats what it returned

>"C:\Program Files\AutoIt3\SciTE\AutoIt3Wrapper\AutoIt3Wrapper.exe" /run /prod /ErrorStdOut /in "C:\Documents and Settings\user\Desktop\StringEncrypt.au3" /autoit3dir "C:\Program Files\AutoIt3" /UserParams   
+>17:56:45 Starting AutoIt3Wrapper v.1.9.5.6    Environment(Language:0409  Keyboard:00000409  OS:WIN_XP/Service Pack 2  CPU:X86)
>Running AU3Check (1.54.10.0)  from:C:\Program Files\AutoIt3
+>17:56:45 AU3Check ended.rc:0
>Running:(3.2.10.0):C:\Program Files\AutoIt3\autoit3.exe "C:\Documents and Settings\user\Desktop\StringEncrypt.au3" 
Debug: $sString = This is a text string
Debug: $sKey = Key phrase
Debug: Encryption level = 1
Debug: $sEncrypted = 13BFA0643B2D2B75A2FA41A98B0B363748DB0EC8C5
Debug: $sDecrypted = This is a text string
Debug: Encryption level = 2
Debug: $sEncrypted = 546869732069732061207465787420737472696E6798FD2569
Debug: $sDecrypted = This is a text string
Debug: Encryption level = 3
Debug: $sEncrypted = 13BFA0643B2D2B75A2FA41A98B0B363748DB0EC8C54E49C23ADB04F6E7
Debug: $sDecrypted = This is a text string
+>17:56:48 AutoIT3.exe ended.rc:0
>Exit code: 0   Time: 4.392
Link to comment
Share on other sites

Thats what it returned

>"C:\Program Files\AutoIt3\SciTE\AutoIt3Wrapper\AutoIt3Wrapper.exe" /run /prod /ErrorStdOut /in "C:\Documents and Settings\user\Desktop\StringEncrypt.au3" /autoit3dir "C:\Program Files\AutoIt3" /UserParams   
+>17:56:45 Starting AutoIt3Wrapper v.1.9.5.6    Environment(Language:0409  Keyboard:00000409  OS:WIN_XP/Service Pack 2  CPU:X86)
>Running AU3Check (1.54.10.0)  from:C:\Program Files\AutoIt3
+>17:56:45 AU3Check ended.rc:0
>Running:(3.2.10.0):C:\Program Files\AutoIt3\autoit3.exe "C:\Documents and Settings\user\Desktop\StringEncrypt.au3" 
Debug: $sString = This is a text string
Debug: $sKey = Key phrase
Debug: Encryption level = 1
Debug: $sEncrypted = 13BFA0643B2D2B75A2FA41A98B0B363748DB0EC8C5
Debug: $sDecrypted = This is a text string
Debug: Encryption level = 2
Debug: $sEncrypted = 546869732069732061207465787420737472696E6798FD2569
Debug: $sDecrypted = This is a text string
Debug: Encryption level = 3
Debug: $sEncrypted = 13BFA0643B2D2B75A2FA41A98B0B363748DB0EC8C54E49C23ADB04F6E7
Debug: $sDecrypted = This is a text string
+>17:56:48 AutoIT3.exe ended.rc:0
>Exit code: 0   Time: 4.392
Is it just me, or is the encrypted values different? :)
Link to comment
Share on other sites

Thats what it returned

>"C:\Program Files\AutoIt3\SciTE\AutoIt3Wrapper\AutoIt3Wrapper.exe" /run /prod /ErrorStdOut /in "C:\Documents and Settings\user\Desktop\StringEncrypt.au3" /autoit3dir "C:\Program Files\AutoIt3" /UserParams   
+>17:56:45 Starting AutoIt3Wrapper v.1.9.5.6    Environment(Language:0409  Keyboard:00000409  OS:WIN_XP/Service Pack 2  CPU:X86)
>Running AU3Check (1.54.10.0)  from:C:\Program Files\AutoIt3
+>17:56:45 AU3Check ended.rc:0
>Running:(3.2.10.0):C:\Program Files\AutoIt3\autoit3.exe "C:\Documents and Settings\user\Desktop\StringEncrypt.au3" 
Debug: $sString = This is a text string
Debug: $sKey = Key phrase
Debug: Encryption level = 1
Debug: $sEncrypted = 13BFA0643B2D2B75A2FA41A98B0B363748DB0EC8C5
Debug: $sDecrypted = This is a text string
Debug: Encryption level = 2
Debug: $sEncrypted = 546869732069732061207465787420737472696E6798FD2569
Debug: $sDecrypted = This is a text string
Debug: Encryption level = 3
Debug: $sEncrypted = 13BFA0643B2D2B75A2FA41A98B0B363748DB0EC8C54E49C23ADB04F6E7
Debug: $sDecrypted = This is a text string
+>17:56:48 AutoIT3.exe ended.rc:0
>Exit code: 0   Time: 4.392
The thing i wonder now is.. If the encrypted value returns the same as the standard autoit _StringEncrypt value ?
Link to comment
Share on other sites

Is it just me, or is the encrypted values different? :)

To start with I'm no expert on encryption algorithms.

They are the same apart from the pseudo-random 32bits added to level 2 and level 3. The other interesting thing I noticed is that if you discard the last 64 bits from the level 3 encrypted it is the same as level 1 encrypted.

Is the multi level encryption achieving much greater security other than adding random noise to the end of the encrypted strings.

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Link to comment
Share on other sites

It wasn't an attempt to reproduce the same output as the _StringEncrypt() function of the current String.au3 UDF. And the multi-pass is not cryptographically useful, it was just throwing in a way to use the $i_EncryptLevel parameter (and not very effectively, it's true).

The idea I was pushing was that the encrypt function should be binary, not string based, with a simple binary-to-string or string-to-binary conversion as required performed by the user code. Any "multi-pass" implementation should be in the user's code too, not in the function. The primary reason for that is to NOT get AutoIt-specific results that cannot be passed to other standard RC4 implementations (i.e. in C++, Perl, PHP, Java, etc.). And to be able to use RC4 encrypted data passed to AutoIt from those other implementations. This would completely break backward compatibility and is a little off topic here.

What Jos really wants is a backwards compatible modification to _StringEncrypt() so it will work with alternate code pages loaded. That seems to be CrazeStar1074's problem.

The code I posted doesn't do that, and should have been saved for another topic (minus the cheesy multi-pass hack).

My apologies.

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

It wasn't an attempt to reproduce the same output as the _StringEncrypt() function of the current String.au3 UDF. And the multi-pass is not cryptographically useful, it was just throwing in a way to use the $i_EncryptLevel parameter (and not very effectively, it's true).

The idea I was pushing was that the encrypt function should be binary, not string based, with a simple binary-to-string or string-to-binary conversion as required performed by the user code. Any "multi-pass" implementation should be in the user's code too, not in the function. The primary reason for that is to NOT get AutoIt-specific results that cannot be passed to other standard RC4 implementations (i.e. in C++, Perl, PHP, Java, etc.). And to be able to use RC4 encrypted data passed to AutoIt from those other implementations. This would completely break backward compatibility and is a little off topic here.

What Jos really wants is a backwards compatible modification to _StringEncrypt() so it will work with alternate code pages loaded. That seems to be CrazeStar1074's problem.

The code I posted doesn't do that, and should have been saved for another topic (minus the cheesy multi-pass hack).

My apologies.

:)

Your english is a little bit hard for me.. LOL , im just in secondary 2..

Link to comment
Share on other sites

I've got a problem.. I've check already and found nothing wrong.. But it returned me an error..

MsgBox(0, "", __StringEncrypt(0, IniRead($htloadfullpath, __StringEncrypt(0, "[5D463233A5CA851675]", "hyena"), __StringEncrypt(0, "55732C39A5DB9301", "hyena"), "ERROR")))
;===============================================================================
;
; Function Name:    __StringEncrypt()
; Description:      RC4 Based string encryption
; Parameter(s):     $i_Encrypt - 1 to encrypt, 0 to decrypt
;                   $s_EncryptText - string to encrypt
;                   $s_EncryptPassword - string to use as an encryption password
;                   $i_EncryptLevel - integer to use as number of times to encrypt string
; Requirement(s):   None
; Return Value(s):  On Success - Returns the string encrypted (blank) times with (blank) password
;                   On Failure - Returns a blank string and sets @error = 1
; Author(s):        Wes Wolfe-Wolvereness <Weswolf at aol dot com>
;           Modified by PsaltyDS to use binary RC4 functionality from SkinnyWhiteGuy
;
;===============================================================================
;
Func __StringEncrypt($i_Encrypt, $s_EncryptText, $s_EncryptPassword, $i_EncryptLevel = 1)
    Local $RET, $sRET = "", $iBinLen, $iHexWords
    
    ; Sanity check of parameters
    If $i_Encrypt <> 0 And $i_Encrypt <> 1 Then
        SetError(1)
        Return ''
    ElseIf $s_EncryptText = '' Or $s_EncryptPassword = '' Then
        SetError(1)
        Return ''
    EndIf
    If Number($i_EncryptLevel) <= 0 Or Int($i_EncryptLevel) <> $i_EncryptLevel Then $i_EncryptLevel = 1
    
    ; Encrypt/Decrypt
    If $i_Encrypt Then
        ; Encrypt selected
        $RET = $s_EncryptText
        For $n = 1 To $i_EncryptLevel
            If $n > 1 Then $RET = $RET & Binary(Random(0, 2 ^ 31 - 1, 1)) ; append random 32bits
            $RET = rc4($s_EncryptPassword, $RET) ; returns binary
        Next
        
        ; Convert to hex string
        $iBinLen = BinaryLen($RET)
        $iHexWords = Int($iBinLen / 4)
        If Mod($iBinLen, 4) Then $iHexWords += 1
        For $n = 1 To $iHexWords
            $sRET &= Hex(BinaryMid($RET, 1 + (4 * ($n - 1)), 4))
        Next
        $RET = $sRET
    Else
        ; Decrypt selected
        ; Convert input string to primary binary
        $RET = Binary("0x" & $s_EncryptText) ; Convert string to binary
        
        ; Additional passes, if required
        For $n = 1 To $i_EncryptLevel
            If $n > 1 Then
                $iBinLen = BinaryLen($RET)
                $RET = BinaryMid($RET, 1, $iBinLen - 4) ; strip random 32bits
            EndIf
            $RET = rc4($s_EncryptPassword, $RET)
        Next
        $RET = BinaryToString($RET)
    EndIf
    
    ; Return result
    Return $RET
EndFunc   ;==>__StringEncrypt

; -------------------------------------------------------
; Function:  rc4
; Purpose:  An encryption/decryption RC4 implementation in AutoIt
; Syntax:  rc4($key, $value)
;   Where:  $key = encrypt/decrypt key
;       $value = value to be encrypted/decrypted
; On success returns encrypted/decrypted version of $value
; Author:  SkinnyWhiteGuy on the AutoIt forums at www.autoitscript.com/forum
; Notes:  The same function encrypts and decrypts $value.
; -------------------------------------------------------
Func rc4($key, $value)
    Local $S[256], $i, $j, $c, $t, $x, $y, $output
    Local $keyLength = BinaryLen($key), $valLength = BinaryLen($value)
    For $i = 0 To 255
        $S[$i] = $i
    Next
    For $i = 0 To 255
        $j = Mod($j + $S[$i] + Dec(StringTrimLeft(BinaryMid($key, Mod($i, $keyLength) + 1, 1), 2)), 256)
        $t = $S[$i]
        $S[$i] = $S[$j]
        $S[$j] = $t
    Next
    For $i = 1 To $valLength
        $x = Mod($x + 1, 256)
        $y = Mod($S[$x] + $y, 256)
        $t = $S[$x]
        $S[$x] = $S[$y]
        $S[$y] = $t
        $j = Mod($S[$x] + $S[$y], 256)
        $c = BitXOR(Dec(StringTrimLeft(BinaryMid($value, $i, 1), 2)), $S[$j])
        $output = Binary($output) & Binary('0x' & Hex($c, 2))
    Next
    Return $output
EndFunc   ;==>rc4
Link to comment
Share on other sites

I've got a problem.. I've check already and found nothing wrong.. But it returned me an error..

MsgBox(0, "", __StringEncrypt(0, IniRead($htloadfullpath, __StringEncrypt(0, "[5D463233A5CA851675]", "hyena"), __StringEncrypt(0, "55732C39A5DB9301", "hyena"), "ERROR")))
You should not have the square brackets "[]" included in the encrypted section name. It is throwing off the decryption and they are not expected by the IniRead() function anyway.

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Removed that.. But still returned this error

Posted Image

Not surprising when you nest functions that far. It is valid code practice, but complicates tracing/debugging when things go wrong. Note you have three calls to __StringEncrypt() in that line, but the password only appears twice. This is the same (I think) broken down for easier human understanding:

$sPass = "hyena"
$sString1 = "5D463233A5CA851675"
$sDecryptString1 = __StringEncrypt(0, $sString1, $sPass)
$sString2 = "55732C39A5DB9301"
$sDecryptString2 = __StringEncrypt(0, $sString2, $sPass)
$sIniRead = IniRead($htloadfullpath, $sDecryptString1, $sDecryptString2, "ERROR")
$sIniRead = __StringEncrypt(0, $sIniRead, $sPass)
MsgBox(0, "", $sIniRead)

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

PsaltyDS, you're way too nice to this guy. :)

I mean, this is ridiculous, 12 friggin pages. And now it has come down to fixing his dumb mistakes like that. I think OP has more important things to do before trying to encrypt anything. Such as... learning to code. And most importantly, actually giving some EFFORT. Something that I didn't notice even once through this entire thread.

Edited by Siao

"be smart, drink your wine"

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