What is special about this way. Well, I did some testing and it turns that it's fast. About three times faster than the fastest way posted here on this forum (machine code implementation).
It's calling functions "CryptBinaryToString" and "CryptStringToBinary" in Crypt32.dll
I'm in the phase when discovering functions DllCall(), DllStructCreate(),... and related, so please be gentle to me, lol
There is no error checking mainly because of the sentence before this one. If someone is willing to add that to the code below (or correct possible mistakes), I'm willing to learn out of that. So, please do.
Here's the script:
Updated 29th October 2008 - error checking added and some code optimization
$encoded = _Base64Encode("Testing") MsgBox(0, 'Base64 Encoded', $encoded) $decoded = _Base64Decode($encoded) MsgBox(0, 'Base64 Decoded - binary', $decoded) MsgBox(0, 'Base64 Decoded - string', BinaryToString($decoded)) Func _Base64Encode($input) $input = Binary($input) Local $struct = DllStructCreate("byte[" & BinaryLen($input) & "]") DllStructSetData($struct, 1, $input) Local $strc = DllStructCreate("int") Local $a_Call = DllCall("Crypt32.dll", "int", "CryptBinaryToString", _ "ptr", DllStructGetPtr($struct), _ "int", DllStructGetSize($struct), _ "int", 1, _ "ptr", 0, _ "ptr", DllStructGetPtr($strc)) If @error Or Not $a_Call[0] Then Return SetError(1, 0, "") ; error calculating the length of the buffer needed EndIf Local $a = DllStructCreate("char[" & DllStructGetData($strc, 1) & "]") $a_Call = DllCall("Crypt32.dll", "int", "CryptBinaryToString", _ "ptr", DllStructGetPtr($struct), _ "int", DllStructGetSize($struct), _ "int", 1, _ "ptr", DllStructGetPtr($a), _ "ptr", DllStructGetPtr($strc)) If @error Or Not $a_Call[0] Then Return SetError(2, 0, ""); error encoding EndIf Return DllStructGetData($a, 1) EndFunc ;==>_Base64Encode Func _Base64Decode($input_string) Local $struct = DllStructCreate("int") $a_Call = DllCall("Crypt32.dll", "int", "CryptStringToBinary", _ "str", $input_string, _ "int", 0, _ "int", 1, _ "ptr", 0, _ "ptr", DllStructGetPtr($struct, 1), _ "ptr", 0, _ "ptr", 0) If @error Or Not $a_Call[0] Then Return SetError(1, 0, "") ; error calculating the length of the buffer needed EndIf Local $a = DllStructCreate("byte[" & DllStructGetData($struct, 1) & "]") $a_Call = DllCall("Crypt32.dll", "int", "CryptStringToBinary", _ "str", $input_string, _ "int", 0, _ "int", 1, _ "ptr", DllStructGetPtr($a), _ "ptr", DllStructGetPtr($struct, 1), _ "ptr", 0, _ "ptr", 0) If @error Or Not $a_Call[0] Then Return SetError(2, 0, ""); error decoding EndIf Return DllStructGetData($a, 1) EndFunc ;==>_Base64Decode
There is @CRLF every 64 chars in return of _Base64Encode().
_Base64Decode() will return binary data. That is intentional to avoid Chr(0) issue. Convert it to string using BinaryToString()
Microsoft about requirements:
Client - Requires Windows Vista or Windows XP.
Server - Requires Windows Server 2008 or Windows Server 2003.
Edited by trancexx, 29 October 2008 - 10:04 PM.





