Sign in to follow this  
Followers 0
0000

Convert C to AutoIt

19 posts in this topic

Hello, I need help to convert one C function to AutoIt, its Xor encryption. I would be very grateful, if smbody could help :)

void xor_crypt(char *string, const char *cryptkey, const int string.len, const int justkey)
{
   for(int n=0; n<=string.len; n++)
      string[n] ^= cryptkey[n%justkey];
}

Thank you for your help, ideas and so on :)

Share this post


Link to post
Share on other sites



Hello, I need help to convert one C function to AutoIt, its Xor encryption. I would be very grateful, if smbody could help :)

void xor_crypt(char *string, const char *cryptkey, const int string.len, const int justkey)
 {
    for(int n=0; n<=string.len; n++)
       string[n] ^= cryptkey[n%justkey];
 }

Thank you for your help, ideas and so on :)

SOmething like this I think.

#cs
    void xor_crypt(char * string, Const char * cryptkey, Const int string.len, Const int justkey)
    {
    For (int n = 0; n<=string.len; n++)
    string[n] ^ = cryptkey[n%justkey];
    }
#ce

Func xor_crypt(ByRef $string, ByRef $cryptkey, $stringlen, $justkey)

    Local $AStr = StringSplit($string, "")
    Local $Acrypt = StringSplit($cryptkey, "")
    $string = ''
    For $n = 1 To $stringlen
        $iStr = Asc($AStr[$n])
        $iCr = Asc($Acrypt[Mod($n, $justkey)]

        $string &= Chr(BitXOR($iStr, $iCr))
    Next
EndFunc  ;==>xor_crypt

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Func xor_crypt($string, $cryptkey)
    Local $stringarr = StringSplit($string, "")
    Local $cryptkeyarr = StringSplit($cryptkey, "")
    Local $n
    Local $ret = ""
    For $n = 1 To $stringarr[0]
        $ret &= Chr(BitXOR(Asc($stringarr[$n]), Asc($cryptkeyarr[Mod($n, $cryptkeyarr[0])])))
    Next
    Return $ret
EndFunc

Untested. See if that does what you want.

Whoops, Martin beat me. We both used the same idea though except that my string is a return value...

Edited by Richard Robertson

Share this post


Link to post
Share on other sites

Its really nice to see 2 methods. Thank you: Richard Robertson & martin :)

Richard Robertson's code is perfectly working, except that I need 2 more var's in it :)

and

martin's code returning some error, like this ;) :

Error parsing function call.:
$iCr = Asc($Acrypt[Mod($n, $justkey)]
$iCr = Asc($Acrypt[Mod($n, $justkey)^ ERROR

Share this post


Link to post
Share on other sites

Your original function needed the length of the strings. AutoIt has that kind of stuff handled automatically.

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Ok, thanks for explantion :) . Also I made a little test:

With C:

String: 12345678
CryptKey: test

Output:
0x45, 0x57, 0x40, 0x40, 0x35, 0xa, 0x34, 0x38

With AutoIt:

Dim $string = "12345678"
Dim $cryptK = "test"

Output: (After using _StringToHex)
455740004153440C
So split it:
0x45, 0x57,0x40, 0x00, 0x41, 0x53, 0x44, 0x0C

So the first few chars is correct by C module, and the others aren't, or I'm wrong :) ?

Edited by 0

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Strange. I wrote a function with StringToASCIIArray and the result is again different:

Func _string_xor($string, $cryptkey)
    ; Prog@ndy
    $string = StringToASCIIArray($string)
    $cryptkey = StringToASCIIArray($cryptkey,0,-1)
    Local $iString=UBound($string)-1, $iCrypt=UBound($cryptkey), $i
    For $i = 0 To $iString
        $string[$i] = (BitXOR($string[$i], $cryptkey[Mod($i,$iCrypt)]))
    Next
    Return StringFromASCIIArray($string)
EndFunc

Dim $string = "12345678"
Dim $cryptK = "test"
ConsoleWrite(Binary(_string_xor($string, $cryptK)) & @CRLF)

--> 0x455740404153444C

0x45, 0x57, 0x40, 0x40, 0x41, 0x53, 0x44, 0x4C

Edited by ProgAndy

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Share this post


Link to post
Share on other sites

Strange. I wrote a function with StringToASCIIArray and the result is again different:

Func _string_xor($string, $cryptkey)
    ; Prog@ndy
    $string = StringToASCIIArray($string)
    $cryptkey = StringToASCIIArray($cryptkey,0,-1)
    Local $iString=UBound($string)-1, $iCrypt=UBound($cryptkey), $i
    For $i = 0 To $iString
        $string[$i] = (BitXOR($string[$i], $cryptkey[Mod($i,$iCrypt)]))
    Next
    Return StringFromASCIIArray($string)
EndFunc

Dim $string = "12345678"
Dim $cryptK = "test"
ConsoleWrite(Binary(_string_xor($string, $cryptK)) & @CRLF)

--> 0x455740404153444C

0x45, 0x57, 0x40, 0x40, 0x41, 0x53, 0x44, 0x4C

Yours is right(er) though.

Both martin and Richard Robertson are doing a mistake by splitting up a string with default flag -0. It should be flag 2 and Ubound() used with them.

This way they are doing Asc(Ubound()) instead of Asc(first character).


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

I would assume it has something to do with the width of the data type in AutoIt. Or the lack thereof.

Trance, yes that is the problem but be more specific next time. It took me a minute to figure out which part you were talking about.

Also, it's not really a mistake. I don't work with AutoIt much anymore. I remember when string split didn't have a flag like that. Also, I hate UBound in AutoIt. It would be more appropriately named Length because UBound should be the upper most index that is accessible.

Edited by Richard Robertson

Share this post


Link to post
Share on other sites

Its really nice to see 2 methods. Thank you: Richard Robertson & martin :)

Richard Robertson's code is perfectly working, except that I need 2 more var's in it :)

and

martin's code returning some error, like this ;) :

Error parsing function call.:
 $iCr = Asc($Acrypt[Mod($n, $justkey)]
 $iCr = Asc($Acrypt[Mod($n, $justkey)^ ERROR
There's a closing bracket missing.

$iCr = Asc($Acrypt[Mod($n, $justkey)])

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

Yours is right(er) though.

Both martin and Richard Robertson are doing a mistake by splitting up a string with default flag -0. It should be flag 2 and Ubound() used with them.

This way they are doing Asc(Ubound()) instead of Asc(first character).

You are not reading my code correctly trancexx.

I use the default mode of 0 that is true, but I used elements 1 to $stringlen. There could be things wrong with my code but I don't think that is one of them.

I assumed that the parameter $stringlen was the number of characters of the string to be used, though I should have checked it wasn't greater than the length of $string.


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

You are not reading my code correctly trancexx.

I use the default mode of 0 that is true, but I used elements 1 to $stringlen. There could be things wrong with my code but I don't think that is one of them.

I assumed that the parameter $stringlen was the number of characters of the string to be used, though I should have checked it wasn't greater than the length of $string.

This:

$iCr = Asc($Acrypt[Mod($n, $justkey)])

What's Mod($justkey, $justkey)?

And what's $Acrypt[0] if

Local $Acrypt = StringSplit($cryptkey, "")

That's Ubound($Acrypt) instead of what it should be.

So, you have Asc(Ubound()).


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

This:

$iCr = Asc($Acrypt[Mod($n, $justkey)])

What's Mod($justkey, $justkey)?

And what's $Acrypt[0] if

Local $Acrypt = StringSplit($cryptkey, "")

That's Ubound($Acrypt) instead of what it should be.

So, you have Asc(Ubound()).

Is that just a strange way of telling me that I should have put

$iCr = Asc($Acrypt[Mod($n, $justkey)+1])

?

I can see that I made a mistake there, but otherwise the point you're trying to make is lost on me, and so is the way you're trying to do it.


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

Is that just a strange way of telling me that I should have put

$iCr = Asc($Acrypt[Mod($n, $justkey)+1])

?

I can see that I made a mistake there, but otherwise the point you're trying to make is lost on me, and so is the way you're trying to do it.

I'm sorry if I wasn't clear enough again. This is your code corrected* by me:

Func xor_crypt(ByRef $string, ByRef $cryptkey, $stringlen, $justkey)

    Local $AStr = StringSplit($string, "", 2)
    Local $Acrypt = StringSplit($cryptkey, "", 2)
    $string = ''
    For $n = 0 To $stringlen - 1
        $iStr = Asc($AStr[$n])
        $iCr = Asc($Acrypt[Mod($n, $justkey)])

        $string &= Chr(BitXOR($iStr, $iCr))
    Next
EndFunc ;==>xor_crypt

* to show what I mean

... compare results with ProgAndy's.


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

I'm sorry if I wasn't clear enough again. This is your code corrected* by me:

Func xor_crypt(ByRef $string, ByRef $cryptkey, $stringlen, $justkey)
 
     Local $AStr = StringSplit($string, "", 2)
     Local $Acrypt = StringSplit($cryptkey, "", 2)
     $string = ''
     For $n = 0 To $stringlen - 1
         $iStr = Asc($AStr[$n])
         $iCr = Asc($Acrypt[Mod($n, $justkey)])
 
         $string &= Chr(BitXOR($iStr, $iCr))
     Next
 EndFunc;==>xor_crypt

* to show what I mean

... compare results with ProgAndy's.

That is not my code corrected. I think it would be more sensible, and certainly be more acceptable as a critisism, if you pointed out the mistakes I made using the approach I chose rather than telling me to change my approach.

Why not

Func xor_crypt(ByRef $string, ByRef $cryptkey, $stringlen, $justkey)
 
     Local $AStr = StringSplit($string, "")
     Local $Acrypt = StringSplit($cryptkey, "")
     $string = ''
     For $n = 1 To $stringlen
         $iStr = Asc($AStr[$n])
         $iCr = Asc($Acrypt[Mod($n-1, $justkey)+1])
 
         $string &= Chr(BitXOR($iStr, $iCr))
     Next
 EndFunc;==>xor_crypt

which uses fewer characters and uses the way I chose to do it and I see no reason why I have to do it some other way unless you can convince me.


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

I pointed to a mistake in my first post here. Everything else was obviously just an attempt to show it to you since you said that I'm not reading your code correctly.

The code was wrong.

And if I said anything wrong please correct me.

Btw, I said corrected* and offered an additional explanation on the term in that context.


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

An a non-array method.

;
 #include <Array.au3>
 Dim $string = "12345678"
 Dim $cryptK = "test"
 
 ConsoleWrite(Binary(_string_xor($string, $cryptK)) & @CRLF)
 
 Func _string_xor($string, $cryptkey)
     Local $iCrypt = StringLen($cryptkey), $i, $sRet
     For $i = 0 To StringLen($string) - 1
         $sRet &= Hex(BitXOR(Asc(StringMid($string, $i + 1, 1)), Asc(StringMid($cryptkey, Mod($i, $iCrypt) + 1, 1))), 2);
     Next
     Return "0x" & $sRet
 EndFunc  ;==>_string_xor
;<array.au3>

Share this post


Link to post
Share on other sites

Thanks to everyone for his method and attempt to do this job :)

Also I would love if answer would be like this:

0x707760190703017945

Splitted to this:

0x70, 0x77, 0x60, 0x19, 0x07, 0x30, 0x01, 0x79

Thanks for try :)

Share this post


Link to post
Share on other sites

#19 ·  Posted (edited)

Thanks to everyone for his method and attempt to do this job ;)

Also I would love if answer would be like this:

0x707760190703017945

Splitted to this:

0x70, 0x77, 0x60, 0x19, 0x07, 0x30, 0x01, 0x79

Thanks for try ;)

This returns your desired format.

;
; Returns 0x45, 0x57, 0x40, 0x40, 0x41, 0x53, 0x44, 0x4C
  Dim $string = "12345678"
  Dim $cryptK = "test"
  
  ConsoleWrite(_string_xor($string, $cryptK) & @CRLF)
  
  Func _string_xor($string, $cryptkey)
      Local $iCrypt = StringLen($cryptkey), $i, $sRet
      For $i = 0 To StringLen($string) - 1
          $sRet &= "0x" & Hex(BitXOR(Asc(StringMid($string, $i + 1, 1)), Asc(StringMid($cryptkey, Mod($i, $iCrypt) + 1, 1))), 2) & ", "
      Next
      Return StringTrimRight($sRet, 2)
  EndFunc ;==>_string_xor
;
Edited by Malkey

Share this post


Link to post
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
Sign in to follow this  
Followers 0