Jump to content

Convert binary to string with bit operations involved


Recommended Posts

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?

Link to comment
Share on other sites

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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

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