turbov21 Posted December 29, 2009 Share Posted December 29, 2009 (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 December 24, 2011 by turbov21 mLipok 1 Link to comment Share on other sites More sharing options...
trancexx Posted December 29, 2009 Share Posted December 29, 2009 Great! ♡♡♡ . eMyvnE Link to comment Share on other sites More sharing options...
gcriaco Posted December 29, 2009 Share Posted December 29, 2009 (edited) Please convert your script to a standard UDF Library to be included in the AutoIt3 installsetSee Example Scripts/Standard UDF LibraryMaybe you could write two functions: _StringToBase64 and _Base64ToString (like _StringToHex/_HexToString)Many ThanksPeppe Edited December 29, 2009 by gcriaco Link to comment Share on other sites More sharing options...
turbov21 Posted December 29, 2009 Author Share Posted December 29, 2009 (edited) Please convert your script to a standard UDF Library to be included in the AutoIt3 installsetSee Example Scripts/Standard UDF LibraryMaybe you could write two functions: _StringToBase64 and _Base64ToString (like _StringToHex/_HexToString)Many ThanksPeppeHow do these look?How do those in the original post look? Edited December 29, 2009 by turbov21 Link to comment Share on other sites More sharing options...
trancexx Posted December 29, 2009 Share Posted December 29, 2009 How do these look?@extended macro can be integer only. ♡♡♡ . eMyvnE Link to comment Share on other sites More sharing options...
turbov21 Posted December 29, 2009 Author Share Posted December 29, 2009 (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 December 29, 2009 by turbov21 Link to comment Share on other sites More sharing options...
daluu Posted January 8, 2010 Share Posted January 8, 2010 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? Link to comment Share on other sites More sharing options...
rudi Posted June 21, 2010 Share Posted June 21, 2010 Hi. It doesn't work for me: expandcollapse popup$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! Link to comment Share on other sites More sharing options...
Baraoic Posted June 22, 2010 Share Posted June 22, 2010 (edited) My guess is it doesn't work with German because I just tried it in English and it worked. Edited June 22, 2010 by Onichan Link to comment Share on other sites More sharing options...
ripdad Posted June 22, 2010 Share Posted June 22, 2010 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 Link to comment Share on other sites More sharing options...
Electon Posted August 18, 2010 Share Posted August 18, 2010 can this be used to encode a file to base64? Link to comment Share on other sites More sharing options...
AndyG Posted August 18, 2010 Share Posted August 18, 2010 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 beLocal $sReturn = BinaryToString($oElement.nodeTypedValue, 1) Link to comment Share on other sites More sharing options...
Electon Posted August 23, 2010 Share Posted August 23, 2010 (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 August 23, 2010 by Electon Link to comment Share on other sites More sharing options...
JFX Posted August 23, 2010 Share Posted August 23, 2010 $str = _Base64Encode($image) $str = _Base64Decode($str) Link to comment Share on other sites More sharing options...
Electon Posted August 23, 2010 Share Posted August 23, 2010 (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 August 23, 2010 by Electon Link to comment Share on other sites More sharing options...
JFX Posted August 23, 2010 Share Posted August 23, 2010 the resulting jpg image doesnt work.Do you have fixed the line AndyG had posted? Link to comment Share on other sites More sharing options...
sksbir Posted September 23, 2010 Share Posted September 23, 2010 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 beLocal $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 beLocal $sReturn = BinaryToString($oElement.nodeTypedValue, X) where X is depending of what you are encoding/decoding.(1=ansi, 4=UTF8..) Link to comment Share on other sites More sharing options...
dv8 Posted November 7, 2011 Share Posted November 7, 2011 Hello all! 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? Free Remote Desktop Software For Windows Link to comment Share on other sites More sharing options...
dv8 Posted November 7, 2011 Share Posted November 7, 2011 I found a kind of workaround. 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) EndIfThis keeps the script from crashing and does the trick for me, even though it renders the non encrypted strings unreadable. Free Remote Desktop Software For Windows Link to comment Share on other sites More sharing options...
dv8 Posted November 9, 2011 Share Posted November 9, 2011 OK, just to let you know that the above approach was also failing in some cases, so I had to find alternative functions...These are working just fine for me: Free Remote Desktop Software For Windows Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now