Pete Freitag Pete Freitag

URL Safe Base64 Encoding / Decoding in CFML

coldfusion

ColdFusion / CFML has a builtin function that can convert a string or a binary object to a standard Base64 encoded string: toBase64 and you can decode back to a string using toBinary() and toString() or the binaryDecode() function.

These builtin functions are quite handy, but if you need to pass the encoded value in a URL, or simply want a nicer looking encoding of a value then as of Java 8 there is a builtin class java.util.Base64 which has a URL Safe Base64 encoder implementation. The standard base64 implementation has characters such a / or +, and padding characters =.

Here's how you can use this in CFML assuming you have Java 8 or greater:

function urlSafeBase64Encode(str) {
	return createObject("java", "java.util.Base64").getUrlEncoder().withoutPadding().encodeToString(str.getBytes("UTF-8"));
}

function urlSafeBase64Decode(str) {
	var bytes = createObject("java", "java.util.Base64").getUrlDecoder().decode(str);
	return createObject("java", "java.lang.String").init(bytes);
}

Like this? Follow me ↯

URL Safe Base64 Encoding / Decoding in CFML was first published on April 22, 2021.

If you like reading about coldfusion, cfml, or java then you might also like:

FuseGuard Web App Firewall for ColdFusion

The FuseGuard Web Application Firewall for ColdFusion & CFML is a high performance, customizable engine that blocks various attacks against your ColdFusion applications.

Post a Comment