Base64 in Kotlin

How to encode and decode Base64 in Kotlin — with copy-paste examples and a live converter to check your output.

On the JVM, Kotlin uses java.util.Base64 directly. Kotlin 1.8+ also ships an experimental kotlin.io.encoding.Base64 for multiplatform code.

Encode to Base64 in Kotlin

Kotlin
import java.util.Base64

val encoded = Base64.getEncoder().encodeToString("Hello, World!".toByteArray(Charsets.UTF_8))

Decode Base64 in Kotlin

Kotlin
import java.util.Base64

val decoded = Base64.getDecoder().decode(encoded)
val text = String(decoded, Charsets.UTF_8)

Notes & gotchas

Use Base64.getUrlEncoder() for the URL-safe alphabet. For Kotlin Multiplatform (non-JVM targets), use the experimental kotlin.io.encoding.Base64.Default / Base64.UrlSafe objects instead of java.util.Base64.

Try it live

0 characters

Base64 in Kotlin — FAQ

How do I Base64 encode a string in Kotlin?+

Use the code shown above. Use Base64.getUrlEncoder() for the URL-safe alphabet. For Kotlin Multiplatform (non-JVM targets), use the experimental kotlin.io.encoding.Base64.Default / Base64.UrlSafe objects instead of java.util.Base64.

How do I decode Base64 back to text in Kotlin?+

Use the decode snippet above. Base64 decoding is lossless, so you get the exact original bytes back; decode them with UTF-8 to recover text.

Is Base64 encoding the same across programming languages?+

Yes. Base64 is a standard (RFC 4648), so a string encoded in one language decodes correctly in any other. Only the API differs, not the output.

Does Base64 secure my data?+

No. Base64 is an encoding, not encryption — anyone can decode it. Never use it to protect secrets.