Sign in to follow this  
Followers 0
Tsukihime

Convert binary to string with bit operations involved

5 posts in this topic

BinaryToString works nicely, except the binary string I have needs to be decrypted first.

This is the algorithm

1: BitXOR the string with FF mask

2: binaryToString the result

3: now I have a human-readable string

Except I'm stuck on the first part.

I can't come up with a way to split the binary value into an array of bytes so that I can XOR each byte and convert to string.

Any ideas?

Share this post


Link to post
Share on other sites



Great, I got an array representing the decrypted characters.

Now I tried _arrayToString, but all I get is the first letter.

That's odd...

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

You mean something like this?:

#include <Array.au3>
#include <String.au3>

$e = Encode("This is a secret text!")
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $e = ' & $e & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console
$d = Decode($e)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $d = ' & $d & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console

Func Encode($text, $xor = 0xFF)
    Local $aASCII = StringToASCIIArray($text), $i
    For $i = 0 To UBound($aASCII) - 1
        $aASCII[$i] = Hex(BitXOR(Hex($aASCII[$i], 2), $xor), 2)
    Next
    Return StringToBinary(_ArrayToString($aASCII, ""))
EndFunc

Func Decode($bString, $xor = 0xFF)
    Local $sCoded = BinaryToString($bString)
    Local $aASCII = StringRegExp($sCoded, ".{0,1}\S", 3)
    Local $s
    For $i = 0 To UBound($aASCII) - 1
        $s &= Chr(Dec(BitXOR("0x" & $aASCII[$i], $xor)))
    Next
    Return $s
EndFunc

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

I am confused about something.

Here's a simple script that inverts a "k" and writes out to a file

$outFile = FileOpen("test", 18)
$start = StringtoBinary("k")
$inverted = BitXOR($start, 0xFF)
FileWrite($outFile, chr($inverted))

However, in the file I get 81 instead of the 94 that I expected.

I tried this on two different computers and on one of them, it was 94. On the other, it was 81.

If I just say

$outFile = FileOpen("test", 18)
$start = StringtoBinary("k")
FileWrite($outFile, chr($start))

It says 6B, as expected.

I thought maybe I had to convert it to hex, so I also tried

$outFile = FileOpen("test", 18)
$start = StringtoBinary("k")
$inverted = hex(BitXOR($start, 0xFF))
FileWrite($outFile, chr($inverted))

But this time it gave me 5E

I expect an inverted "k" to be 94, but for some reason it's not working.

Edited by Tsukihime

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