Jump to content

Recommended Posts

Posted (edited)

I've been working on a script to upload files over HTTP, and in doing so needed a way to encode data to Base64. The following is AutoIt code ported from a VB5 program. Enjoy.

Update: Thanks to gcriaco's asking, I've cleaned the code up so it should work as a standard UDF library. It's attached here as Base64.au3, and the example to show how it works is here as Base64_Example.au3. Thanks to griaco and trancexx for their help.

Edited by turbov21
Posted (edited)

Please convert your script to a standard UDF Library to be included in the AutoIt3 installset

See Example Scripts/Standard UDF Library

Maybe you could write two functions: _StringToBase64 and _Base64ToString (like _StringToHex/_HexToString)

Many Thanks

Peppe

How do these look?

How do those in the original post look?

Edited by turbov21
Posted (edited)

@extended macro can be integer only.

Yikes. Let me go fix that. Thanks.

UPDATE: Fixed. I appreciate you catching that. I ran the library against AU3Checker.exe with all the switches and it checked out, but I still missed that.

Edited by turbov21
  • 2 weeks later...
Posted

Thanks for the info, now I can port that into a VBScript library. Now I just need to find MD5 for VBScript. Do we have an MD5 function for AutoIt anywhere here?

  • 5 months later...
Posted

Hi.

It doesn't work for me:

$text = "Überörtlicher Ärger, dem Ökonom wird ärger übel, Muß."
ConsoleWrite("Original: " & $text & @CRLF)

$B64_1 = _Base64Encode($text)
ConsoleWrite("Encoded : " & $B64_1 & @CRLF)

$Text2 = _Base64Decode($B64_1)
ConsoleWrite("DEcoded : " & $Text2 & @CRLF)

$B64_2 = _Base64Encode($Text2)
ConsoleWrite("ENcoded : " & $B64_2 & @CRLF)


; #FUNCTION# ;===============================================================================
;
; Name...........: _Base64Encode
; Description ...: Returns the given strinng encoded as a Base64 string.
; Syntax.........: _Base64Encode($sData)
; Parameters ....: $sData
; Return values .: Success - Base64 encoded string.
;                  Failure - Returns 0 and Sets @Error:
;                  |0 - No error.
;                  |1 - Could not create DOMDocument
;                  |2 - Could not create Element
;                  |3 - No string to return
; Author ........: turbov21
; Modified.......:
; Remarks .......:
; Related .......: _Base64Decode
; Link ..........;
; Example .......; Yes
;
; ;==========================================================================================
Func _Base64Encode($sData)
    Local $oXml = ObjCreate("Msxml2.DOMDocument")
    If Not IsObj($oXml) Then
        SetError(1, 1, 0)
    EndIf

    Local $oElement = $oXml.createElement("b64")
    If Not IsObj($oElement) Then
        SetError(2, 2, 0)
    EndIf

    $oElement.dataType = "bin.base64"
    $oElement.nodeTypedValue = Binary($sData)
    Local $sReturn = $oElement.Text

    If StringLen($sReturn) = 0 Then
        SetError(3, 3, 0)
    EndIf

    Return $sReturn
EndFunc   ;==>_Base64Encode

; #FUNCTION# ;===============================================================================
;
; Name...........: _Base64Decode
; Description ...: Returns the strinng decoded from the provided Base64 string.
; Syntax.........: _Base64Decode($sData)
; Parameters ....: $sData
; Return values .: Success - String decoded from Base64.
;                  Failure - Returns 0 and Sets @Error:
;                  |0 - No error.
;                  |1 - Could not create DOMDocument
;                  |2 - Could not create Element
;                  |3 - No string to return
; Author ........: turbov21
; Modified.......:
; Remarks .......:
; Related .......: _Base64Encode
; Link ..........;
; Example .......; Yes
;
; ;==========================================================================================
Func _Base64Decode($sData)
    Local $oXml = ObjCreate("Msxml2.DOMDocument")
    If Not IsObj($oXml) Then
        SetError(1, 1, 0)
    EndIf

    Local $oElement = $oXml.createElement("b64")
    If Not IsObj($oElement) Then
        SetError(2, 2, 0)
    EndIf

    $oElement.dataType = "bin.base64"
    $oElement.Text = $sData
    Local $sReturn = BinaryToString($oElement.nodeTypedValue, 4)

    If StringLen($sReturn) = 0 Then
        SetError(3, 3, 0)
    EndIf

    Return $sReturn
EndFunc   ;==>_Base64Decode

Output:

Original: Überörtlicher Ärger, dem Ökonom wird ärger übel, Muß.
Encoded : 3GJlcvZydGxpY2hlciDEcmdlciwgZGVtINZrb25vbSB3aXJkIORyZ2VyIPxiZWwsIE11
3y4=
DEcoded : berrtlicher rger, dem konom wird rger bel, Mu.
ENcoded : YmVycnRsaWNoZXIgcmdlciwgZGVtIGtvbm9tIHdpcmQgcmdlciBiZWwsIE11Lg==

Do I miss something important?

Regards, Rudi.

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Posted

I don't think it's an issue of language - but rather type characters used

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

  • 1 month later...
Posted (edited)

$fh=FileOpen('hush.jpg', 16)
$image=FileRead($fh)
FileClose($fh)
$str = _Base64Encode($image)
$str = _Base64Decode($image)
$filetest = FileOpen("test2.jpg",2+16)
FileWrite($filetest,$str)

this does not work I get: C:\Program Files (x86)\AutoIt3\Include\Base64.au3 (75) : ==> The requested action with this object has failed.:

$oElement.Text = $sData

$oElement.Text = $sData^ ERROR

Edited by Electon
Posted (edited)

$str = _Base64Encode($image)
$str = _Base64Decode($str)

that was my mistake but it is still not working:

$fh=FileOpen('hush.jpg', 16)
$image=FileRead($fh)
FileClose($fh)
$str = _Base64Encode($image)
$str = _Base64Decode($str)
$filetest = FileOpen("test2.jpg",2+16)
FileWrite($filetest,$str)

the resulting jpg image doesnt work.

Edited by Electon
  • 5 weeks later...
Posted

its because the encode-function seems to encode an ansi-string

$oElement.nodeTypedValue = Binary($sData)
  if you decode a utf-8, this doesn´t work

in the decoder it should be

Local $sReturn = BinaryToString($oElement.nodeTypedValue, 1)
 

You are right, but your solution is not complete:

it would be better to replace this in the encoder:

$oElement.nodeTypedValue = Binary($sData)
with
$oElement.nodeTypedValue = StringtoBinary($sData,X)

And in the decoder, it should be

Local $sReturn = BinaryToString($oElement.nodeTypedValue, X)

where X is depending of what you are encoding/decoding.(1=ansi, 4=UTF8..)

  • 1 year later...
Posted

Hello all! :D

I wrote a script a while ago using these functions and it used to work OK, but now I have a problem. I can't remember the AutoIt version that I was using back then, but the command:

_Base64Decode("some text, not Base64 encoded")

used to just return the same string and now it crashes the program with "The requested action with this object has failed" for the line "$oElement.Text = $sData"

The problem is that I have a mixed file with a normal text and some Base64 lines inside, but there is no way I can tell which lines are Base64.

I was reading the file line by line and feeding the lines into the _Base64Decode and it worked fine, but now this approach does not work, because the first normal text line is craching the program.

Any ideas how I can get around this?

Posted

I found a kind of workaround. :D

Apparently the _Base64Decode function crashes only if the length of the string passed to it is not dividable by 4.

So what I did was a simple check:

If IsInt(StringLen($sStringToDecode)/4) Then
   $sDecoded=Base64Decode($sStringToDecode)
EndIf

This keeps the script from crashing and does the trick for me, even though it renders the non encrypted strings unreadable. :oops:

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...