rshad Posted March 26, 2018 Share Posted March 26, 2018 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 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 More sharing options...
spudw2k Posted March 26, 2018 Share Posted March 26, 2018 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 Spoiler Things I've Made: Always On Top Tool ◊ AU History ◊ Deck of Cards ◊ HideIt ◊ ICU ◊ Icon Freezer ◊ Ipod Ejector ◊ Junos Configuration Explorer ◊ Link Downloader ◊ MD5 Folder Enumerator ◊ PassGen ◊ Ping Tool ◊ Quick NIC ◊ Read OCR ◊ RemoteIT ◊ SchTasksGui ◊ SpyCam ◊ System Scan Report Tool ◊ System UpTime ◊ Transparency Machine ◊ VMWare ESX BuilderMisc Code Snippets: ADODB Example ◊ CheckHover ◊ Detect SafeMode ◊ DynEnumArray ◊ GetNetStatData ◊ HashArray ◊ IsBetweenDates ◊ Local Admins ◊ Make Choice ◊ Recursive File List ◊ Remove Sizebox Style ◊ Retrieve PNPDeviceID ◊ Retreive SysListView32 Contents ◊ Set IE Homepage ◊ Tickle Expired Password ◊ Transpose ArrayProjects: Drive Space Usage GUI ◊ LEDkIT ◊ Plasma_kIt ◊ Scan Engine Builder ◊ SpeeDBurner ◊ SubnetCalcCool Stuff: AutoItObject UDF ◊ Extract Icon From Proc ◊ GuiCtrlFontRotate ◊ Hex Edit Funcs ◊ Run binary ◊ Service_UDF Link to comment Share on other sites More sharing options...
rshad Posted March 26, 2018 Author Share Posted March 26, 2018 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 More sharing options...
rshad Posted March 26, 2018 Author Share Posted March 26, 2018 Nvm Replaced all Asc by AscW and Chr by ChrW and now is working Link to comment Share on other sites More sharing options...
rshad Posted March 26, 2018 Author Share Posted March 26, 2018 Well my mission was to make an encryption/decryption Delphi <-> Autoit but I'm seeing Autoit is handling data in a "weird way" so it can't work Any ideas about it? Link to comment Share on other sites More sharing options...
Bilgus Posted March 26, 2018 Share Posted March 26, 2018 (edited) 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 March 26, 2018 by Bilgus Updated to fixed function Link to comment Share on other sites More sharing options...
rshad Posted March 26, 2018 Author Share Posted March 26, 2018 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 Results Encrypting "test" with same "password" Link to comment Share on other sites More sharing options...
Bilgus Posted March 26, 2018 Share Posted March 26, 2018 (edited) 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 March 26, 2018 by Bilgus Link to comment Share on other sites More sharing options...
rshad Posted March 26, 2018 Author Share Posted March 26, 2018 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 I wouldn't be able to do that without your help, thank you sir! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now