Sign in to follow this  
Followers 0
BitByteBit

Problem with Base64 Encoding/Decoding

1 post in this topic

#1 ·  Posted (edited)

Ok, so I got in cryptography a little bit after reading Digital Fortress.

So I wrote this neat little app in 5 mins to encode a string, first in Base64, then to turn the result into hex.

Now I know, I know, this is in no way, what so ever secure, and that I should probably use StringEncrypt (right?).

But I did it for funzies ok?

The program works great, the first time. If you try to encode/decode a second string, it returns a blank.

GuiCtrlRead is returning 0 on the second attempt. Why???

Could anybody explain why it doesn't work a second time?

I'm an idiot, used the same variable for the result and the input box. Delete this thread?

Thanks,

Tris.

#cs
Credits go to Ward for the Base64 methods.
http://www.autoitscript.com/forum/index.php?showtopic=76976
#ce
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <string.au3>
Opt("GUICloseOnESC", 0)
$Form1_1 = GUICreate("Enigma", 298, 195, 192, 124)
GUISetBkColor(0x000000)
$Decrypt = GUICtrlCreateInput("", 16, 128, 265, 45)
GUICtrlSetFont(-1, 26, 400, 0, "MS Sans Serif")
$Encrypt = GUICtrlCreateInput("", 16, 40, 265, 45)
GUICtrlSetFont(-1, 26, 400, 0, "MS Sans Serif")
$EncryptButton = GUICtrlCreateButton("Encrypt", 104, 8, 75, 25, $WS_GROUP)
GUICtrlSetBkColor(-1, 0x00FF00)
$DecryptButton = GUICtrlCreateButton("Decrypt", 104, 96, 75, 25, $WS_GROUP)
GUICtrlSetBkColor(-1, 0x00FF00)
GUISetState(@SW_SHOW)
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $DecryptButton
            Decrypt()
        Case $EncryptButton
            Encrypt()
    EndSwitch
WEnd

Func Decrypt()
    $String = GUICtrlRead($Decrypt)
    MsgBox(0,"",$String )
    $String = StringReplace($String, " ", "")
    $Hex = _HexToString($String)
    ;MsgBox(0,"",$Hex)
    $Result = _Base64Decode($Hex)
    $Result = BinaryToString($Result)
    MsgBox(0, '', $Result)
    GUICtrlSetData($Decrypt,"")
    GUICtrlSetData($Decrypt,$String)
EndFunc   ;==>Decrypt

Func Encrypt()
    $String = GUICtrlRead($Encrypt)
    ;MsgBox(0, "", $Hex)
    $Result = _Base64Encode($String)
    $Result = _StringToHex($Result)
    MsgBox(0, '', $Result, 2)
    ClipPut($Result)
    ToolTip("Put in clipboard")
    Sleep(500)
    ToolTip("")
EndFunc   ;==>Encrypt

Func _Base64Decode($Data)
    Local $Opcode = "0xC81000005356578365F800E8500000003EFFFFFF3F3435363738393A3B3C3DFFFFFF00FFFFFF000102030405060708090A0B0C0D0E0F10111213141516171819FFFFFFFFFFFF1A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132338F45F08B7D0C8B5D0831D2E9910000008365FC00837DFC047D548A034384C0750383EA033C3D75094A803B3D75014AB00084C0751A837DFC047D0D8B75FCC64435F400FF45FCEBED6A018F45F8EB1F3C2B72193C7A77150FB6F083EE2B0375F08A068B75FC884435F4FF45FCEBA68D75F4668B06C0E002C0EC0408E08807668B4601C0E004C0EC0208E08847018A4602C0E00624C00A46038847028D7F038D5203837DF8000F8465FFFFFF89D05F5E5BC9C21000"

    Local $CodeBuffer = DllStructCreate("byte[" & BinaryLen($Opcode) & "]")
    DllStructSetData($CodeBuffer, 1, $Opcode)

    Local $Ouput = DllStructCreate("byte[" & BinaryLen($Data) & "]")
    Local $Ret = DllCall("user32.dll", "int", "CallWindowProc", "ptr", DllStructGetPtr($CodeBuffer), _
            "str", $Data, _
            "ptr", DllStructGetPtr($Ouput), _
            "int", 0, _
            "int", 0)

    Return BinaryMid(DllStructGetData($Ouput, 1), 1, $Ret[0])
EndFunc   ;==>_Base64Decode

Func _Base64Encode($Data, $LineBreak = 76)
    Local $Opcode = "0x5589E5FF7514535657E8410000004142434445464748494A4B4C4D4E4F505152535455565758595A6162636465666768696A6B6C6D6E6F707172737475767778797A303132333435363738392B2F005A8B5D088B7D108B4D0CE98F0000000FB633C1EE0201D68A06880731C083F901760C0FB6430125F0000000C1E8040FB63383E603C1E60409C601D68A0688470183F90176210FB6430225C0000000C1E8060FB6730183E60FC1E60209C601D68A06884702EB04C647023D83F90276100FB6730283E63F01D68A06884703EB04C647033D8D5B038D7F0483E903836DFC04750C8B45148945FC66B80D0A66AB85C90F8F69FFFFFFC607005F5E5BC9C21000"

    Local $CodeBuffer = DllStructCreate("byte[" & BinaryLen($Opcode) & "]")
    DllStructSetData($CodeBuffer, 1, $Opcode)

    $Data = Binary($Data)
    Local $Input = DllStructCreate("byte[" & BinaryLen($Data) & "]")
    DllStructSetData($Input, 1, $Data)

    $LineBreak = Floor($LineBreak / 4) * 4
    Local $OputputSize = Ceiling(BinaryLen($Data) * 4 / 3)
    $OputputSize = $OputputSize + Ceiling($OputputSize / $LineBreak) * 2 + 4

    Local $Ouput = DllStructCreate("char[" & $OputputSize & "]")
    DllCall("user32.dll", "none", "CallWindowProc", "ptr", DllStructGetPtr($CodeBuffer), _
            "ptr", DllStructGetPtr($Input), _
            "int", BinaryLen($Data), _
            "ptr", DllStructGetPtr($Ouput), _
            "uint", $LineBreak)
    Return DllStructGetData($Ouput, 1)
EndFunc   ;==>_Base64Encode
Edited by BitByteBit

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