Jump to content
Rex

Simple Text Scrambler, using StringToAsciiArray and random math

Recommended Posts

Rex

Hi

This is a txt scrambler I wrote long ago, but it just resurfaced when going thru some old code, so I thought that I would share it with you.

; #INDEX# =======================================================================================================================
; Title .........: Simple Txt (un)Scrambler
; Script Version : 1.0.0
; AutoIt Version : 3.3.14.0 - Created with ISN AutoIt Studio v. 1.04
; Description ...: Takes a string and scrambles it, using StringToAsciiArray, Random Multiply, Random letters and BitXOR
;                : The return string contains it's own "unscramble" keys, wich is used by the Unscramble funktion to reverse
;                : the scrambled string to the orginale string.
; Author(s) .....: Rex
; ===============================================================================================================================

; #CHANGE LOG# ==================================================================================================================
; Changes .......:
; Date ..........:
; ===============================================================================================================================

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

; Excaple

Local $sString = 'Hello World'
; Scramble string
Local $sScrambled = _ScrampleString($sString)
ConsoleWrite(@CRLF & 'Scrambled Msg = ' & $sScrambled & @CRLF)

; Unscramble string
ConsoleWrite(@CRLF & 'Unscrambled string = ' & _UnScrampleString($sScrambled) & @CRLF)


; Scramble function
Func _ScrampleString($sMsg)
; Converts the string to an Ascii array
Local $aMsg = StringToASCIIArray($sMsg)
; reverses the Array
_ArrayReverse($aMsg)

; Scramble the string
Local $sSMsg, $iMultiplyWithFirst = Random(10, 99, 1), $iMultiplyWithLast = Random(100, 999, 1)

; Scramble the string by multiply some of the Ascii numbers with x and again others with y
; Looping thru the ascii array
For $i = 0 To UBound($aMsg) -1
    ; Multiplying some of the numbers
    If IsInt($i/3)  then ; If $i dived by 3 is an int. The divede by could be any number
        $aMsg[$i] = $aMsg[$i] * $iMultiplyWithFirst
    ElseIf IsInt($i /6) Then ; If $i diveded by 6 is int. The divede by could be any number
        $aMsg[$i] = $aMsg[$i] * $iMultiplyWithLast
    EndIf
    ; Creating the output string
    ; We do a bitXOR, and add Random letters to the sting
    $sSMsg &= BitXOR($aMsg[$i],2) & __RandomChar()
Next
; Combine and return scrambled string
Return $iMultiplyWithFirst & $sSMsg & $iMultiplyWithLast
EndFunc

; Internal use for Scramble funktion
Func __RandomChar()
    ; Generates a series of random chars (A-Z) in the range of 1 to 3 in a group
    Local $sRandChar
    For $i = 0 To Random(1, 3,1)
        $sRandChar &= chr(Random(65, 90, 1))
    Next
    Return $sRandChar
EndFunc

Func _UnScrampleString($sSMsg)
; Get the Multiplyer
Local $iDivideWithFirst = StringLeft($sSMsg, 2)
Local $iDivideWithLast = StringRight($sSMsg, 3)

;Removing the multiplyer
$sSMsg = StringTrimLeft($sSMsg, 2)
$sSMsg = StringTrimRight($sSMsg, 3)

; Replaces all Chars from the string with whitespaces
Local $sNoChars = StringRegExpReplace($sSMsg, '\D', ' ')

; Removing 2+ whitspaces, stripping the last char from the string and splits the string into an array (Using the no count flag)
Local $aSmsg = StringSplit(StringTrimRight(StringStripWS($sNoChars, $STR_STRIPSPACES), 1), ' ', $STR_NOCOUNT)

; Cleaning up the array
For $i = 0 To UBound($aSmsg) -1
    ; Reversing the BitXOR
    $aSmsg[$i] = BitXOR($aSmsg[$i],2)
    ; Recalculating the orginal numbers
    If IsInt($i/3)  then
        $aSmsg[$i] = $aSmsg[$i] / $iDivideWithFirst
    ElseIf IsInt($i /6) Then
        $aSmsg[$i] = $aSmsg[$i] / $iDivideWithLast
    EndIf
Next

; Now all we need is to change the numbers to chars, so we get the original string
Local $sOrgMsg
For $i = 0 To UBound($aSmsg) -1
        $sOrgMsg &= Chr($aSmsg[$i])
    Next
    Return StringReverse($sOrgMsg)
EndFunc

 

Cheers

/Rex

Share this post


Link to post
Share on other sites
jchd

Remember AutoIt uses Unicode (actually UCS2), so your script won't work in many use cases, assuming there is some.

Edited by jchd
  • Like 1

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)

Share this post


Link to post
Share on other sites
Chimp

funny, time ago I wrote two (simpler) similar snippets

Example()

Func Example()
    Local $sMyString = "The quick brown fox jumps over the lazy dog" & @CR & _
            "Η γρήγορη καφέ αλεπού πηδάει πάνω από το τεμπέλικο σκυλί" & @CR & _
            "Быстрая коричневая лиса прыгает через ленивую собаку" & @CR & _
            "الثعلب البني السريع يقفز فوق الكلب الكسول" & @CR & _
            "Արագ շագանակագույն տոհմը ցատկում է ծույլ շունի վրա" & @CR & _
            "敏捷的棕色狐狸跳過了懶狗"

    $sExample = "Original string:" & @CRLF
    $sExample &= $sMyString & @CRLF & @CRLF

    Local $sScrambled = _Scramble($sMyString)
    $sExample &= "Scrambled string:" & @CRLF
    $sExample &= $sScrambled & @CRLF & @CRLF

    Local $sUnscrabled = _UnScramble($sScrambled)
    $sExample &= "Back to original string:" & @CRLF
    $sExample &= $sUnscrabled & @CRLF & @CRLF

    MsgBox(0, "Example", $sExample)
EndFunc   ;==>Example

; _Scramble() and _UnScramble() are interchangeable
Func _Scramble($sString)
    Local $sScrambled = ""
    For $i = 1 To StringLen($sString)
        $sScrambled &= ChrW(Dec(Hex(BitNOT(AscW(StringMid($sString, $i, 1))), 4)))
    Next
    Return $sScrambled
EndFunc   ;==>_Scramble

Func _UnScramble($sString)
    Return _Scramble($sString)
EndFunc   ;==>_UnScramble

 

Edited by Chimp
_UnScramble and _Scramble are interchangeable

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites
Rex
12 hours ago, jchd said:

Remember AutoIt uses Unicode (actually UCS2), so your script won't work in many use cases, assuming there is some.

Hi jschd

Sry, you lost me there :> I have used this in some of my later scripts, and had no problem with it, and I use std UTF-8.

Or do you think if one uses chars that don't exist in the ascii table? I newer thought about that there could be differences between it you use one or another char table, but of course if one uses some special chars from the UTF-8 table that don't exists in the ascii table, I guess my function will fail.

I my self have used it for scrambling msgbox's , and other type of plain txt in my scripts, to make it just a bit harder to read the code if some one should extract it from exe file.

 

Cheers

/Rex

Share this post


Link to post
Share on other sites
Rex
11 hours ago, Chimp said:

funny, time ago I wrote two (simpler) similar snippets

Cool snippet, uses one function to scramble and unscramble, just shows that my Autoit scripting abilities still needs some trimming :)

 

Cheers

/Rex

Share this post


Link to post
Share on other sites
jchd
21 minutes ago, Rex said:

Or do you think if one uses chars that don't exist in the ascii table? I newer thought about that there could be differences between it you use one or another char table, but of course if one uses some special chars from the UTF-8 table that don't exists in the ascii table, I guess my function will fail.

That's my point.

BTW AutoIt native strings are UCS2 (a subset of full UTF16-LE) and not UTF8. Don't confuse the encoding of the source file and AutoIt internal representation of strings. The string 'A' inside AutoIt uses one 16-bit wchar to represent.


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)

Share this post


Link to post
Share on other sites
Rex

Argg, all those different char tables.... I really miss the good old days where an a was an a and b was b :D

But yes now I understand what you meant, my function only works for input that ranges within the ascii table, really never thought about that limit. :>

 

Cheers

/Rex

Share this post


Link to post
Share on other sites
jchd

Unicode was precisely invented to solve the codepage mess. It nonetheless has its drawback.


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)

Share this post


Link to post
Share on other sites
Chimp
10 hours ago, Rex said:

... my function only works for input that ranges within the ascii table ...

... a quick fix for your function:
using the ChrW() function instead of the Chr() function in line 98 of your listing should fix.
try with the input string from post #3 and use an MsgBox() to view the result, since the ConsoleWrite function, as stated in remarks of the help, converts characters to ANSI before being written.

Edited by Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites
Rex

Thx to jchd that pointed out that my function didn't work with "advance" Unicode chars, and Chimp to point the way to handle Unicode chars.

 

I rewrote the function so it now handles Unicode :), and fixed some spelling errors :>

; #INDEX# =======================================================================================================================
; Title .........: Simple Txt (un)Scrambler
; Script Version : 1.0.1
; AutoIt Version : 3.3.14.0 - Created with ISN AutoIt Studio v. 1.04
; Description ...: Takes a string and scrambles it, by creating an unicode Array, Random Multiply, Random letters and BitXOR
;                : The return string contains it's own "unscramble" keys, wich is used by the Unscramble funktion to reverse
;                : the scrambled string to the orginale string.
; Author(s) .....: Rex
; ===============================================================================================================================

; #CHANGE LOG# ==================================================================================================================
; Changes .......: Ascii -> Unicode, as jchd pointed out the script didn't work with "advance" unicode chars
; Date ..........: 12-02-2018
; ===============================================================================================================================

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

; Example

;Scramble
Local $sMyString = "The quick brown fox jumps over the lazy dog" & @CR & _
            "Η γρήγορη καφέ αλεπού πηδάει πάνω από το τεμπέλικο σκυλί" & @CR & _
            "Быстрая коричневая лиса прыгает через ленивую собаку" & @CR & _
            "الثعلب البني السريع يقفز فوق الكلب الكسول" & @CR & _
            "Արագ շագանակագույն տոհմը ցատկում է ծույլ շունի վրա" & @CR & _
            "敏捷的棕色狐狸跳過了懶狗" ; String "stolen" from Chimp's example string, as I hadn't any unicode chars at hand

$sUniScramble = _ScrambleString($sMyString)
; Unscramble
MsgBox(0, "String Unscrambled", _UnScrambleString($sUniScramble))


; Scramble function
Func _ScrambleString($sMsg)
; Split the string
Local $aMsgSplit = StringSplit($sMsg, '', $STR_NOCOUNT)
; Create a loop to get the Unicode char number
Local $aiMsg[UBound($aMsgSplit)]
For $i = 0 To UBound($aMsgSplit) - 1
    $aiMsg[$i] = AscW($aMsgSplit[$i])
Next
; reverses the Array
_ArrayReverse($aiMsg)

; Scramble the string
Local $sSMsg, $iMultiplyWithFirst = Random(10, 99, 1), $iMultiplyWithLast = Random(100, 999, 1)

; Scramble the string by multiply some of the Ascii numbers with x and again others with y
; Looping thru the ascii array
For $i = 0 To UBound($aiMsg) -1
    ; Multiplying some of the numbers
    If IsInt($i/3)  then ; If $i dived by 3 is an int. The divede by could be any number
        $aiMsg[$i] = $aiMsg[$i] * $iMultiplyWithFirst
    ElseIf IsInt($i /6) Then ; If $i diveded by 6 is int. The divede by could be any number
        $aiMsg[$i] = $aiMsg[$i] * $iMultiplyWithLast
    EndIf
    ; Creating the output string
    ; We do a bitXOR, and add Random letters to the sting
    $sSMsg &= BitXOR($aiMsg[$i],2) & __RandomChar()
Next
; Combine and return scrambled string
Return $iMultiplyWithFirst & $sSMsg & $iMultiplyWithLast
EndFunc

; Internal use for Scramble funktion
Func __RandomChar()
    ; Generates a series of random chars (A-Z) in the range of 1 to 3 in a group
    Local $sRandChar
    For $i = 0 To Random(1, 3,1)
        $sRandChar &= ChrW(Random(65, 90, 1))
    Next
    Return $sRandChar
EndFunc

Func _UnScrambleString($sSMsg)
; Get the Multiplyer
Local $iDivideWithFirst = StringLeft($sSMsg, 2)
Local $iDivideWithLast = StringRight($sSMsg, 3)

;Removing the multiplyer
$sSMsg = StringTrimLeft($sSMsg, 2)
$sSMsg = StringTrimRight($sSMsg, 3)

; Replaces all Chars from the string with whitespaces
Local $sNoChars = StringRegExpReplace($sSMsg, '\D', ' ')

; Removing 2+ whitspaces, stripping the last char from the string and splits the string into an array (Using the no count flag)
Local $aSmsg = StringSplit(StringTrimRight(StringStripWS($sNoChars, $STR_STRIPSPACES), 1), ' ', $STR_NOCOUNT)

; Cleaning up the array
For $i = 0 To UBound($aSmsg) -1
    ; Reversing the BitXOR
    $aSmsg[$i] = BitXOR($aSmsg[$i],2)
    ; Recalculating the orginal numbers
    If IsInt($i/3)  then
        $aSmsg[$i] = $aSmsg[$i] / $iDivideWithFirst
    ElseIf IsInt($i /6) Then
        $aSmsg[$i] = $aSmsg[$i] / $iDivideWithLast
    EndIf
Next

; Now all we need is to change the numbers to chars, so we get the original and unscrambled string
Local $sOrgMsg
For $i = 0 To UBound($aSmsg) -1
        $sOrgMsg &= ChrW($aSmsg[$i])
    Next
    Return StringReverse($sOrgMsg)
EndFunc

 

Cheers

/Rex

Edited by Rex

Share this post


Link to post
Share on other sites
jchd

I didn't check the maths behind the hood but you'd rather make sure you can never produce invalid Unicode codepoints after your arithmetic. Refer to Unicode standards about this.


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)

Share this post


Link to post
Share on other sites
Rex

@jchd:

The math should be stable enough to prevent that :)

 

Cheers

/Rex

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

×