Jump to content

Base91 and Base128 Functions


Beege
 Share

Recommended Posts

Here's  Base91 and Base128 functions I ported from code I found on the net. Quick search of the forum did not pull up anything so don't think these have been posted before. Both just for fun. The base128 is pretty straight forward as its pretty much the same concept as base64 only using 7bits instead of 6, but I thought the base91 was real interesting and Im actually still trying to wrap my head around the algorithm. The functions Ive posted are shortened/combined a bit so if your gonna try and learn the flow I would break it back down to one operation per line. Let me know if you find a string it does not work with. I mainly wanted them for embedding dlls in scripts and the the results are below for a dll that I tested.  Have fun!

 

Base91:

; ; #FUNCTION# ====================================================================================================================
; Name...........: _Base91Encode
; Description ...: Encodes string to Base91
; Author ........: Brian J Christy (Beege)
; Source ........: http://base91.sourceforge.net/  [Joachim Henke]
; ===============================================================================================================================
Func _Base91Encode($sStr)

    Local $aB91 = StringSplit('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~"', '', 2)
    Local $sEncoded, $b, $n, $v, $aStr = StringToASCIIArray($sStr)

    For $i = 0 To UBound($aStr) - 1
        $b = BitOR($b, BitShift($aStr[$i], ($n * - 1)))
        $n += 8
        If $n > 13 Then
            $v = BitAND($b, 8191)
            $s = 13 + ($v <= 88)
            If $v <= 88 Then $v = BitAND($b, 16383)
            $b = BitShift($b, $s)
            $n -= $s
            $sEncoded &= $aB91[Mod($v, 91)] & $aB91[$v / 91]
        EndIf
    Next

    If $n Then
        $sEncoded &= $aB91[Mod($b, 91)]
        If $n > 7 Or $b > 90 Then $sEncoded &= $aB91[$b / 91]
    EndIf

    Return $sEncoded

EndFunc   ;==>_Base91Encode

; #FUNCTION# ====================================================================================================================
; Name...........: _Base91Encode
; Description ...: Decodes string from Base91
; Author ........: Brian J Christy (Beege)
; Source ........: http://base91.sourceforge.net/  [Joachim Henke]
; ===============================================================================================================================
Func _Base91Decode($sStr)

    Local $sB91 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~"'
    Local $sDecoded, $n, $c, $b, $v = -1, $aStr = StringSplit($sStr, '', 2)

    For $i = 0 To UBound($aStr) - 1
        $c = StringInStr($sB91, $aStr[$i], 1) - 1
        If $v < 0 Then
            $v = $c
        Else
            $v += $c * 91
            $b = BitOR($b, BitShift($v, ($n * - 1)))
            $n += 13 + (BitAND($v, 8191) <= 88)
            Do
                $sDecoded &= Chr(BitAND($b, 255))
                $b = BitShift($b, 8)
                $n -= 8
            Until Not ($n > 7)
            $v = -1
        EndIf
    Next

    If ($v + 1) Then $sDecoded &= Chr(BitAND(BitOR($b, BitShift($v, ($n * - 1))), 255))

    Return $sDecoded

EndFunc   ;==>_Base91Decode
 

Base128:

; #FUNCTION# ====================================================================================================================
; Name...........: _Base128Encode
; Description ...: Decodes string from Base128
; Author ........: Brian J Christy (Beege)
; Source ........: https://github.com/seizu/base128/blob/master/base128.php  [Erich Pribitzer]
; ===============================================================================================================================
Func _Base128Encode($sStr)

    Local $aB128 = StringSplit('!#$%()*,.0123456789:;=@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_abcdefghijklmnopqrstuvwxyz{|}~¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎ', '', 2)
    Local $sEncoded, $ls, $r, $rs = 7, $aStr = StringToASCIIArray($sStr & ' ')

    For $i = 0 To UBound($aStr) - 1
        If $ls > 7 Then
            $i -= 1
            $ls = 0
            $rs = 7
        EndIf
        $nc = BitOR(BitAND(BitShift($aStr[$i], ($ls * -1)), 0x7f), $r)
        $r = BitAND(BitShift($aStr[$i], $rs), 0x7f)
        $rs -= 1
        $ls += 1
        $sEncoded &= $aB128[$nc]
    Next

    Return $sEncoded

EndFunc   ;==>_Base128Encode

; #FUNCTION# ====================================================================================================================
; Name...........: _Base91Encode
; Description ...: Decodes string from Base128
; Author ........: Brian J Christy (Beege)
; Source ........: https://github.com/seizu/base128/blob/master/base128.php  [Erich Pribitzer]
; ===============================================================================================================================
Func _Base128Decode($sStr)

    Local $sB128 = '!#$%()*,.0123456789:;=@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_abcdefghijklmnopqrstuvwxyz{|}~¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎ'
    Local $sDecoded, $r, $rs = 8, $ls = 7, $aStr = StringSplit($sStr, '', 2)

    For $i = 0 To UBound($aStr) - 1
        $nc = StringInStr($sB128, $aStr[$i], 1) - 1
        If $rs > 7 Then
            $rs = 1
            $ls = 7
            $r = $nc
            ContinueLoop
        EndIf
        $r1 = $nc
        $nc = BitOR(BitAND(BitShift($nc, ($ls * -1)), 0xFF), $r)
        $r = BitShift($r1, $rs)
        $rs += 1
        $ls -= 1
        $sDecoded &= Chr($nc)
    Next

    Return $sDecoded

EndFunc   ;==>_Base128Decode
Dll Size Tests:

Binary Len  =   57346
Base64 Len  =   38232
Base91 Len  =   35180
Base128 Len =   32768
Base91 - Base128.au3

Edit: Fixed Base91 error pointed out by trancexx

Edited by Beege
Link to comment
Share on other sites

Base91 is really interesting.

Unfortunatly something like this doesn't work:

$sStr = FileRead(@ScriptFullPath)

ConsoleWrite(_Base91Decode(_Base91Encode($sStr)) & @CRLF)



; #FUNCTION# ====================================================================================================================
; Name...........: _Base91Encode
; Description ...: Encodes string to Base91
; Author ........: Brian J Christy (Beege)
; Source ........: http://base91.sourceforge.net/  [Joachim Henke]
; ===============================================================================================================================
Func _Base91Encode($sStr)

    Local $aB91 = StringSplit('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~"', '', 2)
    Local $sEncoded, $b, $n, $v, $s, $aStr = StringToASCIIArray($sStr)

    For $i = 0 To UBound($aStr) - 1
        $b = BitOR($b, BitShift($aStr[$i], ($n * -1)))
        $n += 8
        If $n > 13 Then
            $v = BitAND($b, 8191)
            If ($v <= 88) Then $v = BitAND($b, 16383)
            $s = 13 + ($v <= 88)
            $b = BitShift($b, $s)
            $n -= $s
            $sEncoded &= $aB91[Mod($v, 91)] & $aB91[$v / 91]
        EndIf
    Next

    If $n Then
        $sEncoded &= $aB91[Mod($b, 91)]
        If $n > 7 Or $b > 90 Then $sEncoded &= $aB91[$b / 91]
    EndIf

    Return $sEncoded

EndFunc   ;==>_Base91Encode

; #FUNCTION# ====================================================================================================================
; Name...........: _Base91Encode
; Description ...: Decodes string from Base91
; Author ........: Brian J Christy (Beege)
; Source ........: http://base91.sourceforge.net/  [Joachim Henke]
; ===============================================================================================================================
Func _Base91Decode($sStr)

    Local $sB91 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~"'
    Local $sDecoded, $n, $c, $b, $v = -1, $aStr = StringSplit($sStr, '', 2)

    For $i = 0 To UBound($aStr) - 1
        $c = StringInStr($sB91, $aStr[$i], 1) - 1
        If $v < 0 Then
            $v = $c
        Else
            $v += $c * 91
            $b = BitOR($b, BitShift($v, ($n * -1)))
            $n += 13 + (BitAND($v, 8191) <= 88)
            Do
                $sDecoded &= Chr(BitAND($b, 255))
                $b = BitShift($b, 8)
                $n -= 8
            Until Not ($n > 7)
            $v = -1
        EndIf
    Next

    If ($v + 1) Then $sDecoded &= Chr(BitAND(BitOR($b, BitShift($v, ($n * -1))), 255))

    Return $sDecoded

EndFunc   ;==>_Base91Decode

Did you just translate the code from another language or made it by following documentation.

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

Hey good catch. Thanks! Ill be looking at this later to see if I can pinpoint the problem. - If its my code or not. 

Just a translation from other code  :evil:. Surprisingly base91 really didn't have any documentation that I could find other than what the code does. 

Link to comment
Share on other sites

Link to comment
Share on other sites

  • 2 years later...
_Example()

Func _Example()

        Local $sTest = "bad49c8aa9505e47ee6176feb1ec1aa9"

        $sEnc = _Base128Encode($sTest)
        $sDec = _Base128Decode($sEnc)
        If $sDec = $sTest Then
                ConsoleWrite($sEnc & @LF & $sDec & @LF & '**Test Passed**' & @LF)
        Else
                ConsoleWrite($sEnc & @LF & $sDec & @LF & '**Test Failed**' & @LF)
        EndIf

EndFunc   ;==>_Example

; #FUNCTION# ====================================================================================================================
; Name...........: _Base128Encode
; Description ...: Decodes string from Base128
; Author ........: Brian J Christy (Beege)
; Source ........: https://github.com/seizu/base128/blob/master/base128.php  [Erich Pribitzer]
; ===============================================================================================================================
Func _Base128Encode($sStr)

    Local $aB128 = StringSplit('!#$%()*,.0123456789:;=@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_abcdefghijklmnopqrstuvwxyz{|}~?¢£¤¥|§¨?a???ˉ°±23′μ?·?1o?????àá??????èéê?ìí?', '', 2)
    Local $sEncoded, $ls, $r, $rs = 7, $aStr = StringToASCIIArray($sStr & ' ')

    For $i = 0 To UBound($aStr) - 1
        If $ls > 7 Then
            $i -= 1
            $ls = 0
            $rs = 7
        EndIf
        $nc = BitOR(BitAND(BitShift($aStr[$i], ($ls * - 1)), 0x7f), $r)
        $r = BitAND(BitShift($aStr[$i], $rs), 0x7f)
        $rs -= 1
        $ls += 1
        $sEncoded &= $aB128[$nc]
    Next

    Return $sEncoded

EndFunc   ;==>_Base128Encode

; #FUNCTION# ====================================================================================================================
; Name...........: _Base91Encode
; Description ...: Decodes string from Base128
; Author ........: Brian J Christy (Beege)
; Source ........: https://github.com/seizu/base128/blob/master/base128.php  [Erich Pribitzer]
; ===============================================================================================================================
Func _Base128Decode($sStr)

    Local $sB128 = '!#$%()*,.0123456789:;=@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_abcdefghijklmnopqrstuvwxyz{|}~?¢£¤¥|§¨?a???ˉ°±23′μ?·?1o?????àá??????èéê?ìí?'
    Local $sDecoded, $r, $rs = 8, $ls = 7, $aStr = StringSplit($sStr, '', 2)

    For $i = 0 To UBound($aStr) - 1
        $nc = StringInStr($sB128, $aStr[$i], 1) - 1
        If $rs > 7 Then
            $rs = 1
            $ls = 7
            $r = $nc
            ContinueLoop
        EndIf
        $r1 = $nc
        $nc = BitOR(BitAND(BitShift($nc, ($ls * - 1)), 0xFF), $r)
        $r = BitShift($r1, $rs)
        $rs += 1
        $ls -= 1
        $sDecoded &= Chr($nc)
    Next

    Return $sDecoded

EndFunc   ;==>_Base128Decode

±n8M:?BF°n′S%Py]_?;UμPxEbx=:@P§[[n)w%

bad4?b8aa9505e?)ee6176feb1ec1aa9 **Test Failed**

Base128  The result of the decryption and encryption string is not the same as before

Edited by xz00311
±n8M:?BF°n′S%Py]_?;UμPxEbx=:@P§[[n)w% bad4?b8aa9505e?)ee6176feb1ec1aa9 **Test Failed**
Link to comment
Share on other sites

  • 3 months later...
On 2/22/2016 at 11:05 PM, xz00311 said:
Func _Base128Encode($sStr)

    Local $aB128 = StringSplit('!#$%()*,.0123456789:;=@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_abcdefghijklmnopqrstuvwxyz{|}~?¢£¤¥|§¨?a???ˉ°±23′μ?·?1o?????àá??????èéê?ìí?', '', 2)

EndFunc   ;==>_Base128Encode


Func _Base128Decode($sStr)

    Local $sB128 = '!#$%()*,.0123456789:;=@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_abcdefghijklmnopqrstuvwxyz{|}~?¢£¤¥|§¨?a???ˉ°±23′μ?·?1o?????àá??????èéê?ìí?'

EndFunc   ;==>_Base128Decode

±n8M:?BF°n′S%Py]_?;UμPxEbx=:@P§[[n)w%

bad4?b8aa9505e?)ee6176feb1ec1aa9 **Test Failed**

Base128  The result of the decryption and encryption string is not the same as before

If that's the actual code being used then something went wrong with your copy and paste i think. The character set for $aB128 doesn't match what was originally posted. Make sure you download the .au3 file posted when doing your tests.

±n8M:¶BF°n´S%Py]_½;UµPxEbx=:@P§[[n)w%
bad49c8aa9505e47ee6176feb1ec1aa9
**Test Passed**
Link to comment
Share on other sites

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST APIErrorLog.au3 UDF - A logging Library * Include Dependency Tree (Tool for analyzing script relations) * Show_Macro_Values.au3 *

 

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskSchedulerIE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) * PDF Related:How to get reference to PDF object embeded in IE * IE on Windows 11

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

I also encourage you to check awesome @trancexx code:  * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuffOnHungApp handlerAvoid "AutoIt Error" message box in unknown errors  * HTML editor

winhttp.au3 related : * https://www.autoitscript.com/forum/topic/206771-winhttpau3-download-problem-youre-speaking-plain-http-to-an-ssl-enabled-server-port/

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2023-04-24

Link to comment
Share on other sites

  • 2 weeks later...

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

×
×
  • Create New...