Jump to content

C++ XOR encryption function to AutoIt


Recommended Posts

I'd like to translate a piece of C++ code into AutoIt. Well,the C++ source is a CTRL+C & CTRL+V for me, I don't really know how it works, that's why I can't really translate it. Well, my huge fault, but still, this code would be really important for me!

So, it's a simple XOR encryption. Another problem is that I can't yet understand, how the hell it work (I mean wtf is this XOR)... Well, I'm trying to learn it, but meanwhile I post this topic. Here's the C++ source:

string encryptDecrypt(string toEncrypt)
{
    char key[16] = { 'x', 'Y', 'b', 'N', 'k', 'k', 'F', 'X', 'x', ']', 's', 't', 'o', 'a', 'V', 'c' };
    string output = toEncrypt;

    for (int i = 0; i < toEncrypt.size(); i++)
        output[i] = toEncrypt[i] ^ key[i % (sizeof(key) / sizeof(char))];

    return output;
}

Well, for loop is clear, I have to XOR each letter with a key. But what Is the key? I mean there's an array, but what the hell is this:

i % (sizeof(key) / sizeof(char))

I hope you gus can help me. Thanks a lot!

( I know it isn't that good not to provide an autoit code which I've written, but anyone can write a for loop, it's not that big deal, and what I don't know is the hearth of the code :) )

Link to comment
Share on other sites

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Yes, I can use google too :) I can understand how to XOR for example two unions in maths, but I still get mindf*ck when reading that wiki post :D

Could you help me how to translate that

toEncrypt[i] ^ key[i % (sizeof(key) / sizeof(char))];

part? I have to XOR each letter with the key. But what is the key?

Link to comment
Share on other sites

Looks like the key is the key array, you're stepping through the array one element at a time, and stepping through the string on character at a time. Then XORing the string with the array value.

BTW, there's a BitXOr function that might help in this.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Yes, I've seen the BitXOR command, I just wasn't sure what's the key. So you mean that I XOR each character the following way:

1st char: Key 1st letter (x)

2nd char: Key 2nd letter (Y)

...

...

16th char: Key 16th letter ( c )

17th char: Key 1st letter again (x)

...

...

Edited by Unc3nZureD
Link to comment
Share on other sites

From what you posted that's what I'm guessing. I'm not all that good with C/C++ either so I'm going on limited knowledge, but it appears to work that way.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

I made this little script:

#include <Array.au3>

$text = FileRead(@ScriptDir & "\log.txt")
$text = "Welcome to my little world!"

$arr = StringSplit($text, "", 2)
$key = StringSplit('BzWul]ISL[SWXHvd', "", 2)

$keyindex = 0
For $i = 0 To UBound($arr) - 1
    $keyindex += 1
    If $keyindex = UBound($key) Then
        $keyindex = 0
    EndIf

    $arr[$i] = BitXOR($arr[$i], $key[$keyindex])
Next

$b = _ArrayToString($arr, "")
MsgBox(0,"", $b, "Decoded:")
ClipPut($b)

The originally encrypted text, which should be decrypted is in the log.txt, but it isn't working, so I tried to set a simple text if it works. I have to say no. Any idea what could be the problem? It returns: 000000000000000000000000000

I tried to replace the BitXOR with this:

$arr[$i] = $arr[$i] ^ $key[$keyindex]

But it returns the same, except that all of them are 111111...

What did I miss?

_________________________________________________

As I see, I can XOR only HEX stuffs. How come c++ is able to XOR it meanwhile it's still char? Does it auto converts it? Anyways I still couldn't write a working function :/

Edited by Unc3nZureD
Link to comment
Share on other sites

Woooohooo :D I've finished it :D More or less I understand it now :P Here's my code if anyone needs it:

#include <Array.au3>
$key = "AnyKeyIsWrittenHere"
$xor = XOREncrypt("Welcome to my little world!", $key)
$dexor = XOREncrypt($xor, $key)

MsgBox(0,"", $xor, "Decoded:")
MsgBox(0,"", $dexor, "Decoded:")
ClipPut($xor)

Func XOREncrypt($data, $key)
    $arr = StringSplit($data, "", 2)
    $key = StringSplit($key, "", 2)

    ArrayToHex($arr)
    ArrayToHex($key)

    $keyindex = 0
    For $i = 0 To UBound($arr) - 1
        $keyindex += 1
        If $keyindex = UBound($key) Then
            $keyindex = 0
        EndIf

        $arr[$i] = Hex( BitXOR($arr[$i], $key[$keyindex]), 2 )
    Next

    return BinaryToString("0x" & _ArrayToString($arr, ""))
EndFunc

Func ArrayToHex(ByRef $array)
    For $i = 0 to UBound($array) - 1
        $array[$i] = StringToBinary($array[$i])
    Next
EndFunc

(ArrayToHex is just a small helping function)

___________________________________________________________________

Edit:

Well, it took some hours to understand how it works :D Well, the only thing is that it won't decrypt the code I've got in C++.

I attached a sample if anyone needs it. It has been encrypted with the following char array: BzWul]ISL[sWXHvd

What did I miss again? :)

log.txt

Edited by Unc3nZureD
Link to comment
Share on other sites

1st: BUMP

2nd: I looked thru the c++ code again. I rechecked the following string

output[i] = toEncrypt[i] ^ key[i % (sizeof(key) / sizeof(char))];

This one encrypts the char with the key. It make the following thing in autoit, if I'm right:

key[i % (sizeof(key) / sizeof(char))]

   |

  /

$key[Mod($i, 16)] ; 16, because sizeof(key) might be 16, and If I'm right sizeof(char) is 1.

Could anyone confirm me? Just because it's not working too...

$arr[$i] = Hex( BitXOR($arr[$i], $key[Mod($i, 16)]), 2 )
Link to comment
Share on other sites

This is how I would code it:

Local $sPlaintext = "I'd like to translate a piece of C++ code into AutoIt. Well,the C++ source is a CTRL+C & CTRL+V for me, I don't really know how it works, that's why I can't really translate it. Well, my huge fault, but still, this code would be really important for me!" & @CRLF & _
"So, it's a simple XOR encryption. Another problem is that I can't yet understand, how the hell it work (I mean wtf is this XOR)... Well, I'm trying to learn it, but meanwhile I post this topic. Here's the C++ source:"

Global $sPad = "Could anyone confirm me? Just because it's not working too..."

Local $sCiphertext = _Vernam($sPlaintext)
ConsoleWrite("Ciphertext: " &  Binary($sCiphertext) & @LF & @LF)
ConsoleWrite("Recovered plaintext: " & _Vernam($sCiphertext) & @LF)

Func _Vernam($sIn)
    Local $aIn = StringToASCIIArray($sIn)
    Local $aPad = StringToASCIIArray($sPad)
    Local $sOut
    For $i = 0 To UBound($aIn) - 1
        $sOut &= ChrW(BitXOR($aIn[$i], $aPad[Mod($i, UBound($aPad))]))
    Next
    Return $sOut
EndFunc

Anyway, keep in mind that your key definitely needs to be at least as long as the plaintext, must be used only once and needs to have pretty good randomness. All criterions that this toy example misses. Forget any condition and you're going to be severely bitten.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

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