Sign in to follow this  
Followers 0
jjohn

64 base encode and decode

9 posts in this topic

#1 ·  Posted (edited)

This code is inspired by other similar codes posted here, it hasn't been test throughoutly, if you find any bugs, please let me know.

Thanks

Func convert to and from base64 char b64EnDe.au3

Example EnDe_example.au3

Edited by jjohn

Share this post


Link to post
Share on other sites



Exactly this is I was searching for and for me it works like a charm :whistle:

Thx! Good job!

Share this post


Link to post
Share on other sites

This code is inspired by other similar codes posted here, it hasn't been test throughoutly, if you find any bugs, please let me know.

Thanks

Func convert to and from base64 char b64EnDe.au3

Example EnDe_example.au3

Hi, with this, I can turn my scripts into native 64bit programs? Sweet!

Share this post


Link to post
Share on other sites

Quick GUI for this...

;==============================
;
;     Initialization
;
;==============================

#include <GuiConstants.au3>

Global Const $b64ch[64] = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', _ 
                  'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', _
                  'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', _
                  'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', _
                  'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', _
                  'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', _
                  '8', '9', '+', '/']

Global Const $b64Rch[123] = [ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,     0,  0,  0,  0,  0,  0,  0,  0,  0,  0, _
                         0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, _ 
                         0,  0,  62, 0,  0,  0, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,  0,  0,  0, _
                         0,  0,  0,  0,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, _
                        16, 17, 18, 19, 20, 21, 22, 23, 24, 25,  0,  0,  0,  0,  0,  0, 26, 27, 28, 29, _
                        30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, _
                        50, 51]

;==============================
;
;     GUI Creation
;
;==============================

If Not IsDeclared('WS_CLIPSIBLINGS') Then Global $WS_CLIPSIBLINGS = 0x04000000

GuiCreate("Base64 Encoder and Decoder", 460, 321,(@DesktopWidth-460)/2, (@DesktopHeight-321)/2 , $WS_OVERLAPPEDWINDOW + $WS_VISIBLE + $WS_CLIPSIBLINGS)

$Edit_1 = GuiCtrlCreateEdit("", 10, 10, 440, 270, $ES_MULTILINE + $WS_VSCROLL + $ES_AUTOVSCROLL)
$Button_2 = GuiCtrlCreateButton("Encode", 10, 290, 80, 20)
$Button_3 = GuiCtrlCreateButton("Clear", 190, 290, 80, 20)
$Button_4 = GuiCtrlCreateButton("Decode", 100, 290, 80, 20)
$Button_5 = GuiCtrlCreateButton("Open File", 280, 290, 80, 20)
$Button_6 = GuiCtrlCreateButton("Save File", 370, 290, 80, 20)

;==============================
;
;     Code Body
;
;==============================

GuiSetState()
While 1
    $msg = GuiGetMsg()
    Select
        Case $msg = $Button_2       
            en64b()
            GUICtrlSetData($Edit_1, en64b())

        Case $msg = $Button_3
            GUICtrlSetData($Edit_1, "")  

        Case $msg = $Button_4
            de64b()
            GUICtrlSetData($Edit_1, de64b())

        Case $msg = $Button_5
            _TextfromFile2()
        
        Case $msg = $Button_6
            $text = GUICtrlRead($Edit_1)
            $file = FileSaveDialog("Save to where", @ScriptDir, "All files (*.*)", 16)
            FileWrite($file, $text)

    Case $msg = $GUI_EVENT_CLOSE
        ExitLoop
    Case Else
    ;;;
    EndSelect
WEnd
Exit

;==============================
;
;     Included Functions
;
;==============================

Func en64b()
    $txt = GUICtrlRead($Edit_1)
    $res = ''
    $arrt = StringSplit($txt,'')
    For $a1 = 1 To $arrt[0]
        Select
            Case Mod($a1, 3) = 1
                $aa = $b64ch[BitShift(Asc($arrt[$a1]), 2)]
                $at = BitAND(BitShift(Asc($arrt[$a1]), -4),48)
                If $a1 = $arrt[0] Then 
                    $res = $res & $aa & $b64ch[$at] & '=='
                    Return $res
                EndIf   
            Case Mod($a1, 3) = 2
                $aa = $b64ch[$at+BitShift(Asc($arrt[$a1]), 4)]
                $at = BitAND(BitShift(Asc($arrt[$a1]), -2),60)
                If $a1 = $arrt[0] Then 
                    $res = $res & $aa & $b64ch[$at] & '='
                    Return $res
                EndIf   
            Case Mod($a1, 3) = 0 
                $aa = $b64ch[$at+BitShift(Asc($arrt[$a1]), 6)] & $b64ch[BitAND(Asc($arrt[$a1]), 63)]
                If $a1 = $arrt[0] Then 
                    $res &= $aa
                    Return $res
                EndIf   
        EndSelect
        $res &= $aa
    Next
    Return $res
EndFunc 

Func de64b()
    $txt = GUICtrlRead($Edit_1)
    $res = ''
    $arrt = StringSplit(StringReplace($txt,'=',''),'')
    For $a1 = 1 To $arrt[0]
        If Mod($a1, 4) = 1 Then ContinueLoop
        Select
            Case Mod($a1, 4) = 2
                $at = BitShift($b64Rch[Asc($arrt[$a1-1])], -2) + BitShift($b64Rch[Asc($arrt[$a1])], 4)
                $aa = BitShift($b64Rch[Asc($arrt[$a1])], -4)
                If $a1 = $arrt[0] Then 
                    $res &= chr($at)
                    Return $res
                EndIf   
            Case Mod($a1, 4) = 3 
                $at = $aa + BitShift($b64Rch[Asc($arrt[$a1])], 2)
                $aa = BitShift($b64Rch[Asc($arrt[$a1])], -6) 
                If $a1 = $arrt[0] Then 
                    $res &= chr($at)
                    Return $res
                EndIf   
            Case Mod($a1, 4) = 0 
                $at = $aa + $b64Rch[Asc($arrt[$a1])]
                If $a1 = $arrt[0] Then 
                    $res &= chr($at)
                    Return $res
                EndIf   
        EndSelect
        $res &= chr($at)
    Next
    Return $res
EndFunc 

;================_TextfromFile2() Function

Func _TextfromFile2()
    $file = FileOpenDialog("Open what file?", "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}", "All files (*.*)")
        If Not @error Then
            $chars = FileRead($file, FileGetSize($file))
            GUICtrlSetData($edit_1, $chars)
        EndIf
EndFunc

;==============================
;
;     Script Termination
;
;==============================

Func OnAutoItExit()

    ClipPut("")

EndFunc

Exit

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Actually, in doing some testing I noticed that this code is pretty slow. It does work as advertised, but I'm using it as part of a loop to decode multiple files, and it takes a really long time.

I did some more searching and found this alternative implementation:

http://www.autoitscript.com/forum/index.ph...st&p=148460

The decode function (which is my primary interest) works significantly faster. I recommend checking it out.

Edited by nitro322

Share this post


Link to post
Share on other sites

Here's my short version. I've seen many base64 decoder here and almost all of them are either too slow, too big, or decode incorrectly so I decided to write my own. Take a look for yourself and hopefully it'll help save you some time on base64 decoder. Any feedback is welcome. Happy programming...

Func Base64Decode($s)

Local $key = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=', _

$t = '', $p = -8, $a = 0, $c, $d, $len = StringLen($s)

For $i = 1 to $len

$c = StringInStr($key, StringMid($s,$i,1), 1) - 1

If $c < 0 Then ContinueLoop

$a = BitOR(BitShift($a, -6), BitAND($c, 63))

$p = $p + 6

If $p >= 0 Then

$d = BitAND(BitShift($a, $p), 255)

If $c <> 64 Then $t = $t & Chr($d)

$a = BitAND($a, 63)

$p = $p - 8

EndIf

Next

Return $t

EndFunc

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