Notifications
Clear all

[Closed] Base64 encoding/decoding

I’m playing around with encoding/decoding strings, which is a great way to scramble text and/or reconstruct sanitized strings coming from webforms.

This is a slightly modified function I’ve taken from this thread: http://forums.cgsociety.org/showthread.php?f=98&t=643827&highlight=dnoUTF8

(

	fn base64encode str=
	(
		dncConvert = dotNetClass "System.Convert"
		dnoUTF8 = dotNetObject "System.Text.UTF8Encoding"
		dnvStringIn = dotNetObject "System.String" str
		dnvByteArrayIn = dnoUTF8.getBytes dnvStringIn
		dnvB64String = dncConvert.ToBase64String dnvByteArrayIn
	)

	fn base64decode str=
	(
		dncConvert = dotNetClass "System.Convert"
		dnoUTF8 = dotNetObject "System.Text.UTF8Encoding"
		dnvByteArrayOut = dncConvert.FromBase64String str
		dnvStringOut = dnoUTF8.getString dnvByteArrayOut
	)

	encodedString=base64encode "This is a test. Umlauts: äüöÄÖÜß"
	print encodedString	--result: "VGhpcyBpcyBhIHRlc3QuIFVtbGF1dHM6IMOkw7zDtsOEw5bDnMOf"
	
	decodedString=base64decode "VGhpcyBpcyBhIHRlc3QuIFVtbGF1dHM6IMOkw7zDtsOEw5bDnMOf"
	print decodedString --result: "This is a test. Umlauts: äüöÄÖÜß"
	
		--string encoded with php, includes chinese characters
	decodedString=base64decode "5rGJ6K+tL+a8ouiqng=="
	print decodedString --result: ""

)

Basic encoding/decoding works 100%, including umlauts.
One thing that doesn’t work, when characters are used that are not supported in the maxscript editor, then the decoding function always returns an empty string.
This is not nice. In my example I’ve used chinese characters, encoded with a php script.
I’d appreciate any suggestions how to improve this.