Jump to content

Translating Encryption funtion to Autoit


 Share

Recommended Posts

Hi! I'm pretty new into Autoit, yet I wanted to translate a code from Delphi to Autoit but no luck :/

 

This is the Original working Delphi code
 

function EnDeCrypt(const Value, Key: ansistring): ansistring;
var
  i: Integer;
  KeyAlt: Integer;
begin

  KeyAlt := Length(Key);

  for i := 1 to Length(Key) do
    KeyAlt := KeyAlt xor Ord(Key[i]);

  Result := Value;
  for i := 1 to Length(Value) do
  begin
    Result[i] := ansichar(not (ord(Value[i]) xor Ord(KeyAlt)));
  end

end;

 

This is my failed try :P
 

MsgBox(0,_Encrypt('test','password'),'',0);

Func _Encrypt($Value, $Key)
    Local $i, $KeyAlt, $s_Encrypted,$xValue
    $KeyAlt = StringLen($Key)
   $xValue = $Value

  For $i = 1 To StringLen($Key)
     $s_Encrypted &= Chr(BitNOT (Asc(StringMid($xValue,$i,1)) BitXOR Asc($KeyAlt)))
  Next

    Return $s_Encrypted
EndFunc

 

Any help would be nice, thanks!

Link to comment
Share on other sites

Here's my stab and the issue.  The AutoIt code failed because of the syntax.  The usage of the BitXOR function has you put the values in as parameters rather than doing a XOR operation in between two values as the Delphi code shows.

I'm not sure what the output you are expecting is, but here is "my" corrected BitXOR function call.

MsgBox(0,"",_Encrypt('test','password'),0);

Func _Encrypt($Value, $Key)
    Local $i, $KeyAlt, $s_Encrypted,$xValue
    $KeyAlt = StringLen($Key)
    $xValue = $Value

    For $i = 1 To StringLen($Key)
        $s_Encrypted &= Chr(BitNot(BitXOR(Asc(StringMid($xValue,$i,1)),Asc($KeyAlt))))
    Next

    Return $s_Encrypted
EndFunc

 

Link to comment
Share on other sites

41 minutes ago, spudw2k said:

Here's my stab and the issue.  The AutoIt code failed because of the syntax.  The usage of the BitXOR function has you put the values in as parameters rather than doing a XOR operation in between two values as the Delphi code shows.

I'm not sure what the output you are expecting is, but here is "my" corrected BitXOR function call.

MsgBox(0,"",_Encrypt('test','password'),0);

Func _Encrypt($Value, $Key)
    Local $i, $KeyAlt, $s_Encrypted,$xValue
    $KeyAlt = StringLen($Key)
    $xValue = $Value

    For $i = 1 To StringLen($Key)
        $s_Encrypted &= Chr(BitNot(BitXOR(Asc(StringMid($xValue,$i,1)),Asc($KeyAlt))))
    Next

    Return $s_Encrypted
EndFunc

 

Hi! Following your example I've completed the rest of the code, but it returns corrupted data somehow.

Local $valtest
$valtest = _Encrypt('test','password'); EnCrypt string

MsgBox(0,"",_Encrypt($valtest,'password'),0); Decrypt string

Func _Encrypt($Value, $Key)
    Local $i, $KeyAlt, $s_Encrypted,$xValue
    $KeyAlt = StringLen($Key)
    $xValue = $Value

  for $i = 1 to StringLen($Key)
    $KeyAlt = BitXOR(Asc(StringMid($Key,$i,1)),Asc($KeyAlt))
  Next

    For $i = 1 To StringLen($Key)
        $s_Encrypted &= Chr(BitNot(BitXOR(Asc(StringMid($xValue,$i,1)),Asc($KeyAlt))))
    Next

    Return $s_Encrypted
EndFunc

 

Link to comment
Share on other sites

Couple Things..

First is ANSI string means a single byte per character string thus Chr() and Asc() were correct

Unicode strings use ChrW() and Ascw()

Next BinaryMid and BinaryLen is a bit easier to use in this context

Finally Did you know you can use DllStructSetData for data type conversions?

its return is the actual set value so its perfect for this use that was the final piece of this puzzle as this Xor Cipher used the overflow of 8-Bits to get the value

So when the number was < 0 it was actually 256 - num and > 255 it was actually 0+num

Local $valtest
;
$valtest = _Encrypt('test', 'password') ; EnCrypt string
ConsoleWrite("Crypt'd = " & StringToBinary($valtest) & @CRLF)
MsgBox(0, "", _Encrypt($valtest, 'password'), 0) ; Decrypt string

Func _Encrypt($vValue, $sKey)
    $tByte = DllStructCreate("BYTE") ;Unsigned 8 bit
    Local $s_Encrypted ;'$i' Doesn't need declared as it is local to the For
    Local $iKeyAlt = BinaryLen($sKey)

    For $i = 1 To $iKeyAlt
        $iKeyAlt = BitXOR(BinaryMid($sKey, $i, 1), $iKeyAlt)
    Next

    For $i = 1 To BinaryLen($vValue)
        ;(Ab) Using DllStructSetData to convert value to Unsigned Char
        $s_Encrypted &= Chr(DllStructSetData($tByte, 1, _
                BitNOT(BitXOR(BinaryMid($vValue, $i, 1), $iKeyAlt)))) ;
    Next

    Return $s_Encrypted
EndFunc   ;==>_Encrypt

 

Edited by Bilgus
Updated to fixed function
Link to comment
Share on other sites

7 hours ago, Bilgus said:

Couple Things..

First is ANSI string means a single byte per character string thus Chr() and Asc() were correct

Unicode strings use ChrW() and Ascw()

Next BinaryMid and BinaryLen is a bit easier to use in this context

Finally Did you know you can use DllStructSetData for data type conversions?

its return is the actual set value so its perfect for this use that was the final piece of this puzzle as this Xor Cipher used the overflow of 8-Bits to get the value

So when the number was < 0 it was actually 255 - num and > 255 it was actually 0+num

Local $valtest
$valtest = _Encrypt('test', 'password') ; EnCrypt string
ConsoleWrite ("Crypt'd = " & StringToBinary($valtest) & @CRLF)
MsgBox(0, "", _Encrypt($valtest, 'password'), 0) ; Decrypt string

Func _Encrypt($vValue, $sKey)
    $tByte = DllStructCreate("BYTE") ;Unsigned 8 bit
    Local $iKeyAlt, $s_Encrypted, $xValue ;'$i' Doesn't need declared as it is local to the For
    $iKeyAlt = BinaryLen($sKey)
    $xValue = $vValue

    For $i = 1 To $iKeyAlt
        $iKeyAlt = BitXOR(BinaryMid($sKey, $i, 1), $iKeyAlt)
    Next

    For $i = 1 To BinaryLen($vValue)
        ;(Ab) Using DllStructSetDAta to convert valut to Unsigned Char
        $s_Encrypted &= Chr(DllStructSetData($tByte, 1, BitNOT(BitXOR(BinaryMid($xValue, $i, 1), Asc($iKeyAlt))))) ;
    Next

    Return $s_Encrypted
EndFunc   ;==>_Encrypt

 

The unicode problem is fixed, thanks!

But still the function returns different data in Autoit <-> Delphi so is not compatible :think:

Results Encrypting "test" with same "password"

1a.png.85f6b18d0b062efb3e54ca673f7a1af8.png

1d.png.c7646926bc3478707ff42f353c76cfde.png

 

Link to comment
Share on other sites

Nah, Just an error on my part but having the same string between the two made it easy to spot, I was converting $iKeyAlt to asc() but its already an integer

Edited first post with updated function

Edited by Bilgus
Link to comment
Share on other sites

2 hours ago, Bilgus said:

Nah, Just an error on my part but having the same string between the two made it easy to spot, I was converting $iKeyAlt to asc() but its already an integer

Local $valtest
;
$valtest = _Encrypt('test', 'password') ; EnCrypt string
ConsoleWrite ("Crypt'd = " & StringToBinary($valtest) & @CRLF)
MsgBox(0, "", _Encrypt($valtest, 'password'), 0) ; Decrypt string

Func _Encrypt($vValue, $sKey)
    $tByte = DllStructCreate("BYTE") ;Unsigned 8 bit
    Local $iKeyAlt, $s_Encrypted, $xValue ;'$i' Doesn't need declared as it is local to the For
    $iKeyAlt = StringLen($sKey)

    For $i = 1 To $iKeyAlt
        $iKeyAlt = BitXOR(BinaryMid($sKey, $i, 1), $iKeyAlt)
    Next

    For $i = 1 To BinaryLen($vValue)
        ;(Ab) Using DllStructSetDAta to convert valut to Unsigned Char
        $s_Encrypted &= Chr(DllStructSetData($tByte, 1, BitNOT(BitXOR(BinaryMid($vValue, $i, 1), $iKeyAlt)))) ;
    Next

    Return $s_Encrypted
EndFunc   ;==>_Encrypt

 

Now is working perfectly :D I wouldn't be able to do that without your help, thank you sir!

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